|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--waba.io.Stream | +--superwaba.ext.xplat.io.BufferStream
BufferStream is actually several classes in one. It can serve as a buffer to another stream, packaging up several read or write operations into a single one to make them more efficient. It can act as a sink, collecting any number of writes, returning the result as a byte array. It can do offstream processing, taking a byte array that may have been read from another stream and treating it as a stream itself, or filling in an array so it can be sent off on another stream. It is of most use when used in conjunction with DataStream. Here's some examples:
Here we write a new record to a Catalog and read it back in.
BufferStream outBuffer=new BufferStream(); DataStream ds=new DataStream(outBuffer); ds.writeInt(12); ds.writeString("Hello"); ds.writeFloat(3.14); byte[] buf=bs.getBuffer(); Catalog c=new Catalog("crtr.TYPE",Catalog.WRITE_ONLY); c.addRecord(buf.length); c.writeBytes(buf,0,buf.length); c.close(); ... Catalog c=new Catalog("crtr.TYPE",Catalog.READ_ONLY); c.setRecordPos(c.getRecordCount()-1); int size=c.getRecordSize(); byte[] b=new byte[size]; c.readBytes(buf,0,buf.length); c.close(); BufferStream inBuffer=new BufferStream(buf); DataStream ds=new DataStream(inBuffer); int i=ds.readInt(); // 12 String s=ds.readString(); // Hello float f=ds.readFloat(); // 3.14
In this example we are buffering the output to a SerialPort. In this case any data we write to the BufferStream is not sent to the SerialPort until the buffer is full or flush() is called. In this case the input is still being handled through the SerialPort but the same bufferPort object could be used to read too. One point to note if you do it this way is that when it's buffer is empty, the BufferStream will attempt to read as many bytes as will fit in it's buffer, regardless of how many are asked for. This could cause problems if you want to access the underlying stream without using the BufferStream.
SerialPort port = new SerialPort(0, 9600); if (!port.isOpen()) return; BufferStream bufferPort=new BufferStream(port); byte buf[] = new byte[10]; buf[0] = 3; buf[1] = 7; bufferPort.writeBytes(buf, 0, 2); buf[0] = 4; buf[1] = 1; bufferPort.writeBytes(buf, 0, 2); bufferPort.flush(); int count = port.readBytes(buf, 0, 10); if (count == 10) ... port.close();
Field Summary | |
protected boolean |
closed
is this stream closed? |
protected boolean |
haveRead
have we read bytes from this BufferStream? |
protected boolean |
haveWritten
have we written bytes to this BufferStream? |
protected int |
initCount
the number of bytes available to read or write to initially (used by reset()) |
protected int |
initStart
the index of the array indicating the start point (used by reset()) |
protected boolean |
isGrowable
is the buffer growable (empty constructor)? |
protected byte[] |
rbuffer
the read buffer |
protected int |
readAvailable
the number of bytes available to read in the buffer |
protected int |
readPos
the array position where the next byte will be read from |
protected Stream |
stream
the underlying stream |
protected byte[] |
wbuffer
the write buffer (often points the the same array as rbuffer) |
protected int |
writeAvailable
the number of bytes of space available in the buffer to write |
protected int |
writePos
the array position where the next byte will be written to |
Constructor Summary | |
BufferStream()
Constructs a new BufferStream for writing to. |
|
BufferStream(byte[] buffer)
Constructs a new BufferStream for reading from or writing to a given byte array. |
|
BufferStream(byte[] buffer,
int start,
int count)
Constructs a new BufferStream for reading from or writing to a segment of a given byte array. |
|
BufferStream(Stream stream)
Constructs a new BufferStream for buffering input and output from a given stream. |
|
BufferStream(Stream stream,
int size)
Constructs a new BufferStream for buffering input and output from a given stream. |
Method Summary | |
boolean |
close()
Closes the stream. |
int |
flush()
For stream based buffers, sends any remaining data in the buffer. |
byte[] |
getBuffer()
Gets the buffer. |
boolean |
isOpen()
Returns true if this Stream is open and false otherwise |
int |
readBytes(byte[] buf,
int start,
int count)
Reads bytes from the stream. |
byte[] |
readBytesUntil(byte b)
Reads bytes from the stream until the stream finishes or the given byte is found. |
void |
reset()
For non stream based buffers, resets the read and write positions to their initial location. |
void |
setBuffer(byte[] buffer)
Sets the buffer used by this BufferStream. |
void |
setBuffer(byte[] buffer,
int start,
int count)
Sets the buffer slice used by this BufferStream. |
int |
writeBytes(byte[] buf,
int start,
int count)
Writes bytes to the the stream. |
Methods inherited from class java.lang.Object |
equals,
getClass,
hashCode,
notify,
toString,
wait,
wait |
Field Detail |
protected Stream stream
protected byte[] rbuffer
protected byte[] wbuffer
protected int readAvailable
protected int writeAvailable
protected int initStart
protected int initCount
protected int readPos
protected int writePos
protected boolean isGrowable
protected boolean haveRead
protected boolean haveWritten
protected boolean closed
Constructor Detail |
public BufferStream()
public BufferStream(byte[] buffer)
public BufferStream(byte[] buffer, int start, int count)
public BufferStream(Stream stream)
stream
- the stream to buffer.public BufferStream(Stream stream, int size)
stream
- the stream to buffer.size
- the size of the buffer in bytes. This is the number of
bytes that are read or written when it is empty or fullMethod Detail |
public byte[] getBuffer()
public void setBuffer(byte[] buffer)
buffer
- the buffer to use.public void setBuffer(byte[] buffer, int start, int count)
buffer
- the buffer to use.start
- the start position of the buffercount
- the number of bytes to accesspublic byte[] readBytesUntil(byte b)
b
- the byte to look forpublic int readBytes(byte[] buf, int start, int count)
buf
- the byte array to read data intostart
- the start position in the arraycount
- the number of bytes to readpublic int writeBytes(byte[] buf, int start, int count)
buf
- the byte array to write data fromstart
- the start position in the byte arraycount
- the number of bytes to writepublic int flush()
public void reset()
public boolean close()
public boolean isOpen()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |