Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.ning.compress.lzf;
  
  import java.io.*;
  
Decorator java.io.OutputStream implementation that will compress output using LZF compression algorithm, given uncompressed input to write. Its counterpart is LZFInputStream; although in some ways LZFCompressingInputStream can be seen as the opposite.

Author(s):
jon hartlaub
Tatu Saloranta
See also:
LZFInputStream
LZFCompressingInputStream
 
 public class LZFOutputStream extends OutputStream 
 {
     private static final int OUTPUT_BUFFER_SIZE = .;
 
     private final ChunkEncoder _encoder;
     private final BufferRecycler _recycler;
 	
     protected final OutputStream _outputStream;
     protected byte[] _outputBuffer;
     protected int _position = 0;

    
Configuration setting that governs whether basic 'flush()' should first complete a block or not.

Default value is 'true'

 
     protected boolean _cfgFinishBlockOnFlush = true;
    
    
Flag that indicates if we have already called '_outputStream.close()' (to avoid calling it multiple times)
 
     protected boolean _outputStreamClosed;
     
     /*
     ///////////////////////////////////////////////////////////////////////
     // Construction, configuration
     ///////////////////////////////////////////////////////////////////////
      */
 
     public LZFOutputStream(final OutputStream outputStream)
     {
         this(ChunkEncoderFactory.optimalInstance(), outputStream);
     }
     
     public LZFOutputStream(final ChunkEncoder encoderfinal OutputStream outputStream)
     {
          = encoder;
          = BufferRecycler.instance();
          = outputStream;
          = false;
     }

    
Method for defining whether call to flush() will also complete current block (similar to calling finishBlock()) or not.
 
     public LZFOutputStream setFinishBlockOnFlush(boolean b) {
          = b;
         return this;
     }
     
     /*
     ///////////////////////////////////////////////////////////////////////
     // OutputStream impl
     ///////////////////////////////////////////////////////////////////////
      */
     
     @Override
     public void write(final int singleBytethrows IOException 
     {
         checkNotClosed();
         if ( >= .) {
             writeCompressedBlock();
         }
         [++] = (bytesingleByte;
     }
 
     @Override
     public void write(final byte[] bufferint offsetint lengththrows IOException
     {
         checkNotClosed();
 
         final int BUFFER_LEN = .;
 
         // simple case first: buffering only (for trivially short writes)
         int free = BUFFER_LEN - ;
        if (free >= length) {
            System.arraycopy(bufferoffsetlength);
             += length;
            return;
        }
        // otherwise, copy whatever we can, flush
        System.arraycopy(bufferoffsetfree);
        offset += free;
        length -= free;
         += free;
        writeCompressedBlock();
        // then write intermediate full block, if any, without copying:
        while (length >= BUFFER_LEN) {
            .encodeAndWriteChunk(bufferoffsetBUFFER_LEN);
            offset += BUFFER_LEN;
            length -= BUFFER_LEN;
        }
        // and finally, copy leftovers in buffer, if any
        if (length > 0) {
            System.arraycopy(bufferoffset, 0, length);
        }
         = length;
    }
    @Override
    public void flush() throws IOException
    {
        checkNotClosed();
        if ( &&  > 0) {
            writeCompressedBlock();
        }
        .flush();
    }
    @Override
    public void close() throws IOException  
    {
        if (!) {
            if ( > 0) {
                writeCompressedBlock();
            }
            .flush();
            .close();
            byte[] buf = ;
            if (buf != null) {
                 = null;
                .releaseOutputBuffer(buf);
            }
             = true;
            .close();
        }
    }
    /*
    ///////////////////////////////////////////////////////////////////////
    // Additional public methods
    ///////////////////////////////////////////////////////////////////////
     */

    
Method that can be used to find underlying java.io.OutputStream that we write encoded LZF encoded data into, after compressing it. Will never return null; although underlying stream may be closed (if this stream has been closed).

Since:
0.8
        return ;
    }

    
Accessor for checking whether call to "flush()" will first finish the current block or not

Since:
0.8
    public boolean getFinishBlockOnFlush() {
        return ;
    }

    
Method that can be used to force completion of the current block, which means that all buffered data will be compressed into an LZF block. This typically results in lower compression ratio as larger blocks compress better; but may be necessary for network connections to ensure timely sending of data.

Since:
0.8
    public LZFOutputStream finishBlock() throws IOException
    {
        checkNotClosed();
        if ( > 0) {
            writeCompressedBlock();
        }
        return this;
    }
    
    /*
    ///////////////////////////////////////////////////////////////////////
    // Internal methods
    ///////////////////////////////////////////////////////////////////////
     */
    
    
Compress and write the current block to the OutputStream
    protected void writeCompressedBlock() throws IOException
    {
        int left = ;
         = 0;
        int offset = 0;
        do {
            int chunkLen = Math.min(.left);
            .encodeAndWriteChunk(offsetchunkLen);
            offset += chunkLen;
            left -= chunkLen;
        } while (left > 0);
    }
    protected void checkNotClosed() throws IOException
    {
        if () {
            throw new IOException(getClass().getName()+" already closed");
        }
    }
New to GrepCode? Check out our FAQ X