cz.cuni.jagrlib.piece
Class JavaBitStream

java.lang.Object
  extended by cz.cuni.jagrlib.DefaultProperty
      extended by cz.cuni.jagrlib.Piece
          extended by cz.cuni.jagrlib.piece.JavaBitStream
All Implemented Interfaces:
Breakable, BitStream, Property, Template

public class JavaBitStream
extends Piece
implements BitStream

Bit-stream object implemented with help of java.io streams (InputStream, OutputStream).

Since:
0.13
See Also:
JavaBitStream.java

Field Summary
protected  byte[] array
          Temporary array for faster I/O operations.
protected  long buffer
          Bit buffer.
protected  int bufferBits
          Actual buffer length in bits.
protected static java.lang.String CATEGORY
          Object category.
protected  java.io.InputStream inputStream
          Current input stream.
protected  boolean isOutput
          Stream is intended for output.
protected  java.io.OutputStream outputStream
          Current output stream.
protected  long position
          Actual stream-position in bits.
static RegPiece reg
          Static registration instance for this class.
static java.lang.String STREAM_OUTPUT
          Stream-type property (boolean): intended for output.
protected  java.lang.String streamName
          Data stream name (file-name etc.).
protected static java.lang.String TEMPLATE_NAME
          Object template identifier.
 
Fields inherited from class cz.cuni.jagrlib.Piece
channels, info, pl, plugs, TEMPLATE_TYPE, userBreak
 
Fields inherited from interface cz.cuni.jagrlib.iface.BitStream
DEFAULT_STREAM_NAME, STREAM_NAME
 
Fields inherited from interface cz.cuni.jagrlib.iface.Property
LOGGING, STATISTICS, TEXT_DESCRIPTION
 
Fields inherited from interface cz.cuni.jagrlib.Template
ALL_PLUGS, C_1D, C_2D, C_3D, C_ALPHA, C_BINARY, C_BREP, C_CAMERA, C_CIRCLE, C_CLIP, C_COMPRESSION, C_CURVE, C_DATA, C_DRAW, C_EDITOR, C_ELLIPSE, C_FILL, C_FILTER, C_FLOAT, C_FUNCTION, C_IMAGE, C_INTEGER, C_IO, C_LIGHT, C_LINE, C_METRIC, C_POLYGON, C_PROJECTION, C_RADIOSITY, C_RASTER, C_RENDER, C_SAMPLE, C_SCENE, C_SET, C_SOLID, C_TEXT, C_TRANSFORM, C_VECTOR, C_VIEWER, C_WORKER, CAT_EMPTY, EMPTY, IFACE, JAGRLIB, JAGRLIB2, MANIPULATOR_COMBO, MANIPULATOR_CUSTOM, MANIPULATOR_DEFAULT, MANIPULATOR_MULTILINE, PL_ALPHAMASK, PL_BITMASK, PL_CAUSTIC, PL_CODEC, PL_COLORMAP, PL_COMPARE, PL_DATA, PL_DIRECT, PL_EDITOR, PL_FILTER, PL_FUNCTION, PL_IMAGE, PL_IMPORT, PL_INPUT, PL_INTERSECTION, PL_LIGHTSOURCE, PL_ORDER, PL_OUTPUT, PL_PALETTE, PL_PHOTON, PL_PROPERTY, PL_QUANTIZER, PL_RASTER, PL_RENDER, PL_SHADOW, PL_STREAM, PL_TRANSFORM, PL_TRIGGER, PL_VOLUME, PL_WINDOW, TYPE_BOOLEAN, TYPE_DOUBLE, TYPE_FLOAT, TYPE_INTEGER, TYPE_LONG, TYPE_OBJECT, TYPE_STRING
 
Constructor Summary
JavaBitStream()
           
 
Method Summary
 long available()
          How many bits are available to read (or skip).
 boolean canRead()
          Stream is able to read data.
 void close()
          Stream close.
 void flush()
          Stream flush.
protected  void flushBuffer()
          Flushes out the complete buffer.
 java.lang.Object get(java.lang.String key)
          Gets the given property.
 java.io.InputStream getInputStream()
          Retrieves the associated input stream (non-mandatory).
 java.io.OutputStream getOutputStream()
          Retrieves the associated output stream (non-mandatory).
 void open(boolean output, java.lang.String name, java.lang.String detail)
          Stream initialization.
 long position()
          Actual stream position in bits.
 long read(int length)
          Reads a bunch of bits from the stream.
