/// <exception cref="System.IO.IOException"></exception> private void drainEncoder() { // 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! java.nio.CharBuffer chars = java.nio.CharBuffer.allocate(0); while (true) { java.nio.charset.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. java.nio.charset.CoderResult result_1 = encoder.flush(bytes); while (!result_1.isUnderflow()) { if (result_1.isOverflow()) { flushBytes(false); result_1 = encoder.flush(bytes); } else { result_1.throwException(); } } }
/// <exception cref="System.IO.IOException"></exception> private void convert(java.nio.CharBuffer chars) { while (true) { java.nio.charset.CoderResult result = encoder.encode(chars, bytes, false); if (result.isOverflow()) { // Make room and try again. flushBytes(false); continue; } else { if (result.isError()) { result.throwException(); } } break; } }
public override int read(char[] buffer, int offset, int length) { lock (@lock) { if (!isOpen()) { throw new System.IO.IOException("InputStreamReader is closed"); } java.util.Arrays.checkOffsetAndCount(buffer.Length, offset, length); if (length == 0) { return(0); } java.nio.CharBuffer @out = java.nio.CharBuffer.wrap(buffer, offset, length); java.nio.charset.CoderResult result = java.nio.charset.CoderResult.UNDERFLOW; // bytes.remaining() indicates number of bytes in buffer // when 1-st time entered, it'll be equal to zero bool 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 (System.IO.IOException) { } // 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(((byte[])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 == java.nio.charset.CoderResult.UNDERFLOW && endOfInput) { result = decoder.decode(bytes, @out, true); decoder.flush(@out); decoder.reset(); } if (result.isMalformed() || result.isUnmappable()) { result.throwException(); } return(@out.position() - offset == 0 ? -1 : @out.position() - offset); } }