uk.ac.rdg.resc.jstyx.messages
Class StyxBuffer

java.lang.Object
  extended by uk.ac.rdg.resc.jstyx.messages.StyxBuffer

public class StyxBuffer
extends java.lang.Object

Wrapper class for a ByteBuffer that allows Styx primitives to be easily read from and written to the buffer. Reads and writes unsigned bytes, shorts and integers, in addition to other Styx types. Before using the methods in this class, you should check that the required number of bytes are available in the ByteBuffer. The easiest way to do this is to read the message length (the first four bytes in the Styx message) and check that this number of bytes are available in the buffer.

Author:
Jon Blower $Revision: 507 $ $Date: 2005-12-01 08:21:56 +0000 (Thu, 01 Dec 2005) $ $Log$ Revision 1.5 2005/12/01 08:21:56 jonblower Fixed javadoc comments Revision 1.4 2005/11/03 07:46:55 jonblower Trying to fix bug with sending RreadMessages Revision 1.3 2005/03/16 17:56:22 jonblower Replaced use of java.nio.ByteBuffer with MINA's ByteBuffer to minimise copying of buffers Revision 1.2 2005/03/11 14:02:16 jonblower Merged MINA-Test_20059309 into main line of development Revision 1.1.2.1 2005/03/10 11:35:28 jonblower Moved from JStyx root package Revision 1.2 2005/03/09 17:01:23 jonblower Added more methods reflecting methods in underlying ByteBuffer Revision 1.1.1.1 2005/02/16 18:58:16 jonblower Initial import

Constructor Summary
StyxBuffer(org.apache.mina.common.ByteBuffer buf)
          Creates a new instance of StyxBuffer
 
Method Summary
 void flip()
          Flip the underlying buffer
 org.apache.mina.common.ByteBuffer getBuffer()
          Gets the underlying ByteBuffer
 byte[] getData(int size)
          Gets a chunk of data from the buffer.
 DirEntry getDirEntry()
           
 Qid getQid()
           
 java.lang.String getString()
           
 int getUByte()
           
 long getUInt()
           
 ULong getULong()
           
 int getUShort()
           
 StyxBuffer put(byte[] bytes)
           
 StyxBuffer put(byte[] bytes, int offset, int length)
           
 StyxBuffer putData(org.apache.mina.common.ByteBuffer data)
          Puts a chunk of data to the buffer.
 StyxBuffer putData(org.apache.mina.common.ByteBuffer data, long size)
          Puts a chunk of data to the buffer.
 StyxBuffer putDirEntry(DirEntry dir)
          Puts the given DirEntry to the buffer at the current position
 StyxBuffer putQid(Qid qid)
          Puts the given Qid to the buffer at the current position
 StyxBuffer putString(java.lang.String s)
          Puts a string into the buffer at the current position.
 StyxBuffer putUByte(int b)
          Puts an unsigned byte into the buffer at the current position
 StyxBuffer putUInt(int index, long l)
          Puts an unsigned int (4 bytes) into the buffer at the given position
 StyxBuffer putUInt(long l)
          Puts an unsigned int (4 bytes) into the buffer at the current position
 StyxBuffer putULong(ULong ulong)
          Puts the given ULong to the buffer at the current position
 StyxBuffer putUShort(int s)
          Puts an unsigned short (2 bytes) into the buffer at the current position
 StyxBuffer putUShort(int index, int s)
          Puts an unsigned short (2 bytes) into the buffer at the given position
 int remaining()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StyxBuffer

public StyxBuffer(org.apache.mina.common.ByteBuffer buf)
Creates a new instance of StyxBuffer

Parameters:
buf - The ByteBuffer to wrap
Throws:
java.lang.IllegalArgumentException - if the ByteBuffer is null
Method Detail

getUByte

public int getUByte()
Returns:
the next unsigned byte as an integer between 0 and 255

putUByte

public StyxBuffer putUByte(int b)
Puts an unsigned byte into the buffer at the current position

Parameters:
b - The value of the byte as an integer between 0 and 255
Returns:
this StyxBuffer (allows chaining of put commands)
Throws:
java.lang.IllegalArgumentException - if the parameter is out of range

getUShort

public int getUShort()
Returns:
the next unsigned short (2 bytes) as an integer between 0 and 65335

putUShort

