fi.helsinki.cs.ohtu.mpeg2.util
Class UnsignedIntegerField

java.lang.Object
  extended by fi.helsinki.cs.ohtu.mpeg2.util.UnsignedIntegerField

public class UnsignedIntegerField
extends java.lang.Object

Represents an unsigned integer field in a MPEG stream. Fields have specific bit widths. This class enforces the width by not allowing setting values which won't fit into a field with the given size as unsigned binary integers. This limit is called the maximum width. In addition to the maximum width of the field, a minimum width for serializing to a bit stream can be specified. Any value in the field at the time of writing will be padded with zero bits, written before the actual value bits to the stream. The maximum width of a field supported by this class is one less than Long.SIZE. The highest bit in a long integer is used for the sign bit, which makes no sense in the unsigned integer context.


Field Summary
private  int maxWidth
           
private  int minWidth
           
private  long value
           
private  int width
           
 
Constructor Summary
UnsignedIntegerField(int width)
          Constructs a new instance which is effectively fixed width.
UnsignedIntegerField(int minWidth, int maxWidth, long value)
          Constructs a new instance with variable width.
UnsignedIntegerField(int width, long value)
          Constructs a new instance which is effectively fixed width.
 
Method Summary
 int getMaxWidth()
          Gets the maximum width of the field.
 int getMinWidth()
          Gets the minimum width of the field.
 long getValue()
          Gets the value of the field.
 void setValue(long newValue)
          Sets the value of the field.
 void writeTo(BitOutputStream stream)
          Writes the value of the field to the given stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

minWidth

private final int minWidth

maxWidth

private final int maxWidth

width

private int width

value

private long value
Constructor Detail

UnsignedIntegerField

public UnsignedIntegerField(int width)
Constructs a new instance which is effectively fixed width.

Parameters:
width - The width of the field, in bits. Both maximum and minimum width are set to this.

UnsignedIntegerField

public UnsignedIntegerField(int width,
                            long value)
Constructs a new instance which is effectively fixed width.

Parameters:
width - The width of the field, in bits.Both maximum and minimum width are set to this.
value - The initial value to set the field to, as if setValue() was called.

UnsignedIntegerField

public UnsignedIntegerField(int minWidth,
                            int maxWidth,
                            long value)
Constructs a new instance with variable width.

Parameters:
minWidth - Minimal allowed width for the field, in bits.
maxWidth - Maximum allowed width for the field, in bits.
value - The initial value to set the field to, as if setValue() was called.
Method Detail

getMaxWidth

public int getMaxWidth()
Gets the maximum width of the field.

Returns:
the maximum width

getMinWidth

public int getMinWidth()
Gets the minimum width of the field.

Returns:
the minimum width

getValue

public long getValue()
Gets the value of the field.

Returns:
The value of the field.

setValue

public void setValue(long newValue)
Sets the value of the field.

Parameters:
newValue - The value to set the field to.

writeTo

public void writeTo(BitOutputStream stream)
             throws java.io.IOException
Writes the value of the field to the given stream.

Parameters:
stream - The bit stream to write to.
Throws:
java.io.IOException