@Override public int read(final byte[] b, final InputStream inputStream) throws IOException { if (b == null) { return 0; } return read(b, 0, b.length, inputStream); }
private int lineFromReadBuffer(final CharArrayBuffer charbuffer, final int position) throws IOException { int pos = position; final int off = this.bufferPos; int len; this.bufferPos = pos + 1; if (pos > off && this.buffer[pos - 1] == Chars.CR) { // skip CR if found pos--; } len = pos - off; if (this.decoder == null) { charbuffer.append(this.buffer, off, len); } else { final ByteBuffer bbuf = ByteBuffer.wrap(this.buffer, off, len); len = appendDecoded(charbuffer, bbuf); } return len; }
@Override public int read(final InputStream inputStream) throws IOException { Args.notNull(inputStream, "Input stream"); int readLen; while (!hasBufferedData()) { readLen = fillBuffer(inputStream); if (readLen == -1) { return -1; } } return this.buffer[this.bufferPos++] & 0xff; }
@Test public void testBasics() throws IOException { final String s = "1234567890123456"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 10L); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final byte[] buffer = new byte[50]; int len = in.read(buffer, 0, 2); outputStream.write(buffer, 0, len); len = in.read(buffer); outputStream.write(buffer, 0, len); final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1); Assert.assertEquals(result, "1234567890"); in.close(); }
return lineFromReadBuffer(charBuffer, pos); } else { if (hasBufferedData()) { final int len = this.bufferLen - this.bufferPos; this.lineBuffer.append(this.buffer, this.bufferPos, len); this.bufferPos = this.bufferLen; readLen = fillBuffer(inputStream); if (readLen == -1) { retry = false; return lineFromLineBuffer(charBuffer);
private int fillInputBuffer(final int timeout) throws IOException { final SocketHolder socketHolder = ensureOpen(); final Socket socket = socketHolder.getSocket(); final int oldtimeout = socket.getSoTimeout(); try { socket.setSoTimeout(timeout); return this.inBuffer.fillBuffer(socketHolder.getInputStream()); } finally { socket.setSoTimeout(oldtimeout); } }
protected boolean awaitInput(final int timeout) throws IOException { if (this.inBuffer.hasBufferedData()) { return true; } fillInputBuffer(timeout); return this.inBuffer.hasBufferedData(); }
private int appendDecoded( final CharArrayBuffer charbuffer, final ByteBuffer bbuf) throws IOException { if (!bbuf.hasRemaining()) { return 0; } if (this.cbuf == null) { this.cbuf = CharBuffer.allocate(1024); } this.decoder.reset(); int len = 0; while (bbuf.hasRemaining()) { final CoderResult result = this.decoder.decode(bbuf, this.cbuf, true); len += handleDecodingResult(result, charbuffer); } final CoderResult result = this.decoder.flush(this.cbuf); len += handleDecodingResult(result, charbuffer); this.cbuf.clear(); return len; }
@Override public void close() throws IOException { final SocketHolder socketHolder = this.socketHolderRef.getAndSet(null); if (socketHolder != null) { try (final Socket socket = socketHolder.getSocket()) { this.inBuffer.clear(); this.outbuffer.flush(socketHolder.getOutputStream()); try { try { socket.shutdownOutput(); } catch (final IOException ignore) { } try { socket.shutdownInput(); } catch (final IOException ignore) { } } catch (final UnsupportedOperationException ignore) { // if one isn't supported, the other one isn't either } } } }
@Test public void testAvailable() throws IOException { final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {1, 2, 3}); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 3L); Assert.assertEquals(0, in.available()); in.read(); Assert.assertEquals(2, in.available()); in.close(); }
return lineFromReadBuffer(charBuffer, pos); } else { if (hasBufferedData()) { final int len = this.bufferLen - this.bufferPos; this.lineBuffer.append(this.buffer, this.bufferPos, len); this.bufferPos = this.bufferLen; readLen = fillBuffer(inputStream); if (readLen == -1) { retry = false; return lineFromLineBuffer(charBuffer);
@Override public int read(final InputStream inputStream) throws IOException { Args.notNull(inputStream, "Input stream"); int readLen; while (!hasBufferedData()) { readLen = fillBuffer(inputStream); if (readLen == -1) { return -1; } } return this.buffer[this.bufferPos++] & 0xff; }
private int fillInputBuffer(final int timeout) throws IOException { final SocketHolder socketHolder = ensureOpen(); final Socket socket = socketHolder.getSocket(); final int oldtimeout = socket.getSoTimeout(); try { socket.setSoTimeout(timeout); return this.inBuffer.fillBuffer(socketHolder.getInputStream()); } finally { socket.setSoTimeout(oldtimeout); } }
protected boolean awaitInput(final int timeout) throws IOException { if (this.inBuffer.hasBufferedData()) { return true; } fillInputBuffer(timeout); return this.inBuffer.hasBufferedData(); }
private int appendDecoded( final CharArrayBuffer charbuffer, final ByteBuffer bbuf) throws IOException { if (!bbuf.hasRemaining()) { return 0; } if (this.cbuf == null) { this.cbuf = CharBuffer.allocate(1024); } this.decoder.reset(); int len = 0; while (bbuf.hasRemaining()) { final CoderResult result = this.decoder.decode(bbuf, this.cbuf, true); len += handleDecodingResult(result, charbuffer); } final CoderResult result = this.decoder.flush(this.cbuf); len += handleDecodingResult(result, charbuffer); this.cbuf.clear(); return len; }
@Override public void close() throws IOException { final SocketHolder socketHolder = this.socketHolderRef.getAndSet(null); if (socketHolder != null) { try (final Socket socket = socketHolder.getSocket()) { this.inBuffer.clear(); this.outbuffer.flush(socketHolder.getOutputStream()); try { try { socket.shutdownOutput(); } catch (final IOException ignore) { } try { socket.shutdownInput(); } catch (final IOException ignore) { } } catch (final UnsupportedOperationException ignore) { // if one isn't supported, the other one isn't either } } } }
@Test public void testHugeChunk() throws IOException { final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream("1234567890abcdef\r\n01234567".getBytes( StandardCharsets.ISO_8859_1)); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); final ByteArrayOutputStream out = new ByteArrayOutputStream(); for (int i = 0; i < 8; ++i) { out.write(in.read()); } final String result = new String(out.toByteArray(), StandardCharsets.ISO_8859_1); Assert.assertEquals("01234567", result); }
return 0; if (hasBufferedData()) { final int chunk = Math.min(len, this.bufferLen - this.bufferPos); System.arraycopy(this.buffer, this.bufferPos, b, off, chunk); while (!hasBufferedData()) { final int readLen = fillBuffer(inputStream); if (readLen == -1) { return -1;
@Override public int read(final byte[] b, final InputStream inputStream) throws IOException { if (b == null) { return 0; } return read(b, 0, b.length, inputStream); }