use of java.nio.charset.CoderResult in project ADWLauncher2 by boombuler.
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 gradle by gradle.
the class StreamByteBuffer method readAsCharBuffer.
private CharBuffer readAsCharBuffer(Charset charset) throws CharacterCodingException {
CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
CharBuffer charbuffer = CharBuffer.allocate(totalBytesUnread());
ByteBuffer buf = null;
boolean wasUnderflow = false;
ByteBuffer nextBuf = null;
boolean needsFlush = false;
while (hasRemaining(nextBuf) || hasRemaining(buf) || prepareRead() != -1) {
if (hasRemaining(buf)) {
// handle decoding underflow, multi-byte unicode character at buffer chunk boundary
if (!wasUnderflow) {
throw new IllegalStateException("Unexpected state. Buffer has remaining bytes without underflow in decoding.");
}
if (!hasRemaining(nextBuf) && prepareRead() != -1) {
nextBuf = currentReadChunk.readToNioBuffer();
}
// copy one by one until the underflow has been resolved
buf = ByteBuffer.allocate(buf.remaining() + 1).put(buf);
buf.put(nextBuf.get());
buf.flip();
} else {
if (hasRemaining(nextBuf)) {
buf = nextBuf;
} else if (prepareRead() != -1) {
buf = currentReadChunk.readToNioBuffer();
if (!hasRemaining(buf)) {
throw new IllegalStateException("Unexpected state. Buffer is empty.");
}
}
nextBuf = null;
}
boolean endOfInput = !hasRemaining(nextBuf) && prepareRead() == -1;
int bufRemainingBefore = buf.remaining();
CoderResult result = decoder.decode(buf, charbuffer, false);
if (bufRemainingBefore > buf.remaining()) {
needsFlush = true;
}
if (endOfInput) {
result = decoder.decode(ByteBuffer.allocate(0), charbuffer, true);
if (!result.isUnderflow()) {
result.throwException();
}
break;
}
wasUnderflow = result.isUnderflow();
}
if (needsFlush) {
CoderResult result = decoder.flush(charbuffer);
if (!result.isUnderflow()) {
result.throwException();
}
}
clear();
// push back remaining bytes of multi-byte unicode character
while (hasRemaining(buf)) {
byte b = buf.get();
try {
getOutputStream().write(b);
} catch (IOException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
}
charbuffer.flip();
return charbuffer;
}
use of java.nio.charset.CoderResult in project j2objc by google.
the class OutputStreamWriter method drainEncoder.
private void drainEncoder() throws IOException {
// Strictly speaking, I think it's part of the CharsetEncoder contract that you call
// encode with endOfInput true before flushing. Our ICU-based implementations don't
// actually need this, and you'd hope that any reasonable implementation wouldn't either.
// CharsetEncoder.encode doesn't actually pass the boolean through to encodeLoop anyway!
CharBuffer chars = CharBuffer.allocate(0);
while (true) {
CoderResult result = encoder.encode(chars, bytes, true);
if (result.isError()) {
result.throwException();
} else if (result.isOverflow()) {
flushBytes(false);
continue;
}
break;
}
// Some encoders (such as ISO-2022-JP) have stuff to write out after all the
// characters (such as shifting back into a default state). In our implementation,
// this is actually the first time ICU is told that we've run out of input.
CoderResult result = encoder.flush(bytes);
while (!result.isUnderflow()) {
if (result.isOverflow()) {
flushBytes(false);
result = encoder.flush(bytes);
} else {
result.throwException();
}
}
}
use of java.nio.charset.CoderResult in project j2objc by google.
the class Manifest method writeEntry.
private static void writeEntry(OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
String nameString = name.getName();
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 j2objc by google.
the class CharsetEncoderTest method testSurrogatePairAllAtOnce.
public void testSurrogatePairAllAtOnce() throws Exception {
// okay: surrogate pair seen all at once is decoded to U+20b9f.
Charset cs = Charset.forName("UTF-32BE");
CharsetEncoder e = cs.newEncoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '�', '�' }), bb, false);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(4, bb.position());
assertEquals((byte) 0x00, bb.get(0));
assertEquals((byte) 0x02, bb.get(1));
assertEquals((byte) 0x0b, bb.get(2));
assertEquals((byte) 0x9f, bb.get(3));
}
Aggregations