/** Reads all data from the underlying {@link InputStream}, while notifying the * observers. * @throws IOException The underlying {@link InputStream}, or either of the * observers has thrown an exception. */ public void consume() throws IOException { final byte[] buffer = new byte[8192]; for (;;) { final int res = read(buffer); if (res == -1) { return; } } }
/** Notifies the observers by invoking {@link Observer#finished()}. * @throws IOException Some observer has thrown an exception, which is being * passed down. */ protected void noteClosed() throws IOException { for (final Observer observer : getObservers()) { observer.closed(); } }
@Override public void close() throws IOException { IOException ioe = null; try { super.close(); } catch (final IOException e) { ioe = e; } if (ioe == null) { noteClosed(); } else { noteError(ioe); } }
@Override public int read(final byte[] pBuffer) throws IOException { int result = 0; IOException ioe = null; try { result = super.read(pBuffer); } catch (final IOException pException) { ioe = pException; } if (ioe != null) { noteError(ioe); } else if (result == -1) { noteFinished(); } else if (result > 0) { noteDataBytes(pBuffer, 0, result); } return result; }
@Override public int read() throws IOException { int result = 0; IOException ioe = null; try { result = super.read(); } catch (final IOException pException) { ioe = pException; } if (ioe != null) { noteError(ioe); } else if (result == -1) { noteFinished(); } else { noteDataByte(result); } return result; }
final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(4096); final ByteArrayInputStream bais = new ByteArrayInputStream(buffer); final ObservableInputStream ois = new ObservableInputStream(bais); final LastBytesKeepingObserver lko = new LastBytesKeepingObserver(); final byte[] readBuffer = new byte[23]; assertEquals(null, lko.buffer); ois.read(readBuffer); assertEquals(null, lko.buffer); ois.add(lko); for (;;) { if (bais.available() >= 2048) { final int result = ois.read(readBuffer); if (result == -1) { ois.close(); break; } else { final int result = ois.read(readBuffer, 1, 11); if (result == -1) { ois.close(); break; } else {
@Override public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { int result = 0; IOException ioe = null; try { result = super.read(pBuffer, pOffset, pLength); } catch (final IOException pException) { ioe = pException; } if (ioe != null) { noteError(ioe); } else if (result == -1) { noteFinished(); } else if (result > 0) { noteDataBytes(pBuffer, pOffset, result); } return result; }
@Override public int read() throws IOException { int result = 0; IOException ioe = null; try { result = super.read(); } catch (final IOException pException) { ioe = pException; } if (ioe != null) { noteError(ioe); } else if (result == -1) { noteFinished(); } else { noteDataByte(result); } return result; }
/** Tests, that {@link Observer#data(int)} is called. */ @Test public void testDataByteCalled() throws Exception { final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(4096); final ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer)); final LastByteKeepingObserver lko = new LastByteKeepingObserver(); assertEquals(-1, lko.lastByteSeen); ois.read(); assertEquals(-1, lko.lastByteSeen); assertFalse(lko.finished); assertFalse(lko.closed); ois.add(lko); for (int i = 1; i < buffer.length; i++) { final int result = ois.read(); assertEquals((byte) result, buffer[i]); assertEquals(result, lko.lastByteSeen); assertFalse(lko.finished); assertFalse(lko.closed); } final int result = ois.read(); assertEquals(-1, result); assertTrue(lko.finished); assertFalse(lko.closed); ois.close(); assertTrue(lko.finished); assertTrue(lko.closed); }
@Override public int read(final byte[] pBuffer) throws IOException { int result = 0; IOException ioe = null; try { result = super.read(pBuffer); } catch (final IOException pException) { ioe = pException; } if (ioe != null) { noteError(ioe); } else if (result == -1) { noteFinished(); } else if (result > 0) { noteDataBytes(pBuffer, 0, result); } return result; }
/** Notifies the observers by invoking {@link Observer#finished()}. * @throws IOException Some observer has thrown an exception, which is being * passed down. */ protected void noteFinished() throws IOException { for (final Observer observer : getObservers()) { observer.finished(); } }
@Override public void close() throws IOException { IOException ioe = null; try { super.close(); } catch (final IOException e) { ioe = e; } if (ioe == null) { noteClosed(); } else { noteError(ioe); } }
/** Reads all data from the underlying {@link InputStream}, while notifying the * observers. * @throws IOException The underlying {@link InputStream}, or either of the * observers has thrown an exception. */ public void consume() throws IOException { final byte[] buffer = new byte[8192]; for (;;) { final int res = read(buffer); if (res == -1) { return; } } }
@Override public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { int result = 0; IOException ioe = null; try { result = super.read(pBuffer, pOffset, pLength); } catch (final IOException pException) { ioe = pException; } if (ioe != null) { noteError(ioe); } else if (result == -1) { noteFinished(); } else if (result > 0) { noteDataBytes(pBuffer, pOffset, result); } return result; }
/** Notifies the observers by invoking {@link Observer#error(IOException)} * with the given argument. * @param pException Passed to the observers. * @throws IOException Some observer has thrown an exception, which is being * passed down. This may be the same exception, which has been passed as an * argument. */ protected void noteError(final IOException pException) throws IOException { for (final Observer observer : getObservers()) { observer.error(pException); } }
/** Notifies the observers by invoking {@link Observer#data(int)} * with the given arguments. * @param pDataByte Passed to the observers. * @throws IOException Some observer has thrown an exception, which is being * passed down. */ protected void noteDataByte(final int pDataByte) throws IOException { for (final Observer observer : getObservers()) { observer.data(pDataByte); } }
/** Notifies the observers by invoking {@link Observer#data(byte[],int,int)} * with the given arguments. * @param pBuffer Passed to the observers. * @param pOffset Passed to the observers. * @param pLength Passed to the observers. * @throws IOException Some observer has thrown an exception, which is being * passed down. */ protected void noteDataBytes(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { for (final Observer observer : getObservers()) { observer.data(pBuffer, pOffset, pLength); } }
/** Notifies the observers by invoking {@link Observer#finished()}. * @throws IOException Some observer has thrown an exception, which is being * passed down. */ protected void noteFinished() throws IOException { for (final Observer observer : getObservers()) { observer.finished(); } }
/** Notifies the observers by invoking {@link Observer#finished()}. * @throws IOException Some observer has thrown an exception, which is being * passed down. */ protected void noteClosed() throws IOException { for (final Observer observer : getObservers()) { observer.closed(); } }
/** Notifies the observers by invoking {@link Observer#data(int)} * with the given arguments. * @param pDataByte Passed to the observers. * @throws IOException Some observer has thrown an exception, which is being * passed down. */ protected void noteDataByte(final int pDataByte) throws IOException { for (final Observer observer : getObservers()) { observer.data(pDataByte); } }