use of java.nio.charset.CoderResult in project XobotOS by xamarin.
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(Charsets.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 android_frameworks_base by DirtyUnicorns.
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 android_frameworks_base by DirtyUnicorns.
the class LoggingPrintStream method write.
@Override
public synchronized void write(byte[] bytes, int start, int count) {
if (decoder == null) {
encodedBytes = ByteBuffer.allocate(80);
decodedChars = CharBuffer.allocate(80);
decoder = Charset.defaultCharset().newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
}
int end = start + count;
while (start < end) {
// copy some bytes from the array to the long-lived buffer. This
// way, if we end with a partial character we don't lose it.
int numBytes = Math.min(encodedBytes.remaining(), end - start);
encodedBytes.put(bytes, start, numBytes);
start += numBytes;
encodedBytes.flip();
CoderResult coderResult;
do {
// decode bytes from the byte buffer into the char buffer
coderResult = decoder.decode(encodedBytes, decodedChars, false);
// copy chars from the char buffer into our string builder
decodedChars.flip();
builder.append(decodedChars);
decodedChars.clear();
} while (coderResult.isOverflow());
encodedBytes.compact();
}
flush(false);
}
use of java.nio.charset.CoderResult in project android_frameworks_base by AOSPA.
the class LoggingPrintStream method write.
@Override
public synchronized void write(byte[] bytes, int start, int count) {
if (decoder == null) {
encodedBytes = ByteBuffer.allocate(80);
decodedChars = CharBuffer.allocate(80);
decoder = Charset.defaultCharset().newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
}
int end = start + count;
while (start < end) {
// copy some bytes from the array to the long-lived buffer. This
// way, if we end with a partial character we don't lose it.
int numBytes = Math.min(encodedBytes.remaining(), end - start);
encodedBytes.put(bytes, start, numBytes);
start += numBytes;
encodedBytes.flip();
CoderResult coderResult;
do {
// decode bytes from the byte buffer into the char buffer
coderResult = decoder.decode(encodedBytes, decodedChars, false);
// copy chars from the char buffer into our string builder
decodedChars.flip();
builder.append(decodedChars);
decodedChars.clear();
} while (coderResult.isOverflow());
encodedBytes.compact();
}
flush(false);
}
use of java.nio.charset.CoderResult in project android_frameworks_base by ResurrectionRemix.
the class FastPrintWriter method flushLocked.
private void flushLocked() 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 (!mIoError) {
if (result.isError()) {
throw new IOException(result.toString());
} else if (result.isOverflow()) {
flushBytesLocked();
result = mCharset.encode(charBuffer, mBytes, true);
continue;
}
break;
}
if (!mIoError) {
flushBytesLocked();
mOutputStream.flush();
}
} else if (mWriter != null) {
if (!mIoError) {
mWriter.write(mText, 0, mPos);
mWriter.flush();
}
} else {
int nonEolOff = 0;
final int sepLen = mSeparator.length();
final int len = sepLen < mPos ? sepLen : mPos;
while (nonEolOff < len && mText[mPos - 1 - nonEolOff] == mSeparator.charAt(mSeparator.length() - 1 - nonEolOff)) {
nonEolOff++;
}
if (nonEolOff >= mPos) {
mPrinter.println("");
} else {
mPrinter.println(new String(mText, 0, mPos - nonEolOff));
}
}
mPos = 0;
}
}
Aggregations