use of java.nio.CharBuffer in project buck by facebook.
the class NulTerminatedCharsetDecoderTest method nulTerminatedEmptyBufferDecodesToEmptyBuffer.
@Test
public void nulTerminatedEmptyBufferDecodesToEmptyBuffer() {
NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
ByteBuffer in = decodeHex("00");
CharBuffer out = CharBuffer.allocate(0);
assertThat(in.position(), is(equalTo(0)));
assertThat(in.limit(), is(equalTo(1)));
assertThat(out.position(), is(equalTo(0)));
assertThat(out.limit(), is(equalTo(0)));
NulTerminatedCharsetDecoder.Result result = decoder.decode(in, out, true);
assertThat(in.position(), is(equalTo(1)));
assertThat(in.limit(), is(equalTo(1)));
assertThat(out.position(), is(equalTo(0)));
assertThat(out.limit(), is(equalTo(0)));
assertThat(result, is(equalTo(new NulTerminatedCharsetDecoder.Result(true, CoderResult.UNDERFLOW))));
}
use of java.nio.CharBuffer in project buck by facebook.
the class NulTerminatedCharsetDecoderTest method invalidUTF8BufferReturnsMalformedResult.
@Test
public void invalidUTF8BufferReturnsMalformedResult() {
NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
ByteBuffer in = decodeHex("C0FFEE00");
CharBuffer out = CharBuffer.allocate(4);
assertThat(in.position(), is(equalTo(0)));
assertThat(in.limit(), is(equalTo(4)));
assertThat(out.position(), is(equalTo(0)));
assertThat(out.limit(), is(equalTo(4)));
NulTerminatedCharsetDecoder.Result result = decoder.decode(in, out, true);
assertThat(result, is(equalTo(new NulTerminatedCharsetDecoder.Result(false, CoderResult.malformedForLength(1)))));
assertThat(in.position(), is(equalTo(0)));
assertThat(in.limit(), is(equalTo(4)));
assertThat(out.position(), is(equalTo(0)));
assertThat(out.limit(), is(equalTo(4)));
}
use of java.nio.CharBuffer in project android_frameworks_base by ParanoidAndroid.
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 feign by OpenFeign.
the class Util method toString.
/**
* Adapted from {@code com.google.common.io.CharStreams.toString()}.
*/
public static String toString(Reader reader) throws IOException {
if (reader == null) {
return null;
}
try {
StringBuilder to = new StringBuilder();
CharBuffer buf = CharBuffer.allocate(BUF_SIZE);
while (reader.read(buf) != -1) {
buf.flip();
to.append(buf);
buf.clear();
}
return to.toString();
} finally {
ensureClosed(reader);
}
}
use of java.nio.CharBuffer in project fastjson by alibaba.
the class JSON method parse.
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) {
charsetDecoder.reset();
int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte());
char[] chars = allocateChars(scaleLength);
ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len);
CharBuffer charBuf = CharBuffer.wrap(chars);
IOUtils.decode(charsetDecoder, byteBuf, charBuf);
int position = charBuf.position();
DefaultJSONParser parser = new DefaultJSONParser(chars, position, ParserConfig.getGlobalInstance(), features);
Object value = parser.parse();
parser.handleResovleTask(value);
parser.close();
return value;
}
Aggregations