use of java.nio.charset.CoderResult in project robovm by robovm.
the class CharsetDecoderTest method testDecodeLjava_nio_ByteBuffer_ReplaceOverflow.
/*
* Test the method decode(ByteBuffer) .
*/
public void testDecodeLjava_nio_ByteBuffer_ReplaceOverflow() throws Exception {
String replaceString = "a";
Charset cs = Charset.forName("UTF-8");
MockMalformedDecoder decoder = new MockMalformedDecoder(cs);
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.replaceWith(replaceString);
CharBuffer out = CharBuffer.allocate(1);
// MockMalformedDecoder treats the second byte '0x38' as malformed,
// but "out" doesn't have enough space for replace string.
ByteBuffer in = ByteBuffer.wrap(new byte[] { 0x45, 0x38, 0x45, 0x45 });
CoderResult result = decoder.decode(in, out, false);
assertTrue(result.isOverflow());
// allocate enough space for "out"
out = CharBuffer.allocate(10);
// replace string should be put into "out" firstly,
// and then decode "in".
result = decoder.decode(in, out, true);
out.flip();
assertTrue(result.isUnderflow());
assertEquals("bb", out.toString());
}
use of java.nio.charset.CoderResult 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;
}
}
use of java.nio.charset.CoderResult 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.charset.CoderResult 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.charset.CoderResult in project Japid by branaway.
the class StringUtils method encodeUTF8.
/**
* try to encode a char[] as fast as possible for later use in outputstream
*
* @param ca
* @param off
* @param len
* @return
*/
public static ByteBuffer encodeUTF8(String src) {
// char[] ca = src.toCharArray();
int len = src.length();
int off = 0;
int en = (int) (len * ce.maxBytesPerChar());
byte[] ba = new byte[en];
if (len == 0)
return null;
ce.reset();
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(src, off, len);
try {
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
cr.throwException();
cr = ce.flush(bb);
if (!cr.isUnderflow())
cr.throwException();
return bb;
} catch (CharacterCodingException x) {
// so this shouldn't happen
throw new Error(x);
}
}
Aggregations