protected  void readBuffer()
          Reads maximum amount of input bits into buffer.
 void readBytes(byte[] data, int start, int length)
          Reads a bunch of bytes from the stream.
 void reOpen(boolean output, java.lang.String detail)
          Stream re-initialization.
 long seek(long pos)
          Sets actual position of the stream in bits.
 void set(java.lang.String key, java.lang.Object value)
          Sets the given property.
static int setTemplate(Template t, int ord)
          General-purpose registration routine.
 long skip(long length)
          Skips the given number of bits.
 void write(long bits, int length)
          Writes a bunch of bits into the stream.
protected  void writeBuffer()
          Writes all complete bytes from buffer to the output stream.
 void writeBytes(byte[] data, int start, int length)
          Writes a bunch of bytes into the stream.
 
Methods inherited from class cz.cuni.jagrlib.Piece
className, connect, findPlug, findPlug, getInfo, getInterface, getInterface, getPlug, getReg, init, isCompatible, isCompatible, isConnected, logError, logWarning, myInterface, newInputPlug, newOptOutputPlug, newOutputPlug, newPlug, noTemplates, propBegin, propBounds, propDefault, propEnd, propEnum, propManipulator, propManipulator, setProgressListener, setRegStrings, setTemplateDynamic, stop
 
Methods inherited from class cz.cuni.jagrlib.DefaultProperty
booleanProperty, booleanProperty, colorDoubleProperty, colorProperty, commit, doubleProperty, doubleProperty, doubleProperty, enumProperty, floatProperty, floatProperty, floatProperty, intProperty, intProperty, intProperty, longProperty, longProperty, longProperty, set, stringProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface cz.cuni.jagrlib.iface.Property
commit
 

Field Detail

isOutput

protected boolean isOutput
Stream is intended for output.


streamName

protected java.lang.String streamName
Data stream name (file-name etc.).


inputStream

protected java.io.InputStream inputStream
Current input stream.


outputStream

protected java.io.OutputStream outputStream
Current output stream.


buffer

protected long buffer
Bit buffer.


bufferBits

protected int bufferBits
Actual buffer length in bits.


position

protected long position
Actual stream-position in bits.


array

protected byte[] array
Temporary array for faster I/O operations.


STREAM_OUTPUT

public static final java.lang.String STREAM_OUTPUT
Stream-type property (boolean): intended for output.

See Also:
Constant Field Values

TEMPLATE_NAME

protected static final java.lang.String TEMPLATE_NAME
Object template identifier.

See Also:
Constant Field Values

CATEGORY

protected static final java.lang.String CATEGORY
Object category.

See Also:
Constant Field Values

reg

public static final RegPiece reg
Static registration instance for this class. Automatically initialized in class-loading time.

Constructor Detail

JavaBitStream

public JavaBitStream()
Method Detail

writeBuffer

protected final void writeBuffer()
                          throws java.io.IOException
Writes all complete bytes from buffer to the output stream.

Throws:
java.io.IOException
See Also:
flushBuffer(), buffer

flushBuffer

protected void flushBuffer()
                    throws java.io.IOException
Flushes out the complete buffer. Incomplete byte is padded with zero bits.

Throws:
java.io.IOException
See Also:
writeBuffer(), buffer

readBuffer

protected final void readBuffer()
                         throws java.io.IOException
Reads maximum amount of input bits into buffer. Assumes bufferBits== 0.

Throws:
java.io.IOException
See Also:
writeBuffer(), buffer

canRead

public boolean canRead()
Stream is able to read data. If true, stream can be opened automatically in the init-phase.

Specified by:
canRead in interface BitStream

open

public void open(boolean output,
                 java.lang.String name,
                 java.lang.String detail)
          throws java.io.IOException
Stream initialization.

Specified by:
open in interface BitStream
Parameters:
output - Open for output?
name - Stream name (either stream identification e.g. file-name). null for implicit bit-stream (reOpen(boolean, java.lang.String)).
detail - Further implementation-specific information.
Throws:
java.io.IOException
See Also:
reOpen(boolean, java.lang.String)

reOpen

public void reOpen(boolean output,
                   java.lang.String detail)
            throws java.io.IOException
Stream re-initialization.

Specified by:
reOpen in interface BitStream
Parameters:
output - Open for output?
detail - Further implementation-specific information.
Throws:
java.io.IOException
See Also:
open(boolean, java.lang.String, java.lang.String)

