use of java.nio.charset.CoderResult in project android_frameworks_base by ResurrectionRemix.
the class StrictJarManifest method writeEntry.
private static void writeEntry(OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
String nameString = name.toString();
os.write(nameString.getBytes(StandardCharsets.US_ASCII));
os.write(VALUE_SEPARATOR);
encoder.reset();
bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);
CharBuffer cBuf = CharBuffer.wrap(value);
while (true) {
CoderResult r = encoder.encode(cBuf, bBuf, true);
if (CoderResult.UNDERFLOW == r) {
r = encoder.flush(bBuf);
}
os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
os.write(LINE_SEPARATOR);
if (CoderResult.UNDERFLOW == r) {
break;
}
os.write(' ');
bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
}
}
use of java.nio.charset.CoderResult in project voltdb by VoltDB.
the class ByteBufUtil method encodeString0.
static ByteBuf encodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset) {
final CharsetEncoder encoder = CharsetUtil.encoder(charset);
int length = (int) ((double) src.remaining() * encoder.maxBytesPerChar());
boolean release = true;
final ByteBuf dst;
if (enforceHeap) {
dst = alloc.heapBuffer(length);
} else {
dst = alloc.buffer(length);
}
try {
final ByteBuffer dstBuf = dst.internalNioBuffer(0, length);
final int pos = dstBuf.position();
CoderResult cr = encoder.encode(src, dstBuf, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = encoder.flush(dstBuf);
if (!cr.isUnderflow()) {
cr.throwException();
}
dst.writerIndex(dst.writerIndex() + dstBuf.position() - pos);
release = false;
return dst;
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
} finally {
if (release) {
dst.release();
}
}
}
use of java.nio.charset.CoderResult in project async-http-client by AsyncHttpClient.
the class Utf8ByteBufCharsetDecoder method handleSplitCharBuffer.
private void handleSplitCharBuffer(ByteBuffer nioBuffer, boolean endOfInput) throws CharacterCodingException {
// TODO we could save charSize
int missingBytes = charSize(splitCharBuffer.get(0)) - splitCharBuffer.position();
if (nioBuffer.remaining() < missingBytes) {
if (endOfInput) {
throw new CharacterCodingException();
}
// still not enough bytes
splitCharBuffer.put(nioBuffer);
} else {
// FIXME better way?
for (int i = 0; i < missingBytes; i++) {
splitCharBuffer.put(nioBuffer.get());
}
splitCharBuffer.flip();
CoderResult res = decoder.decode(splitCharBuffer, charBuffer, endOfInput && !nioBuffer.hasRemaining());
if (res.isError()) {
res.throwException();
}
splitCharBuffer.clear();
}
}
use of java.nio.charset.CoderResult in project logging-log4j2 by apache.
the class TextEncoderHelper method flushRemainingBytes.
private static ByteBuffer flushRemainingBytes(final CharsetEncoder charsetEncoder, final ByteBufferDestination destination, ByteBuffer temp) throws CharacterCodingException {
CoderResult result;
do {
// write any final bytes to the output buffer once the overall input sequence has been read
result = charsetEncoder.flush(temp);
temp = drainIfByteBufferFull(destination, temp, result);
} while (// byte buffer has been drained: retry
result.isOverflow());
if (!result.isUnderflow()) {
// we should have fully flushed the remaining bytes
result.throwException();
}
return temp;
}
use of java.nio.charset.CoderResult in project logging-log4j2 by apache.
the class TextEncoderHelper method encodeAsMuchAsPossible.
private static ByteBuffer encodeAsMuchAsPossible(final CharsetEncoder charsetEncoder, final CharBuffer charBuf, final boolean endOfInput, final ByteBufferDestination destination, ByteBuffer temp) throws CharacterCodingException {
CoderResult result;
do {
result = charsetEncoder.encode(charBuf, temp, endOfInput);
temp = drainIfByteBufferFull(destination, temp, result);
} while (// byte buffer has been drained: retry
result.isOverflow());
if (!result.isUnderflow()) {
// we should have fully read the char buffer contents
result.throwException();
}
return temp;
}
Aggregations