ewe.io
Class CompressedRandomStream

java.lang.Object
  extended byewe.io.RandomStream
      extended byewe.io.CompressedRandomStream

public class CompressedRandomStream
extends RandomStream

A CompressedRandomStream is a read-only RandomStream that has been compressed for storage, but optimized for random access reading. This is unlike a stream that has been zipped where the entire stream is zipped into one entire zipped file. A CompressedRandomStream is compressed in blocks (of a size that you can select on creation) and a directory of the locations of each block is maintained, allowing for quick location and decompression of specific bytes within the file.

Any file may be converted to a CompressedRandomStream, using the static compressStream() methods or you can run the main() method of CompressedRandomStream to compress a File from the command line.


Field Summary
protected  RandomStream inputStream
           
static byte[] signature
          These are the signature bytes of a CompressedRandomStream.
 
Fields inherited from class ewe.io.RandomStream
closed, noWritingAllowed, ras
 
Constructor Summary
CompressedRandomStream(RandomAccessStream compressedFile)
          Create a CompressedRandomStream from a RandomAccessStream.
CompressedRandomStream(RandomStream compressedFile)
          Create a CompressedRandomStream from a RandomStream.
 
Method Summary
 boolean canWrite()
           
 void close()
           
static boolean compressStream(Handle handle, InputStream input, RandomStream out, int blockSize)
          Compress an InputStream into a CompressedRandomStream.
static boolean compressStream(Handle handle, InputStream input, RandomStream out, int blockSize, int compressionLevel, long knownSize)
          Compress an InputStream into a CompressedRandomStream.
 long getCacheSize()
          Get the number of bytes to cache in memory.
static boolean isCompressedRandomStream(File input)
          Check if a File has the CompressedRandomStream signature.
 long length()
           
static void main(String[] args)
          This method can be used to convert a File into a CompressedRandomStream.
 int read()
          Reads the next byte of data from this input stream.
 int read(byte[] buffer, int start, int length)
          Read in a number of bytes of data from the input stream.
 void seek(long position)
           
 void setCacheSize(long cacheSize)
          Set the number of bytes to cache in memory.
 long tell()
           
 
Methods inherited from class ewe.io.RandomStream
available, cantWrite, checkClosed, flush, getFastStream, read, readSingleByteFromMultiByteRead, setLength, shutdown, skip, toInputStream, toOutputStream, toRandomAccessStream, toReadableStream, toWritableStream, write, write, write, writeSingleByteToMultiByteWrite
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

signature

public static final byte[] signature
These are the signature bytes of a CompressedRandomStream.


inputStream

protected RandomStream inputStream
Constructor Detail

CompressedRandomStream

public CompressedRandomStream(RandomAccessStream compressedFile)
                       throws IOException
Create a CompressedRandomStream from a RandomAccessStream.

Parameters:
compressedFile - the compressed file.
Throws:
IOException - if there was an error reading the file.

CompressedRandomStream

public CompressedRandomStream(RandomStream compressedFile)
                       throws IOException
Create a CompressedRandomStream from a RandomStream.

Parameters:
compressedFile - the compressed file.
Throws:
IOException - if there was an error reading the file.
Method Detail

compressStream

public static boolean compressStream(Handle handle,
                                     InputStream input,
                                     RandomStream out,
                                     int blockSize)
                              throws IOException
Compress an InputStream into a CompressedRandomStream.

Parameters:
handle - an optional handle that can be used to abort the process.
input - the source stream.
out - an output RandomStream.
blockSize - the block size to use. A default of 1024 is used if this is zero.
Returns:
true if the operation completed, false if it was aborted because stop() was called on the Handle.
Throws:
IOException - if an IO error occured.

compressStream

public static boolean compressStream(Handle handle,
                                     InputStream input,
                                     RandomStream out,
                                     int blockSize,
                                     int compressionLevel,
                                     long knownSize)
                              throws IOException
Compress an InputStream into a CompressedRandomStream.

Parameters:
handle - an optional handle that can be used to abort the process.
input - the source stream.
out - an output RandomStream.
blockSize - the block size to use. A default of 1024 is used if this is zero.
compressionLevel - one of the ZipConstant.Z_xxx_COMPRESSION values, or zero for default.
knownSize - the size of the input stream if it is known, or -1 if it is not known.
Returns:
true if the operation completed, false if it was aborted because stop() was called on the Handle.
Throws:
IOException - if an IO error occured.

isCompressedRandomStream

public static boolean isCompressedRandomStream(File input)
                                        throws IOException
Check if a File has the CompressedRandomStream signature.

Parameters:
input - the input file.
Returns:
true if the file has the CompressedRandomStream signature.
Throws:
IOException - if there is an error reading from the file.

setCacheSize

public void setCacheSize(long cacheSize)
Set the number of bytes to cache in memory.

Parameters:
cacheSize - the number of bytes to cache in memory.

getCacheSize

public long getCacheSize()
Get the number of bytes to cache in memory.


main

public static void main(String[] args)
                 throws IOException,
                        InterruptedException
This method can be used to convert a File into a CompressedRandomStream.

use the command line:

        Ewe ewe.io.CompressedRandomStream input_file output_file [block_size]

The default block_size is 1024 bytes.

Throws:
IOException
InterruptedException

read

public int read(byte[] buffer,
                int start,
                int length)
         throws IOException
Description copied from class: RandomStream
Read in a number of bytes of data from the input stream.

Overrides:
read in class RandomStream
Parameters:
buffer - a destination buffer for the data.
start - The start offset in the destination buffer.
length - The number of bytes to read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs during reading.

read

public int read()
         throws IOException
Description copied from class: RandomStream
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Overrides:
read in class RandomStream
Returns:
the byte read or -1 on end of stream.
Throws:
IOException - if an I/O error occured.

close

public void close()
           throws IOException
Overrides:
close in class RandomStream
Throws:
IOException

seek

public void seek(long position)
          throws IOException
Overrides:
seek in class RandomStream
Throws:
IOException

tell

public long tell()
          throws IOException
Overrides:
tell in class RandomStream
Throws:
IOException

length

public long length()
            throws IOException
Overrides:
length in class RandomStream
Throws:
IOException

canWrite

public boolean canWrite()
Overrides:
canWrite in class RandomStream