/** * Gets data from the ring buffer. * * <p> * Convenience method for: <code>read(buf, 0, buf.length)</code> */ public int get(byte[] buf) { return get(buf, 0, buf.length); }
@Override public int read(byte[] bytes, int offset, int length) throws IOException { lock.lock(); try { // are there any bytes available? while (buffer.getUsed() <= 0) { // was the output closed? if (outputClosed.get()) { // return EOF return -1; } //log.debug("waiting for write so up to {} bytes can be read", length); writeSignal.await(); } int getLength = Math.min(buffer.getUsed(), length); //log.debug("trying to get {} bytes ({} available)", getLength, buffer.getUsed()); buffer.get(bytes, offset, getLength); //log.debug("get {} bytes into buffer ({} available)", getLength, buffer.getUsed()); // signal data read readSignal.signal(); return getLength; } catch (InterruptedException e) { throw new IOException("Interrupted while waiting for signal", e); } finally { lock.unlock(); } }
@Override public int read(byte[] bytes, int offset, int length) throws IOException { lock.lock(); try { // are there any bytes available? while (buffer.getUsed() <= 0) { // was the output closed? if (outputClosed.get()) { // return EOF return -1; } //log.debug("waiting for write so up to {} bytes can be read", length); writeSignal.await(); } int getLength = Math.min(buffer.getUsed(), length); //log.debug("trying to get {} bytes ({} available)", getLength, buffer.getUsed()); buffer.get(bytes, offset, getLength); //log.debug("get {} bytes into buffer ({} available)", getLength, buffer.getUsed()); // signal data read readSignal.signal(); return getLength; } catch (InterruptedException e) { throw new IOException("Interrupted while waiting for signal", e); } finally { lock.unlock(); } }
/** * Gets data from the ring buffer. * * <p> * Convenience method for: <code>read(buf, 0, buf.length)</code> */ public int get(byte[] buf) { return get(buf, 0, buf.length); }
/** * Resizes the ring buffer by preserving it's data. * * <p> * If the new size is not enough to keep all used data in the ring buffer, * the excess old data is discarded. */ public void resize(int newSize) { if (newSize <= 0) { throw new IllegalArgumentException(); } if (newSize < rBufUsed) { // if new buffer is too small to contain all data discard(rBufUsed - newSize); } // discard oldest data byte[] newBuf = new byte[newSize]; int newBufUsed = get(newBuf, 0, newSize); // transfer data to new buffer rBuf = newBuf; rBufSize = newSize; rBufPos = 0; rBufUsed = newBufUsed; }
/** * Resizes the ring buffer by preserving it's data. * * <p> * If the new size is not enough to keep all used data in the ring buffer, * the excess old data is discarded. */ public void resize(int newSize) { if (newSize <= 0) { throw new IllegalArgumentException(); } if (newSize < rBufUsed) { // if new buffer is too small to contain all data discard(rBufUsed - newSize); } // discard oldest data byte[] newBuf = new byte[newSize]; int newBufUsed = get(newBuf, 0, newSize); // transfer data to new buffer rBuf = newBuf; rBufSize = newSize; rBufPos = 0; rBufUsed = newBufUsed; }