@Override public int read(byte[] buffer, int offset, int length) throws IOException { return read(buffer, offset, length, false); }
@Override public int read() throws IOException { if (!readyBuffer()) { return -1; } return uncompressed[position++] & 255; }
/** * Fill the uncompressed bytes buffer by reading the underlying inputStream. */ protected boolean readyBuffer() throws IOException { if (position < valid) { return true; } if (closed) { return false; } valid = uncompress(in, uncompressed); if (valid < 0) { return false; } position = 0; return (position < valid); }
@Override public void close() throws IOException { position = valid = 0; if (!closed) { closed = true; doClose(); in.close(); } }
public CompressedStreamInput(StreamInput in) throws IOException { this.in = in; super.setVersion(in.getVersion()); readHeader(in); }
@Override public byte readByte() throws IOException { if (!readyBuffer()) { throw new EOFException(); } return uncompressed[position++]; }
@Override public void readBytes(byte[] b, int offset, int len) throws IOException { int result = read(b, offset, len, true /* we want to have full reads, thats the contract... */); if (result < len) { throw new EOFException(); } }
public int read(byte[] buffer, int offset, int length, boolean fullRead) throws IOException { if (length < 1) { return 0; } if (!readyBuffer()) { return -1; } // First let's read however much data we happen to have... int chunkLength = Math.min(valid - position, length); System.arraycopy(uncompressed, position, buffer, offset, chunkLength); position += chunkLength; if (chunkLength == length || !fullRead) { return chunkLength; } // Need more data, then int totalRead = chunkLength; do { offset += chunkLength; if (!readyBuffer()) { break; } chunkLength = Math.min(valid - position, (length - totalRead)); System.arraycopy(uncompressed, position, buffer, offset, chunkLength); position += chunkLength; totalRead += chunkLength; } while (totalRead < length); return totalRead; }