/** * Writes the specified byte to this output stream. The general contract for * write is that one byte is written to the output stream. The byte to be * written is the eight low-order bits of the argument b. The 24 high-order * bits of b are ignored. */ /* (non-Javadoc) * @see java.io.OutputStream#write(int) */ @Override public void write(int b) throws IOException { if (closed) { throw new IOException("Stream is closed"); } if (inputCursor >= inputBuffer.length) { compressInput(); } inputBuffer[inputCursor++] = (byte) b; }
@Override public void flush() throws IOException { if (closed) { throw new IOException("Stream is closed"); } compressInput(); dumpOutput(); out.flush(); }
@Override public void write(byte[] b, int byteOffset, int byteLength) throws IOException { if (closed) { throw new IOException("Stream is closed"); } int cursor = 0; while (cursor < byteLength) { int readLen = Math.min(byteLength - cursor, blockSize - inputCursor); // copy the input data to uncompressed buffer if (readLen > 0) { System.arraycopy(b, byteOffset + cursor, inputBuffer, inputCursor, readLen); inputCursor += readLen; } if (inputCursor < blockSize) { return; } compressInput(); cursor += readLen; } }
/** * Compress the raw byte array data. * * @param array array data of any type (e.g., byte[], float[], long[], ...) * @param byteOffset * @param byteLength * @throws IOException */ public void rawWrite(Object array, int byteOffset, int byteLength) throws IOException { if (closed) { throw new IOException("Stream is closed"); } int cursor = 0; while (cursor < byteLength) { int readLen = Math.min(byteLength - cursor, blockSize - inputCursor); // copy the input data to uncompressed buffer if (readLen > 0) { Snappy.arrayCopy(array, byteOffset + cursor, readLen, inputBuffer, inputCursor); inputCursor += readLen; } if (inputCursor < blockSize) { return; } compressInput(); cursor += readLen; } }