@Test public void testAvailable() throws Exception { final int BUFFER_SIZE = 769; final ByteBufferInputStream bbis = new ByteBufferInputStream(); ByteBuffer data = ByteBuffer.allocate(BUFFER_SIZE); data.clear(); for (int j = 0; j < data.capacity(); j++) { data.put((byte) 'A'); } data.flip(); bbis.put(data); assertEquals("Available bytes", BUFFER_SIZE, bbis.available()); data = ByteBuffer.allocate(BUFFER_SIZE); data.clear(); for (int j = 0; j < data.capacity(); j++) { data.put((byte) 'B'); } data.flip(); bbis.put(data); assertEquals("Available bytes", 2 * BUFFER_SIZE, bbis.available()); int c = bbis.read(); assertEquals("Byte read", 'A', c); assertEquals("Available bytes", 2 * BUFFER_SIZE - 1, bbis.available()); byte[] buff = new byte[199]; int l = bbis.read(buff); assertEquals("Number of bytes read", buff.length, l); assertEquals("Available bytes", 2 * BUFFER_SIZE - 200, bbis.available()); buff = new byte[1000]; l = bbis.read(buff); assertEquals("Number of bytes read", buff.length, l); assertEquals("Available bytes", 2 * BUFFER_SIZE - 1200, bbis.available()); bbis.closeQueue(); l = bbis.read(buff); assertEquals("Number of bytes read", 2 * BUFFER_SIZE - 1200, l); assertEquals("Available bytes", 0, bbis.available()); bbis.close(); }
@Override public int available() throws IOException { if (eof || closed.get()) { checkThrowable(); return 0; } int available = 0; if (current != null && current.hasRemaining()) { available = current.remaining(); } for (final ByteBuffer buffer : buffers) { if (buffer == EOF) { break; } available += buffer.remaining(); } checkThrowable(); return closed.get() ? 0 : available; }
ByteBufferInputStream extends NonBlockingInputStream { @Override public int available() throws IOException { if (eof || closed.get()) { checkThrowable(); return 0; } int available = 0; if (current != null && current.hasRemaining()) { available = current.remaining(); } for (final ByteBuffer buffer : buffers) { if (buffer == EOF) { break; } available += buffer.remaining(); } checkThrowable(); return closed.get() ? 0 : available; } }
ByteBufferInputStream extends NonBlockingInputStream { @Override public int available() throws IOException { if (eof || closed.get()) { checkThrowable(); return 0; } int available = 0; if (current != null && current.hasRemaining()) { available = current.remaining(); } for (final ByteBuffer buffer : buffers) { if (buffer == EOF) { break; } available += buffer.remaining(); } checkThrowable(); return closed.get() ? 0 : available; } ByteBufferInputStream(); }
ByteBufferInputStream extends NonBlockingInputStream { @Override public int available() throws IOException { if (eof || closed.get()) { checkThrowable(); return 0; } int available = 0; if (current != null && current.hasRemaining()) { available = current.remaining(); } for (final ByteBuffer buffer : buffers) { if (buffer == EOF) { break; } available += buffer.remaining(); } checkThrowable(); return closed.get() ? 0 : available; } ByteBufferInputStream(); @Override int available(); @Override int read(); @Override int read(byte[] b, int off, int len); int tryRead(); @Override int tryRead(final byte[] b); @Override int tryRead(final byte[] b, final int off, final int len); @Override void close(); boolean put(final ByteBuffer src); void closeQueue(); void closeQueue(final Throwable throwable); }
ByteBufferInputStream extends NonBlockingInputStream { @Override public int available() throws IOException { if (eof || closed.get()) { checkThrowable(); return 0; } int available = 0; if (current != null && current.hasRemaining()) { available = current.remaining(); } for (final ByteBuffer buffer : buffers) { if (buffer == EOF) { break; } available += buffer.remaining(); } checkThrowable(); return closed.get() ? 0 : available; } ByteBufferInputStream(); @Override int available(); @Override int read(); @Override int read(byte[] b, int off, int len); int tryRead(); @Override int tryRead(final byte[] b); @Override int tryRead(final byte[] b, final int off, final int len); @Override void close(); boolean put(final ByteBuffer src); void closeQueue(); void closeQueue(final Throwable throwable);  }