Package org.iq80.snappy
Class AbstractSnappyOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.iq80.snappy.AbstractSnappyOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
- Direct Known Subclasses:
SnappyFramedOutputStream
,SnappyOutputStream
abstract class AbstractSnappyOutputStream extends java.io.OutputStream
This is a base class supporting both theSnappyOutputStream
andSnappyFramedOutputStream
.Delegates writing the header bytes and individual frames to the specific implementations. Implementations may also override the crc32 checksum calculation.
- Since:
- 0.4
-
-
Field Summary
Fields Modifier and Type Field Description private int
blockSize
private byte[]
buffer
private boolean
closed
private double
minCompressionRatio
private java.io.OutputStream
out
private byte[]
outputBuffer
private int
position
private BufferRecycler
recycler
-
Constructor Summary
Constructors Constructor Description AbstractSnappyOutputStream(java.io.OutputStream out, int blockSize, double minCompressionRatio)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected int
calculateCRC32C(byte[] data, int offset, int length)
Calculates a CRC32C checksum over the data.void
close()
private void
copyToBuffer(byte[] input, int offset, int length)
void
flush()
private void
flushBuffer()
Compresses and writes out any buffered data.void
write(byte[] input, int offset, int length)
void
write(int b)
protected abstract void
writeBlock(java.io.OutputStream out, byte[] data, int offset, int length, boolean compressed, int crc32c)
Write a frame (block) to out.private void
writeCompressed(byte[] input, int offset, int length)
Calculates
the crc, compresses the data, determines if the compression ratio is acceptable and callswriteBlock(OutputStream, byte[], int, int, boolean, int)
to actually write the frame.protected abstract void
writeHeader(java.io.OutputStream out)
Writes the implementation specific header or "marker bytes" to out.
-
-
-
Field Detail
-
recycler
private final BufferRecycler recycler
-
blockSize
private final int blockSize
-
buffer
private final byte[] buffer
-
outputBuffer
private final byte[] outputBuffer
-
minCompressionRatio
private final double minCompressionRatio
-
out
private final java.io.OutputStream out
-
position
private int position
-
closed
private boolean closed
-
-
Constructor Detail
-
AbstractSnappyOutputStream
public AbstractSnappyOutputStream(java.io.OutputStream out, int blockSize, double minCompressionRatio) throws java.io.IOException
- Parameters:
out
- The underlyingOutputStream
to write to. Must not benull
.blockSize
- The block size (of raw data) to compress before writing frames to out.minCompressionRatio
- Defines the minimum compression ratio (compressedLength / rawLength
) that must be achieved to write the compressed data. This must be in (0, 1.0].- Throws:
java.io.IOException
-
-
Method Detail
-
writeHeader
protected abstract void writeHeader(java.io.OutputStream out) throws java.io.IOException
Writes the implementation specific header or "marker bytes" to out.- Parameters:
out
- The underlyingOutputStream
.- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] input, int offset, int length) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
flush
public final void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
close
public final void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
copyToBuffer
private void copyToBuffer(byte[] input, int offset, int length)
-
flushBuffer
private void flushBuffer() throws java.io.IOException
Compresses and writes out any buffered data. This does nothing if there is no currently buffered data.- Throws:
java.io.IOException
-
writeCompressed
private void writeCompressed(byte[] input, int offset, int length) throws java.io.IOException
Calculates
the crc, compresses the data, determines if the compression ratio is acceptable and callswriteBlock(OutputStream, byte[], int, int, boolean, int)
to actually write the frame.- Parameters:
input
- The byte[] containing the raw data to be compressed.offset
- The offset into input where the data starts.length
- The amount of data in input.- Throws:
java.io.IOException
-
calculateCRC32C
protected int calculateCRC32C(byte[] data, int offset, int length)
Calculates a CRC32C checksum over the data.This can be overridden to provider alternative implementations (such as returning 0 if checksums are not desired).
- Returns:
- The CRC32 checksum.
-
writeBlock
protected abstract void writeBlock(java.io.OutputStream out, byte[] data, int offset, int length, boolean compressed, int crc32c) throws java.io.IOException
Write a frame (block) to out.- Parameters:
out
- TheOutputStream
to write to.data
- The data to write.offset
- The offset in data to start at.length
- The length of data to use.compressed
- Indicates if data is the compressed or raw content. This is based on whether the compression ratio desired is reached.crc32c
- The calculated checksum.- Throws:
java.io.IOException
-
-