Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.ning.compress.lzf;
  
  
Decorator java.io.InputStream implementation used for reading compressed data and uncompressing it on the fly, such that reads return uncompressed data. Its direct counterpart is LZFOutputStream; but there is also LZFCompressingInputStream which does reverse of this class.

 
 public class LZFInputStream extends InputStream
 {
    
Underlying decoder in use.
 
     protected final ChunkDecoder _decoder;
    
    
Object that handles details of buffer recycling
 
     protected final BufferRecycler _recycler;

    
stream to be decompressed
 
     protected final InputStream _inputStream;

    
Flag that indicates if we have already called 'inputStream.close()' (to avoid calling it multiple times)
 
     protected boolean _inputStreamClosed;
    
    
Flag that indicates whether we force full reads (reading of as many bytes as requested), or 'optimal' reads (up to as many as available, but at least one). Default is false, meaning that 'optimal' read is used.
 
     protected boolean _cfgFullReads = false;

    
the current buffer of compressed bytes (from which to decode)
 
     protected byte[] _inputBuffer;

    
the buffer of uncompressed bytes from which content is read
 
     protected byte[] _decodedBytes;

    
The current position (next char to output) in the uncompressed bytes buffer.
 
     protected int _bufferPosition = 0;
    
    
Length of the current uncompressed bytes buffer
 
     protected int _bufferLength = 0;

    
Number of bytes read from the underlying _inputStream
 
     protected int _readCount = 0;
     
     /*
     ///////////////////////////////////////////////////////////////////////
     // Construction
     ///////////////////////////////////////////////////////////////////////
      */
 
     public LZFInputStream(final InputStream inputStreamthrows IOException
     {
         this(inputStreamfalse);
     }
 
     public LZFInputStream(final ChunkDecoder decoderfinal InputStream in)
         throws IOException
     {
         this(decoderinfalse);
     }
    
    

Parameters:
in Underlying input stream to use
fullReads Whether read(byte[]) should try to read exactly as many bytes as requested (true); or just however many happen to be available (false)
 
    public LZFInputStream(final InputStream inboolean fullReadsthrows IOException
    {
        this(ChunkDecoderFactory.optimalInstance(), infullReads);
    }
    public LZFInputStream(final ChunkDecoder decoderfinal InputStream inboolean fullReads)
        throws IOException
    {
        super();
         = decoder;
         = BufferRecycler.instance();
         = in;
         = false;
         = fullReads;
    }

    
Method that can be used define whether reads should be "full" or "optimal": former means that full compressed blocks are read right away as needed, optimal that only smaller chunks are read at a time, more being read as needed.
    public void setUseFullReads(boolean b) {
         = b;
    }
    
    /*
    ///////////////////////////////////////////////////////////////////////
    // InputStream impl
    ///////////////////////////////////////////////////////////////////////
     */

    
Method is overridden to report number of bytes that can now be read from decoded data buffer, without reading bytes from the underlying stream. Never throws an exception; returns number of bytes available without further reads from underlying source; -1 if stream has been closed, or 0 if an actual read (and possible blocking) is needed to find out.
    @Override
    public int available()
    {
        if () { // javadocs suggest 0 for closed as well (not -1)
            return 0;
        }
        int left = ( - );
        return (left <= 0) ? 0 : left;
    }
    
    @Override
    public int read() throws IOException
    {
        if (!readyBuffer()) {
            return -1;
        }
        return [++] & 255;
    }
    @Override
    public int read(final byte[] bufferthrows IOException
    {
        return read(buffer, 0, buffer.length);
    }
    @Override
    public int read(final byte[] bufferint offsetint lengththrows 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( - length);
    	System.arraycopy(bufferoffsetchunkLength);
    	 += chunkLength;
    	if (chunkLength == length || !) {
    	    return chunkLength;
    	}
    	// Need more data, then
    	int totalRead = chunkLength;
    	do {
            offset += chunkLength;
            if (!readyBuffer()) {
                break;
            }
            chunkLength = Math.min( - , (length - totalRead));
            System.arraycopy(bufferoffsetchunkLength);
             += chunkLength;
            totalRead += chunkLength;
    	} while (totalRead < length);
    	return totalRead;
    }
    
    @Override
    public void close() throws IOException
    {
         =  = 0;
        byte[] buf = ;
        if (buf != null) {
             = null;
            .releaseInputBuffer(buf);
        }
        buf = ;
        if (buf != null) {
             = null;
            .releaseDecodeBuffer(buf);
        }
        if (!) {
             = true;
            .close();
        }
    }

    
Overridden to implement efficient skipping by skipping full chunks whenever possible.
    @Override
    public long skip(long nthrows IOException
    {
        if () {
            return -1;
        }
        if (n <= 0L) {
            return n;
        }
        long skipped;
        // if any left to skip, just return that for simplicity
        if ( < ) {
            int left = ( - );
            if (n <= left) { // small skip, fulfilled from what we already got
                 += (intn;
                return n;
            }
             = ;
            skipped = left;
            n -= left;
        } else {
            skipped = 0L;
        }
        // and then full-chunk skipping, if possible
        while (true) {
            int amount = .skipOrDecodeChunk(n);
            if (amount >= 0) { // successful skipping of the chunk
                skipped += amount;
                n -= amount;
                if (n <= 0L) {
                    return skipped;
                }
                continue;
            }
            if (amount == -1) { // EOF
                close();
                return skipped;
            }
            // decoded buffer-full, more than max skip
             = -(amount+1);
            skipped += n;
             = (intn;
            return skipped;
        }
    }
    
    /*
    ///////////////////////////////////////////////////////////////////////
    // Extended public API
    ///////////////////////////////////////////////////////////////////////
     */

    
Method that can be used to find underlying java.io.InputStream that we read from to get LZF encoded data to decode. Will never return null; although underlying stream may be closed (if this stream has been closed).

Since:
0.8
        return ;
    }

    
Method that can be called to discard any already buffered input, read from input source. Specialized method that only makes sense if the underlying java.io.InputStream can be repositioned reliably.

Since:
0.9
    public void discardBuffered()
    {
         =  = 0;
    }

    
Convenience method that will read and uncompress all data available, and write it using given java.io.OutputStream. This avoids having to make an intermediate copy of uncompressed data which would be needed when doing the same manually.

Parameters:
out OutputStream to use for writing content
Returns:
Number of bytes written (uncompressed)
Since:
0.9.3
    public int readAndWrite(OutputStream outthrows IOException
    {
        int total = 0;
        while (readyBuffer()) {
            int avail =  - ;
            out.write(avail);
             += avail// to ensure it looks like we consumed it all
            total += avail;
        }
        return total;
    }
    
    /*
    ///////////////////////////////////////////////////////////////////////
    // Internal methods
    ///////////////////////////////////////////////////////////////////////
     */

    
Fill the uncompressed bytes buffer by reading the underlying inputStream.

Returns:
True if there is now at least one byte to read in the buffer; false if there is no more content to read
Throws:
java.io.IOException
    protected boolean readyBuffer() throws IOException
    {
        if ( < ) {
            return true;
        }
        if () {
            return false;
        }
        if ( < 0) {
            close();
            return false;
        }
         = 0;
        return ( < );
    }
New to GrepCode? Check out our FAQ X