public StyxBuffer putUShort(int s)
Puts an unsigned short (2 bytes) into the buffer at the current position

Parameters:
s - The value of the short as an integer between 0 and 65535
Returns:
this StyxBuffer (allows chaining of put commands)
Throws:
java.lang.IllegalArgumentException - if the parameter is out of range

putUShort

public StyxBuffer putUShort(int index,
                            int s)
Puts an unsigned short (2 bytes) into the buffer at the given position

Parameters:
index - The place in the buffer at which to put the data
s - The value of the short as an integer between 0 and 65535
Returns:
this StyxBuffer (allows chaining of put commands)
Throws:
java.lang.IllegalArgumentException - if the parameter is out of range

getUInt

public long getUInt()
Returns:
the next unsigned int (4 bytes) as a long between 0 and 4,294,967,295

putUInt

public StyxBuffer putUInt(long l)
Puts an unsigned int (4 bytes) into the buffer at the current position

Parameters:
l - The value of the int as a long integer between 0 and 4,294,967,295
Returns:
this StyxBuffer (allows chaining of put commands)
Throws:
java.lang.IllegalArgumentException - if the parameter is out of range

putUInt

public StyxBuffer putUInt(int index,
                          long l)
Puts an unsigned int (4 bytes) into the buffer at the given position

Parameters:
index - The place in the buffer at which to put the data
l - The value of the int as a long integer between 0 and 4,294,967,295
Returns:
this StyxBuffer (allows chaining of put commands)
Throws:
java.lang.IllegalArgumentException - if the parameter is out of range

getULong

public ULong getULong()
Returns:
the next ULong (unsigned 8-byte integer)

putULong

public StyxBuffer putULong(ULong ulong)
Puts the given ULong to the buffer at the current position

Parameters:
ulong - The ulong to write
Returns:
this StyxBuffer (allows chaining of put commands)

getString

public java.lang.String getString()
Returns:
the next string

putString

public StyxBuffer putString(java.lang.String s)
Puts a string into the buffer at the current position. First converts the string to an array of bytes in UTF-8.

Parameters:
s - The string to write to the buffer
Returns:
this StyxBuffer (allows chaining of put commands)

getQid

public Qid getQid()
Returns:
The next Qid (server's representation of a file)

putQid

public StyxBuffer putQid(Qid qid)
Puts the given Qid to the buffer at the current position

Parameters:
qid - The Qid to write
Returns:
this StyxBuffer (allows chaining of put commands)

getDirEntry

public DirEntry getDirEntry()
Returns:
The next DirEntry (Directory entry)

putDirEntry

public StyxBuffer putDirEntry(DirEntry dir)
Puts the given DirEntry to the buffer at the current position

Parameters:
dir - The DirEntry to write
Returns:
this StyxBuffer (allows chaining of put commands)

getData

public byte[] getData(int size)
Gets a chunk of data from the buffer. After calling this method successfully, the position of the buffer will be increased by size.

Parameters:
size - the number of bytes to get
Returns:
A new byte array containing a copy of the data in this buffer
Throws:
java.lang.IllegalArgumentException - if there aren't enough bytes left in the buffer

put

public StyxBuffer put(byte[] bytes)

put

public StyxBuffer put(byte[] bytes,
                      int offset,
                      int length)

putData

public StyxBuffer putData(org.apache.mina.common.ByteBuffer data,
                          long size)
Puts a chunk of data to the buffer.

Parameters:
data - The data to write. The position and limit of this buffer will be unaffected by this method.
size - The number of bytes to write
Returns:
this StyxBuffer (allows chaining of put commands)
Throws:
java.lang.IllegalArgumentException - if the buffer does not contain at least size bytes, or if there is not enough space in the output buffer for the data.

putData

public StyxBuffer putData(org.apache.mina.common.ByteBuffer data)
Puts a chunk of data to the buffer.

Parameters:
data - The data to write. The position and limit of this buffer will be unaffected by this method. All the remaining data in the buffer will be written.
Returns:
this StyxBuffer (allows chaining of put commands)

getBuffer

public org.apache.mina.common.ByteBuffer getBuffer()
Gets the underlying ByteBuffer


remaining

public int remaining()
Returns:
the number of bytes remaining in the underlying buffer

flip

public void flip()
Flip the underlying buffer



Copyright © 2004-2006 Reading e-Science Centre. All Rights Reserved.