/**
* Write the UTF-8 representation of a single Unicode code point to the terminal output. The written data will be consumed by the emulation client as input. <p> This implementation encodes the code point and then calls {@link #write(byte[],int,int)} to do the actual writing. It shouldtherefore usually be unnecessary to override this method; override {@link #write(byte[],int,int)} instead.
* @param codePoint The Unicode code point to write to the terminal.
*/
public void write(int codePoint){
CharBuffer charBuf=mWriteCharBuffer;
ByteBuffer byteBuf=mWriteByteBuffer;
CharsetEncoder encoder=mUTF8Encoder;
charBuf.clear();
byteBuf.clear();
Character.toChars(codePoint,charBuf.array(),0);
encoder.reset();
encoder.encode(charBuf,byteBuf,true);
encoder.flush(byteBuf);
write(byteBuf.array(),0,byteBuf.position() - 1);
}

Example 2

From project android_external_guava, under directory /src/com/google/common/io/.

/**
* Copies all characters between the {@link Readable} and {@link Appendable}objects. Does not close or flush either object.
* @param from the object to read from
* @param to the object to write to
* @return the number of characters copied
* @throws IOException if an I/O error occurs
*/
public static long copy(Readable from,Appendable to) throws IOException {
CharBuffer buf=CharBuffer.allocate(BUF_SIZE);
long total=0;
while (true) {
int r=from.read(buf);
if (r == -1) {
break;
}
buf.flip();
to.append(buf,0,r);
total+=r;
}
return total;
}

Example 3

From project asterisk-java, under directory /src/main/java/org/asteriskjava/config/.

/**
* Creates or re-uses a {@link CharBuffer} that has a minimum size. Callingthis method multiple times with the same key will always return the same buffer, as long as it has the minimum size and is marked to be re-used. Buffers that are allowed to be re-used should be released using {@link #releaseCharBuffer(Key,CharBuffer)}.
* @param key the buffer's identifier
* @param minSize the minimum size
* @return the {@link CharBuffer} instance
*/
public CharBuffer charBuffer(Key key,int minSize){
minSize=Math.max(minSize,GLOBAL_MIN_SIZE);
CharBuffer r=_charBuffers[key.ordinal()];
if (r == null || r.capacity() < minSize) {
r=CharBuffer.allocate(minSize);
}
else {
_charBuffers[key.ordinal()]=null;
r.clear();
}
return r;
}

Example 5

From project cloudhopper-commons-charset, under directory /src/main/java/com/cloudhopper/commons/charset/.

/**
* Create a sort key for a given unicode string. The sort key can be compared instead of the original strings and will compare based on the sorting represented by this Sort class. Using a sort key is more efficient if many comparisons are being done (for example if you are sorting a list of strings).
* @param object This is saved in the sort key for later retrieval and plays no part in the sorting.
* @param s The string for which the sort key is to be created.
* @param second Secondary sort key.
* @param cache A cache for the created keys. This is for saving memory so it is essential that thisis managed by the caller.
* @return A sort key.
*/
public <T>SortKey<T> createSortKey(T object,String s,int second,Map<String,byte[]> cache){
byte[] key;
if (cache != null) {
key=cache.get(s);
if (key != null) return new SrtSortKey<T>(object,key,second);
}
CharBuffer inb=CharBuffer.wrap(s);
try {
ByteBuffer out=encoder.encode(inb);
byte[] bval=out.array();
key=new byte[(bval.length + 1 + 2) * 3];
try {
fillCompleteKey(bval,key);
}
catch ( ArrayIndexOutOfBoundsException e) {
key=new byte[bval.length * 3 * maxExpSize + 3];
}
if (cache != null) cache.put(s,key);
return new SrtSortKey<T>(object,key,second);
}
catch ( CharacterCodingException e) {
return new SrtSortKey<T>(object,ZERO_KEY);
}
}

/**
* Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed.
* @param target the buffer to read characters into
* @return The number of characters added to the buffer, or-1 if this source of characters is at its end
* @throws java.io.IOException if an I/O error occurs
* @throws NullPointerException if target is null
* @throws java.nio.ReadOnlyBufferException if target is a read only buffer
*/
@Override public int read(CharBuffer target) throws IOException {
synchronized (lock) {
int len=target.remaining();
char[] cbuf=new char[len];
int n=read(cbuf,0,len);
if (n > 0) {
target.put(cbuf,0,n);
}
return n;
}
}

Example 57

From project ciel-java, under directory /examples/Grep/src/skywriting/examples/grep/.

/**
* Create a new iterator with no parent and a prefix. <p> The prefix path supplied is inserted in front of all paths generated by this iterator. It is intended to be used when an iterator is being created for a subsection of an overall repository and needs to be combined with other iterators that are created to run over the entire repository namespace.
* @param prefix position of this iterator in the repository tree. The value may be null or the empty string to indicate the prefix is the root of the repository. A trailing slash ('/') is automatically appended if the prefix does not end in '/'.
*/
protected AbstractTreeIterator(final String prefix){
parent=null;
if (prefix != null && prefix.length() > 0) {
final ByteBuffer b;
b=Constants.CHARSET.encode(CharBuffer.wrap(prefix));
pathLen=b.limit();
path=new byte[Math.max(DEFAULT_PATH_SIZE,pathLen + 1)];
b.get(path,0,pathLen);
if (path[pathLen - 1] != '/') path[pathLen++]='/';
pathOffset=pathLen;
}
else {
path=new byte[DEFAULT_PATH_SIZE];
pathOffset=0;
}
}