getOutputStream

public java.io.OutputStream getOutputStream()
Retrieves the associated output stream (non-mandatory).

Specified by:
getOutputStream in interface BitStream
Returns:
The associated OutputStream instance or null if not implemented.
See Also:
getInputStream()

getInputStream

public java.io.InputStream getInputStream()
Retrieves the associated input stream (non-mandatory).

Specified by:
getInputStream in interface BitStream
Returns:
The associated InputStream instance or null if not implemented.
See Also:
getOutputStream()

flush

public void flush()
           throws java.io.IOException
Stream flush.

Specified by:
flush in interface BitStream
Throws:
java.io.IOException
See Also:
open(boolean, java.lang.String, java.lang.String), close()

close

public void close()
           throws java.io.IOException
Stream close. Eventually flushes all written data and disconnects the stream.

Specified by:
close in interface BitStream
Throws:
java.io.IOException
See Also:
open(boolean, java.lang.String, java.lang.String), reOpen(boolean, java.lang.String)

position

public long position()
              throws java.io.IOException
Actual stream position in bits. After the output stream is closed, returns maximal written position.

Specified by:
position in interface BitStream
Returns:
Actual (maximal) stream position in bits.
Throws:
java.io.IOException
See Also:
available(), seek(long)

available

public long available()
               throws java.io.IOException
How many bits are available to read (or skip).

Specified by:
available in interface BitStream
Returns:
Number of bits available to read (or skip).
Throws:
java.io.IOException
See Also:
position, seek(long)

seek

public long seek(long pos)
Sets actual position of the stream in bits. Non mandatory.

Specified by:
seek in interface BitStream
Parameters:
pos - Required stream position in bits.
Returns:
New stream position in bits (or -1L if not implemented).
Throws:
java.io.IOException
See Also:
position

write

public void write(long bits,
                  int length)
           throws java.io.IOException
Writes a bunch of bits into the stream.

Specified by:
write in interface BitStream
Parameters:
bits - Bits to be written (LSB first).
length - Number of bits to write (negative lengths are ignored).
Throws:
java.io.IOException
See Also:
writeBytes(byte[], int, int), read(int)

writeBytes

public void writeBytes(byte[] data,
                       int start,
                       int length)
                throws java.io.IOException
Writes a bunch of bytes into the stream.

Specified by:
writeBytes in interface BitStream
Parameters:
data - Bytes to be written (each byte is written in "LSB first" order).
start - First index to write. Must be less than data.length.
length - Number of bytes to write (start + length > data.length is ignored).
Throws:
java.io.IOException
See Also:
write(long, int), readBytes(byte[], int, int)

read

public long read(int length)
          throws java.io.IOException
Reads a bunch of bits from the stream.

Specified by:
read in interface BitStream
Parameters:
length - Number of bits to read (negative lengths are ignored).
Returns:
Read bits (LSB first).
Throws:
java.io.IOException
See Also:
readBytes(byte[], int, int), write(long, int)

readBytes

public void readBytes(byte[] data,
                      int start,
                      int length)
               throws java.io.IOException
Reads a bunch of bytes from the stream.

Specified by:
readBytes in interface BitStream
Parameters:
data - Array holding the read bytes (each byte is read in "LSB first" order).
start - First index to read. Must be less than data.length.
length - Number of bytes to read (start + length > data.length is ignored).
Throws:
java.io.IOException
See Also:
read(int), writeBytes(byte[], int, int)

skip

public long skip(long length)
          throws java.io.IOException
Skips the given number of bits.

Specified by:
skip in interface BitStream
Parameters:
length - Number of bits to skip (negative lengths are ignored).
Returns:
Number of bits actually skipped.
Throws:
java.io.IOException
See Also:
read(int)

set

public void set(java.lang.String key,
                java.lang.Object value)
Sets the given property.

Specified by:
set in interface Property
Overrides:
set in class DefaultProperty
Parameters:
key - Key string.
value - The new value.
See Also:
Property.commit()

get

public java.lang.Object get(java.lang.String key)
Gets the given property.

Specified by:
get in interface Property
Overrides:
get in class DefaultProperty
Parameters:
key - Key string.
Returns:
The actual value or null.

setTemplate

public static int setTemplate(Template t,
                              int ord)
General-purpose registration routine. Sets all plugs, strings, etc. to the given Template.