public boolean isDataAvailable() { try { return interruptableIn.available() > 0; } catch (final Exception e) { return false; } }
public void interrupt() { if (interruptableOut != null) { interruptableOut.interrupt(); } } }
public StandardCommsSession(final String hostname, final int port, final int timeoutMillis) throws IOException { socketChannel = SocketChannel.open(); socketChannel.socket().connect(new InetSocketAddress(hostname, port), timeoutMillis); socketChannel.configureBlocking(false); in = new SocketChannelInputStream(socketChannel); bufferedIn = new InterruptableInputStream(new BufferedInputStream(in)); out = new SocketChannelOutputStream(socketChannel); bufferedOut = new InterruptableOutputStream(new BufferedOutputStream(out)); this.hostname = hostname; this.port = port; }
public SocketChannelOutput(final SocketChannel socketChannel) throws IOException { socketOutStream = new SocketChannelOutputStream(socketChannel); countingOut = new ByteCountingOutputStream(socketOutStream); bufferedOut = new BufferedOutputStream(countingOut); interruptableOut = new InterruptableOutputStream(bufferedOut); }
public SocketChannelInput(final SocketChannel socketChannel) throws IOException { this.socketIn = new SocketChannelInputStream(socketChannel); countingIn = new ByteCountingInputStream(socketIn); bufferedIn = new BufferedInputStream(countingIn); interruptableIn = new InterruptableInputStream(bufferedIn); }
protected void readChunkHeader() throws IOException { // Ensure that we have a valid SYNC chunk fillBuffer(fourByteBuffer); if (!Arrays.equals(CompressionOutputStream.SYNC_BYTES, fourByteBuffer)) { throw new IOException("Invalid CompressionInputStream. Expected first 4 bytes to be 'SYNC' but were " + toHex(fourByteBuffer)); } // determine the size of the decompressed buffer fillBuffer(fourByteBuffer); buffer = new byte[toInt(fourByteBuffer)]; // determine the size of the compressed buffer fillBuffer(fourByteBuffer); compressedBuffer = new byte[toInt(fourByteBuffer)]; bufferIndex = buffer.length; // indicate that buffer is empty }
public void interrupt() { if (interruptableIn != null) { interruptableIn.interrupt(); } } }
@Override public void write(final int b) throws IOException { buffer[bufferIndex++] = (byte) (b & 0xFF); if (bufferFull()) { compressAndWrite(); } }
@Override public int read() throws IOException { if (eos) { return -1; } if (isBufferEmpty()) { bufferAndDecompress(); } if (isBufferEmpty()) { eos = true; return -1; } return buffer[bufferIndex++] & 0xFF; }
public void setInputStream(InputStream inputStream) { interruptableIn = new InterruptableInputStream(inputStream); this.countingIn = new ByteCountingInputStream(interruptableIn); }
public void setOutputStream(OutputStream outputStream) { interruptableOut = new InterruptableOutputStream(outputStream); this.countingOut = new ByteCountingOutputStream(interruptableOut); }
@Override public void flush() throws IOException { compressAndWrite(); super.flush(); }
@Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
@Override public void write(final byte[] b) throws IOException { write(b, 0, b.length); }
private void writeChunkHeader(final int compressedBytes) throws IOException { // If we have already written data, write out a '1' to indicate that we have more data; when we close // the stream, we instead write a '0' to indicate that we are finished sending data. if (dataWritten) { out.write(1); } out.write(SYNC_BYTES); dataWritten = true; writeInt(out, bufferIndex); writeInt(out, compressedBytes); }
public void interrupt() { interruptableOut.interrupt(); } }
public void interrupt() { interruptableIn.interrupt(); }
@Override public void write(final byte[] b, final int off, final int len) throws IOException { int bytesLeft = len; while (bytesLeft > 0) { final int free = buffer.length - bufferIndex; final int bytesThisIteration = Math.min(bytesLeft, free); System.arraycopy(b, off + len - bytesLeft, buffer, bufferIndex, bytesThisIteration); bufferIndex += bytesThisIteration; bytesLeft -= bytesThisIteration; if (bufferFull()) { compressAndWrite(); } } }
/** * Flushes remaining buffer and calls {@link Deflater#end()} to free acquired memory to prevent OutOfMemory error. * @throws IOException for any issues closing underlying stream */ @Override public void close() throws IOException { compressAndWrite(); out.write(0); // indicate that the stream is finished. out.flush(); deflater.end(); } }