public int available() throws IOException { lock.lock(); try { return size(); } finally { lock.unlock(); } }
/** * Moves the bytes between [read_pos and write_pos] read_pos bytes to the left, such that the new read_pos is 0 * and the write_pos is write_pos - read_pos. Lock must be held. */ @GuardedBy("lock") protected void compact() { if(read_pos == 0) return; if(size() == 0) { read_pos=write_pos=0; return; } int length=write_pos - read_pos; System.arraycopy(buf, read_pos, buf, 0, length); write_pos-=read_pos; read_pos=0; not_full.signalAll(); }
public int read(byte[] b, int off, int len) throws IOException { sanityCheck(b, off, len); int bytes_read=0; int bytes_to_be_read=Math.min(b.length, len); lock.lock(); try { while(bytes_read < bytes_to_be_read) { if(read_pos < write_pos) { int bytes_to_read=Math.min(len-bytes_read, size()); System.arraycopy(buf, read_pos, b, bytes_read + off, bytes_to_read); read_pos+=bytes_to_read; bytes_read+=bytes_to_read; not_full.signalAll(); continue; } if(closed) return bytes_read > 0? bytes_read : -1; try { not_empty.await(); } catch(InterruptedException e) { } } return bytes_read > 0? bytes_read : -1; } finally { lock.unlock(); } }
public int available() throws IOException { lock.lock(); try { return size(); } finally { lock.unlock(); } }
/** * Moves the bytes between [read_pos and write_pos] read_pos bytes to the left, such that the new read_pos is 0 * and the write_pos is write_pos - read_pos. Lock must be held. */ @GuardedBy("lock") protected void compact() { if(read_pos == 0) return; if(size() == 0) { read_pos=write_pos=0; return; } int length=write_pos - read_pos; System.arraycopy(buf, read_pos, buf, 0, length); write_pos-=read_pos; read_pos=0; not_full.signalAll(); }
public int read(byte[] b, int off, int len) throws IOException { sanityCheck(b, off, len); int bytes_read=0; int bytes_to_be_read=Math.min(b.length, len); lock.lock(); try { while(bytes_read < bytes_to_be_read) { if(read_pos < write_pos) { int bytes_to_read=Math.min(len-bytes_read, size()); System.arraycopy(buf, read_pos, b, bytes_read + off, bytes_to_read); read_pos+=bytes_to_read; bytes_read+=bytes_to_read; not_full.signalAll(); continue; } if(closed) return bytes_read > 0? bytes_read : -1; try { not_empty.await(); } catch(InterruptedException e) { } } return bytes_read > 0? bytes_read : -1; } finally { lock.unlock(); } }