private static Compressor getCompressor(boolean fast) { return fast ? new CompressLZF() : new CompressDeflate(); }
Compressor getCompressorFast() { if (compressorFast == null) { compressorFast = new CompressLZF(); } return compressorFast; }
public LZFOutputStream(OutputStream out) throws IOException { this.out = out; int len = Constants.IO_BUFFER_SIZE_COMPRESS; buffer = new byte[len]; ensureOutput(len); writeInt(MAGIC); }
private void fillBuffer() throws IOException { if (buffer != null && pos < bufferLength) { return; } int len = readInt(); if (decompress == null) { // EOF this.bufferLength = 0; } else if (len < 0) { len = -len; buffer = ensureSize(buffer, len); readFully(buffer, len); this.bufferLength = len; } else { inBuffer = ensureSize(inBuffer, len); int size = readInt(); readFully(inBuffer, len); buffer = ensureSize(buffer, size); try { decompress.expand(inBuffer, 0, len, buffer, 0, size); } catch (ArrayIndexOutOfBoundsException e) { DbException.convertToIOException(e); } this.bufferLength = size; } pos = 0; }
private static Compressor getCompressor(int algorithm) { switch (algorithm) { case Compressor.NO: return new CompressNo(); case Compressor.LZF: return new CompressLZF(); case Compressor.DEFLATE: return new CompressDeflate(); default: throw DbException.get( ErrorCode.UNSUPPORTED_COMPRESSION_ALGORITHM_1, "" + algorithm); } }
private void compressAndWrite(byte[] buff, int len) throws IOException { if (len > 0) { ensureOutput(len); int compressed = compress.compress(buff, len, outBuffer, 0); if (compressed > len) { writeInt(-len); out.write(buff, 0, len); } else { writeInt(compressed); writeInt(len); out.write(outBuffer, 0, compressed); } } }
private static int compress(byte[] in, int len, Compressor compress, byte[] out) { int newLen = 0; out[0] = (byte) compress.getAlgorithm(); int start = 1 + writeVariableInt(out, 1, len); newLen = compress.compress(in, len, out, start); if (newLen > len + start || newLen <= 0) { out[0] = Compressor.NO; System.arraycopy(in, 0, out, start, len); newLen = len + start; } return newLen; }
Compressor getCompressorHigh() { if (compressorHigh == null) { compressorHigh = new CompressDeflate(); } return compressorHigh; }
@Override public int read() throws IOException { fillBuffer(); if (pos >= bufferLength) { return -1; } return buffer[pos++] & 255; }
@Override public void write(int b) throws IOException { if (pos >= buffer.length) { flush(); } buffer[pos++] = (byte) b; }
@Override public void flush() throws IOException { compressAndWrite(buffer, pos); pos = 0; }
@Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
@Override public int read(byte[] b, int off, int len) throws IOException { if (len == 0) { return 0; } int read = 0; while (len > 0) { int r = readBlock(b, off, len); if (r < 0) { break; } read += r; off += r; len -= r; } return read == 0 ? -1 : read; }
public LZFInputStream(InputStream in) throws IOException { this.in = in; if (readInt() != LZFOutputStream.MAGIC) { throw new IOException("Not an LZFInputStream"); } }
private byte[] expand(int page) { byte[] d = getPage(page); if (d.length == BLOCK_SIZE) { return d; } byte[] out = new byte[BLOCK_SIZE]; if (d != COMPRESSED_EMPTY_BLOCK) { synchronized (LZF) { LZF.expand(d, 0, d.length, out, 0, BLOCK_SIZE); } } setPage(page, d, out, false); return out; }
@Override public int compress(byte[] in, int inLen, byte[] out, int outPos) { Deflater deflater = new Deflater(level); deflater.setStrategy(strategy); deflater.setInput(in, 0, inLen); deflater.finish(); int compressed = deflater.deflate(out, outPos, out.length - outPos); while (compressed == 0) { // the compressed length is 0, meaning compression didn't work // (sounds like a JDK bug) // try again, using the default strategy and compression level strategy = Deflater.DEFAULT_STRATEGY; level = Deflater.DEFAULT_COMPRESSION; return compress(in, inLen, out, outPos); } deflater.end(); return outPos + compressed; }
@Override protected CompressLZF initialValue() { return new CompressLZF(); } };
private int readBlock(byte[] b, int off, int len) throws IOException { fillBuffer(); if (pos >= bufferLength) { return -1; } int max = Math.min(len, bufferLength - pos); max = Math.min(max, b.length - off); System.arraycopy(buffer, pos, b, off, max); pos += max; return max; }
@Override public void close() throws IOException { flush(); out.close(); }
PageLog(PageStore store) { this.store = store; dataBuffer = store.createData(); trace = store.getTrace(); compress = new CompressLZF(); compressBuffer = new byte[store.getPageSize() * 2]; }