protected Tuple<InputStream,Object> createStreamToProvider(final Address provider, final StateHeader hdr) { Util.close(input_stream); input_stream=new BlockingInputStream(buffer_size); return new Tuple<>(input_stream, null); }
sanityCheck(buf, offset, length); while(bytes_written < length && !closed) { if(read_pos > 0 && length - bytes_written > remaining()) compact(); if(remaining() > 0) { int bytes_to_write=Math.min(length-bytes_written, remaining()); System.arraycopy(buf, offset+bytes_written, this.buf, write_pos, bytes_to_write); write_pos+=bytes_to_write;
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(); } }
/** * Appends bytes to the end of the stream * @param buf * @throws IOException * @see #write(byte[],int,int) */ public void write(byte[] buf) throws IOException { if(buf != null) write(buf, 0, buf.length); }
public int available() throws IOException { lock.lock(); try { return size(); } finally { lock.unlock(); } }
public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
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(); } }
protected void handleStateChunk(Address sender, byte[] buffer, int offset, int length) { if(buffer == null || input_stream == null) return; try { if(log.isTraceEnabled()) log.trace("%s: received chunk of %s from %s",local_addr,Util.printBytes(length),sender); input_stream.write(buffer, offset, length); } catch(IOException e) { handleException(e); } }
/** * 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) throws IOException { return read(b, 0, b.length); }
sanityCheck(buf, offset, length); while(bytes_written < length && !closed) { if(read_pos > 0 && length - bytes_written > remaining()) compact(); if(remaining() > 0) { int bytes_to_write=Math.min(length-bytes_written, remaining()); System.arraycopy(buf, offset+bytes_written, this.buf, write_pos, bytes_to_write); write_pos+=bytes_to_write;
/** * Appends bytes to the end of the stream * @param buf * @throws IOException * @see #write(byte[],int,int) */ public void write(byte[] buf) throws IOException { if(buf != null) write(buf, 0, buf.length); }
public int available() throws IOException { lock.lock(); try { return size(); } finally { lock.unlock(); } }
protected Tuple<InputStream,Object> createStreamToProvider(final Address provider, final StateHeader hdr) { Util.close(input_stream); input_stream=new BlockingInputStream(buffer_size); return new Tuple<>(input_stream, null); }
protected void handleStateChunk(Address sender, byte[] buffer, int offset, int length) { if(buffer == null || input_stream == null) return; try { if(log.isTraceEnabled()) log.trace("%s: received chunk of %s from %s",local_addr,Util.printBytes(length),sender); input_stream.write(buffer, offset, length); } catch(IOException e) { handleException(e); } }
/** * 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(); }