use of java.nio.CharBuffer in project cassandra by apache.
the class TypeValidationTest method testValidUtf8.
@Test
public void testValidUtf8() throws UnsupportedEncodingException {
assert Character.MAX_CODE_POINT == 0x0010ffff;
CharBuffer cb = CharBuffer.allocate(2837314);
// let's test all of the unicode space.
for (int i = 0; i < Character.MAX_CODE_POINT; i++) {
// valid byte sequences (gives us '?' instead), so there is no point testing them.
if (i >= 55296 && i <= 57343)
continue;
char[] ch = Character.toChars(i);
for (char c : ch) cb.append(c);
}
String s = new String(cb.array());
byte[] arr = s.getBytes("UTF8");
ByteBuffer buf = ByteBuffer.wrap(arr);
UTF8Type.instance.validate(buf);
// some you might not expect.
UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] {}));
// valid Utf8, unspecified in modified utf8.
UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { 0 }));
// modified utf8 null.
UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { 99, (byte) 0xc0, (byte) 0x80, 112 }));
// edges, for my sanity.
UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { (byte) 0xc2, (byte) 0x81 }));
UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { (byte) 0xe0, (byte) 0xa0, (byte) 0x81 }));
UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { (byte) 0xf0, (byte) 0x90, (byte) 0x81, (byte) 0x81 }));
}
use of java.nio.CharBuffer in project platform_frameworks_base by android.
the class TimeFormatter method formatInternal.
/**
* Format the specified {@code wallTime} using {@code pattern}. The output is written to
* {@link #outputBuilder}.
*/
private void formatInternal(String pattern, ZoneInfo.WallTime wallTime, ZoneInfo zoneInfo) {
CharBuffer formatBuffer = CharBuffer.wrap(pattern);
while (formatBuffer.remaining() > 0) {
boolean outputCurrentChar = true;
char currentChar = formatBuffer.get(formatBuffer.position());
if (currentChar == '%') {
outputCurrentChar = handleToken(formatBuffer, wallTime, zoneInfo);
}
if (outputCurrentChar) {
outputBuilder.append(formatBuffer.get(formatBuffer.position()));
}
formatBuffer.position(formatBuffer.position() + 1);
}
}
use of java.nio.CharBuffer in project cassandra by apache.
the class CBUtil method decodeString.
// Taken from Netty's ChannelBuffers.decodeString(). We need to use our own decoder to properly handle invalid
// UTF-8 sequences. See CASSANDRA-8101 for more details. This can be removed once https://github.com/netty/netty/pull/2999
// is resolved in a release used by Cassandra.
private static String decodeString(ByteBuffer src) throws CharacterCodingException {
// the decoder needs to be reset every time we use it, hence the copy per thread
CharsetDecoder theDecoder = TL_UTF8_DECODER.get();
theDecoder.reset();
CharBuffer dst = TL_CHAR_BUFFER.get();
int capacity = (int) ((double) src.remaining() * theDecoder.maxCharsPerByte());
if (dst == null) {
capacity = Math.max(capacity, 4096);
dst = CharBuffer.allocate(capacity);
TL_CHAR_BUFFER.set(dst);
} else {
dst.clear();
if (dst.capacity() < capacity) {
dst = CharBuffer.allocate(capacity);
TL_CHAR_BUFFER.set(dst);
}
}
CoderResult cr = theDecoder.decode(src, dst, true);
if (!cr.isUnderflow())
cr.throwException();
return dst.flip().toString();
}
use of java.nio.CharBuffer in project carat by amplab.
the class FastXmlSerializer method flush.
public void flush() throws IOException {
//Log.i("PackageManager", "flush mPos=" + mPos);
if (mPos > 0) {
if (mOutputStream != null) {
CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
CoderResult result = mCharset.encode(charBuffer, mBytes, true);
while (true) {
if (result.isError()) {
throw new IOException(result.toString());
} else if (result.isOverflow()) {
flushBytes();
result = mCharset.encode(charBuffer, mBytes, true);
continue;
}
break;
}
flushBytes();
mOutputStream.flush();
} else {
mWriter.write(mText, 0, mPos);
mWriter.flush();
}
mPos = 0;
}
}
use of java.nio.CharBuffer in project platform_frameworks_base by android.
the class FastXmlSerializer method flush.
public void flush() throws IOException {
//Log.i("PackageManager", "flush mPos=" + mPos);
if (mPos > 0) {
if (mOutputStream != null) {
CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
CoderResult result = mCharset.encode(charBuffer, mBytes, true);
while (true) {
if (result.isError()) {
throw new IOException(result.toString());
} else if (result.isOverflow()) {
flushBytes();
result = mCharset.encode(charBuffer, mBytes, true);
continue;
}
break;
}
flushBytes();
mOutputStream.flush();
} else {
mWriter.write(mText, 0, mPos);
mWriter.flush();
}
mPos = 0;
}
}
Aggregations