com.msi.comms.bytebuffer
Class FixedBuffer

java.lang.Object
  |
  +--com.msi.comms.bytebuffer.FixedBuffer
All Implemented Interfaces:
java.io.Serializable

public class FixedBuffer
extends java.lang.Object
implements java.io.Serializable

FixedBuffer provides a model of a fixed length byte buffer as is commonly used in communications between various systems. The buffers modeled here have:

This implementation supports construction of the various buffer formats that might be used in a communications protocol without hard-coding low-level access.

Given a buffer layout:

 msgid  cmd   parm 1    parm 2
[-----][---][--------][--------]

    FixedBuffer buf = new FixedBuffer(24);
    buf.addCell("Message ID", 5);
    buf.addCell("Command", 3);
    buf.addCell("Parameter 1", 8);
    buf.addCell("Parameter 2", 8);

Version:
1.0
Author:
Ken Thompson
See Also:
Serialized Form

Field Summary
protected  byte[] buffer
           
private  java.util.ArrayList cells
           
private  int length
           
 
Constructor Summary
FixedBuffer(int length)
          Creates a new FixedBuffer instance.
 
Method Summary
 int addCell(java.lang.String name, int length)
          addCell add a cell to the end of the existing cells defining the buffer format.
 void clearBuffer(byte b)
          clearBuffer set all bytes in the buffer to the given value.
 byte[] getBytes()
          getBytes return a copy of the buffer bytes.
 byte[] getCell(int index)
          getCell retrieve the buffer contents specified by cell index.
 double getCellDouble(int index)
          getCellDouble retrieve the buffer contents specified by cell index.
 int getCellInt(int index)
          getCellInt retrieve the buffer contents specified by cell index.
 java.util.ArrayList getCells()
          Get the value of cells.
 java.lang.String getCellText(int index)
          getCellText retrieve the buffer contents specified by cell index.
 java.lang.String getCellTextTrim(int index)
          getCellTextTrim retrieve the buffer contents specified by cell index.
 int getLength()
          Get the value of length.
private  int nextOffset()
          nextOffset calculate the index of the next available offset into the buffer.
 void setBuffer(byte[] v)
          setBuffer copy as much of the supplied data as possible into the buffer.
 void setCell(int index, byte[] value)
          setCell copy the supplied bytes into the fixed buffer at the location defined by the cell at the supplied index.
 void setCells(java.util.ArrayList v)
          Set the value of cells.
 void setLength(int v)
          Set the value of length.
 java.lang.String toString()
          toString presentation of class state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

length

private int length

buffer

protected byte[] buffer

cells

private java.util.ArrayList cells
Constructor Detail

FixedBuffer

public FixedBuffer(int length)
Creates a new FixedBuffer instance.

Parameters:
length - an int of the fixed buffer in bytes.
Method Detail

setBuffer

public void setBuffer(byte[] v)
setBuffer copy as much of the supplied data as possible into the buffer.

Parameters:
v - a byte[] value

clearBuffer

public void clearBuffer(byte b)
clearBuffer set all bytes in the buffer to the given value.

Parameters:
b - a byte value

getBytes

public byte[] getBytes()
getBytes return a copy of the buffer bytes.

Returns:
a byte[] value

addCell

public int addCell(java.lang.String name,
                   int length)
            throws java.lang.Exception
addCell add a cell to the end of the existing cells defining the buffer format.

Parameters:
name - a String name for the cell.
length - an int length in bytes for the cell.
Returns:
an int representing the index of the new cell.
java.lang.Exception

setCell

public void setCell(int index,
                    byte[] value)
             throws java.lang.Exception
setCell copy the supplied bytes into the fixed buffer at the location defined by the cell at the supplied index.

Parameters:
index - an int value
value - a byte[] value
Throws:
java.lang.Exception - if an error occurs

getCell

public byte[] getCell(int index)
               throws java.lang.Exception
getCell retrieve the buffer contents specified by cell index.

Parameters:
index - an int value
Returns:
a byte[] value
Throws:
java.lang.Exception - if an error occurs

getCellText

public java.lang.String getCellText(int index)
                             throws java.lang.Exception
getCellText retrieve the buffer contents specified by cell index.

Parameters:
index - an int value
Throws:
java.lang.Exception - if an error occurs

getCellTextTrim

public java.lang.String getCellTextTrim(int index)
                                 throws java.lang.Exception
getCellTextTrim retrieve the buffer contents specified by cell index.

Parameters:
index - an int value
Throws:
java.lang.Exception - if an error occurs

getCellInt

public int getCellInt(int index)
               throws java.lang.Exception
getCellInt retrieve the buffer contents specified by cell index.

Parameters:
index - an int value
Throws:
java.lang.Exception - if an error occurs

getCellDouble

public double getCellDouble(int index)
                     throws java.lang.Exception
getCellDouble retrieve the buffer contents specified by cell index.

Parameters:
index - an int value
Throws:
java.lang.Exception - if an error occurs

toString

public java.lang.String toString()
toString presentation of class state.

Overrides:
toString in class java.lang.Object
Returns:
a String value

nextOffset

private int nextOffset()
nextOffset calculate the index of the next available offset into the buffer.

Returns:
an int index of the next available byte.

getLength

public int getLength()
Get the value of length.

Returns:
value of length.

setLength

public void setLength(int v)
Set the value of length.

Parameters:
v - Value to assign to length.

getCells

public java.util.ArrayList getCells()
Get the value of cells.

Returns:
value of cells.

setCells

public void setCells(java.util.ArrayList v)
Set the value of cells.

Parameters:
v - Value to assign to cells.


Copyright © 2002 Cingular. All Rights Reserved.