use of java.nio.charset.CoderResult in project grails-core by grails.
the class BoundedCharsAsEncodedBytesCounter method update.
public void update(char[] buf, int off, int len) {
if (calculationActive && len > 0) {
try {
CharBuffer cb = CharBuffer.wrap(buf, off, len);
ce.reset();
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow()) {
terminateCalculation();
return;
}
cr = ce.flush(bb);
if (!cr.isUnderflow()) {
terminateCalculation();
return;
}
} catch (BufferOverflowException e) {
terminateCalculation();
} catch (Exception x) {
terminateCalculation();
}
}
}
use of java.nio.charset.CoderResult in project j2objc by google.
the class CharsetDecoderTest method test_ByteArray_decode_with_offset.
// http://code.google.com/p/android/issues/detail?id=4237
public void test_ByteArray_decode_with_offset() throws Exception {
CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
byte[] arr = encode("UTF-16", "Android");
arr = prependByteToByteArray(arr, new Integer(1).byteValue());
int offset = 1;
ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
decoder.reset();
CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
assertFalse(coderResult.toString(), coderResult.isError());
decoder.flush(outBuffer);
outBuffer.flip();
assertEquals("Android", outBuffer.toString().trim());
}
use of java.nio.charset.CoderResult in project j2objc by google.
the class InputStreamReader method read.
/**
* Reads up to {@code count} characters from this reader and stores them
* at position {@code offset} in the character array {@code buffer}. Returns
* the number of characters actually read or -1 if the end of the reader has
* been reached. The bytes are either obtained from converting bytes in this
* reader's buffer or by first filling the buffer from the source
* InputStream and then reading from the buffer.
*
* @throws IndexOutOfBoundsException
* if {@code offset < 0 || count < 0 || offset + count > buffer.length}.
* @throws IOException
* if this reader is closed or some other I/O error occurs.
*/
@Override
public int read(char[] buffer, int offset, int count) throws IOException {
synchronized (lock) {
if (!isOpen()) {
throw new IOException("InputStreamReader is closed");
}
Arrays.checkOffsetAndCount(buffer.length, offset, count);
if (count == 0) {
return 0;
}
CharBuffer out = CharBuffer.wrap(buffer, offset, count);
CoderResult result = CoderResult.UNDERFLOW;
// bytes.remaining() indicates number of bytes in buffer
// when 1-st time entered, it'll be equal to zero
boolean needInput = !bytes.hasRemaining();
while (out.hasRemaining()) {
// fill the buffer if needed
if (needInput) {
try {
if (in.available() == 0 && out.position() > offset) {
// we could return the result without blocking read
break;
}
} catch (IOException e) {
// available didn't work so just try the read
}
int desiredByteCount = bytes.capacity() - bytes.limit();
int off = bytes.arrayOffset() + bytes.limit();
int actualByteCount = in.read(bytes.array(), off, desiredByteCount);
if (actualByteCount == -1) {
endOfInput = true;
break;
} else if (actualByteCount == 0) {
break;
}
bytes.limit(bytes.limit() + actualByteCount);
needInput = false;
}
// decode bytes
result = decoder.decode(bytes, out, false);
if (result.isUnderflow()) {
// compact the buffer if no space left
if (bytes.limit() == bytes.capacity()) {
bytes.compact();
bytes.limit(bytes.position());
bytes.position(0);
}
needInput = true;
} else {
break;
}
}
if (result == CoderResult.UNDERFLOW && endOfInput) {
result = decoder.decode(bytes, out, true);
if (result == CoderResult.UNDERFLOW) {
result = decoder.flush(out);
}
decoder.reset();
}
if (result.isMalformed() || result.isUnmappable()) {
result.throwException();
}
return out.position() - offset == 0 ? -1 : out.position() - offset;
}
}
use of java.nio.charset.CoderResult in project grails-core by grails.
the class StreamByteBuffer method readAsString.
public String readAsString(Charset charset) throws CharacterCodingException {
int unreadSize = totalBytesUnread();
if (unreadSize > 0) {
CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
CharBuffer charbuffer = CharBuffer.allocate(unreadSize);
ByteBuffer buf = null;
while (prepareRead() != -1) {
buf = currentReadChunk.readToNioBuffer();
boolean endOfInput = (prepareRead() == -1);
CoderResult result = decoder.decode(buf, charbuffer, endOfInput);
if (endOfInput) {
if (!result.isUnderflow()) {
result.throwException();
}
}
}
CoderResult result = decoder.flush(charbuffer);
if (buf.hasRemaining()) {
throw new IllegalStateException("There's a bug here, buffer wasn't read fully.");
}
if (!result.isUnderflow()) {
result.throwException();
}
charbuffer.flip();
String str;
if (charbuffer.hasArray()) {
int len = charbuffer.remaining();
char[] ch = charbuffer.array();
if (len != ch.length) {
ch = (char[]) GrailsArrayUtils.subarray(ch, 0, len);
}
str = StringCharArrayAccessor.createString(ch);
} else {
str = charbuffer.toString();
}
return str;
}
return null;
}
use of java.nio.charset.CoderResult in project XobotOS by xamarin.
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;
}
}
Aggregations