gnu.iou
Class bbuf

java.lang.Object
  |
  +--gnu.iou.bbuf
Direct Known Subclasses:
bbp

public class bbuf
extends java.lang.Object

Byte buffer with `InputStream' and `OutputStream' emulation APIs for convenience in software using streams or buffers. Unfortunately we're not able to implement `InputStream' and `OutputStream' interfaces as both are abstract classes, not interfaces.

API

Reading and writing can occur in any order. Writing adds data, reading consumes data. Any constructor can be used with any usage.

The `reset()' method positions the read pointer to the beginning of the internal buffer, unless the `mark(int)' method has been used.

The `flush()' method positions the write pointer to the beginning of the buffer. After writing has occurred, reading will not proceed beyond the written buffer, unless we're still reading the input (constructor) buffer.

The `close()' method does both. And resets read- marking as well.

Not MT Safe

This class is not synchronized, as most applications of most classes have only one thread accessing an object and are penalized in runtime performance by utilities that are multithread- safe (synchronized).

Multithreaded applications of this class must provide their own multi- thread safety (synchronization).

Author:
John Pritchard (john@syntelos.org)
See Also:
bbo, bbi, dbo

Constructor Summary
bbuf()
          Output/ write constructor.
bbuf(byte[] input)
          Input/ read constructor.
bbuf(java.io.InputStream in)
          Copy input into buffer until EOF
bbuf(int gf)
           
 
Method Summary
 void append(boolean[] ary)
           
 void append(double[] ary, bbuf bb)
           
 void append(float[] ary, bbuf bb)
           
 void append(int[] ary)
           
 void append(long[] ary)
           
 void append(java.lang.Object o)
           
 void append(java.lang.Object[] oary)
           
 void append(short[] ary)
           
 int available()
          Bytes available for reading.
 int buf_gf(int gf)
          Get or set internal buffer growth factor.
 java.lang.String bufString()
          Naively construct a string on a non- null buffer, otherwise return null for an empty buffer.
static byte[] cat(byte[] a, byte[] b)
           
 void close()
          Reset reading, writing and "read mark" to the start.
 int copyOutArray(java.io.OutputStream out)
          Unsynchronized raw buffer copy to output stream using a loop for block devices, etc..
 int copyOutLoop(java.io.OutputStream out)
          Unsynchronized raw buffer copy to output stream using a loop for TCP based destinations.
static void debugPrint(byte[] buf, int ofs, int len, java.io.PrintStream out)
           
 void destroy()
          Discard the internal buffer.
 byte[] dump()
          Return a copy of the internal buffer, verbatim.
 void flush()
          Reset reading, writing and "read mark" to the head of the current buffer.
static byte[] growbuf(byte[] src, int to_idx)
          Array stretch function.
 int length()
          Same as `available()'.
 void mark()
          Sets "reset" (writing) mark to current position.
 void mark(int readlimit)
          Sets "reset" (writing) mark to the current position, ignoring "readlimit" because this is a buffer -- we don't need a buffer size.
 byte[] markedBits()
          Return bytes written since last "mark".
 boolean markSupported()
           
 int nwrite(byte ch, int many)
          Repeat the byte into the buffer.
 void print(java.lang.String s)
          Encode string in UTF-8 and append to buffer.
 void println()
          Write CRLF newline to buffer.
 void println(java.lang.Object obj)
          Encode to string (UTF-8) with CRLF newline.
 void println(java.lang.Object[] objs)
          Add CRLF after each.
 void println(java.lang.String s)
          Encode string with CRLF newline in UTF-8 and append to buffer.
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
          Copy into buffer as many as buffer- length bytes, or available bytes.
 short read2()
          Read two bytes into a short.
 int read4()
          Read four bytes into an int using network byte order (big endian).
 long read8()
          Read eight bytes into a long using network byte order (big endian).
 void reset()
          Reset writing back to first byte of the current buffer, or to the last marked position.
 long skip(long n)
          Skip bytes, or available bytes.
 byte[] toByteArray()
          Copy readable (available) bytes from buffer.
static byte[] toByteArray(java.lang.Object obj)
           
static byte[] toByteArray(java.lang.Object[] oary)
           
static bbuf toByteArray(java.lang.Object[] objs, bbuf bbu)
           
static bbuf toByteArray(java.lang.Object obj, bbuf bbu)
           
 java.lang.String toString()
          Pass the available bits through UTF-8
 int write(byte[] b)
           
 int write(byte[] b, int off, int len)
           
 void write(int b)
           
 void write2(short b)
          Write 16 bits in big endian network byte order.
 void write4(int b)
          Write 32 bits in big endian network byte order.
 void write8(long b)
          Write 64 bits in big endian network byte order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

bbuf

public bbuf(byte[] input)
Input/ read constructor.
Parameters:
input - Read- available data (not an empty buffer!)

bbuf

public bbuf(java.io.InputStream in)
     throws java.io.IOException
Copy input into buffer until EOF

bbuf

public bbuf()
Output/ write constructor.

bbuf

public bbuf(int gf)
Parameters:
gf - Internal buffer growth factor, set initial buffer size to gf.
Method Detail

buf_gf

public final int buf_gf(int gf)
Get or set internal buffer growth factor. Default 1024.
Parameters:
gf - If greater than zero, set, otherwise just return current value.

read

public int read()

read

public int read(byte[] b)

read

public int read(byte[] b,
                int off,
                int len)
Copy into buffer as many as buffer- length bytes, or available bytes.
Parameters:
b - Non null buffer to copy into.
off - Valid offset in buffer `b' to copy to.
len - Valid number of bytes to copy into `b', usually `b.length'.

read2

public short read2()
            throws java.io.IOException
Read two bytes into a short.
Throws:
java.io.IOException - If two bytes are not available.

read4

public int read4()
          throws java.io.IOException
Read four bytes into an int using network byte order (big endian).
Throws:
java.io.IOException - If four bytes are not available.

read8

public long read8()
           throws java.io.IOException
Read eight bytes into a long using network byte order (big endian).
Throws:
java.io.IOException - If eight bytes are not available.

skip

public long skip(long n)
Skip bytes, or available bytes.
Parameters:
n - Number of bytes to skip.

available

public int available()
Bytes available for reading. (This is also the internal reference for bytes available for the read interface.)

reset

public void reset()
Reset writing back to first byte of the current buffer, or to the last marked position. Also reset the writing mark to zero so that two (or more) calls to reset clears any mark.

mark

public void mark(int readlimit)
Sets "reset" (writing) mark to the current position, ignoring "readlimit" because this is a buffer -- we don't need a buffer size.
See Also:
The readlimit argument is ignored because this is a buffer

markSupported

public boolean markSupported()

mark

public void mark()
Sets "reset" (writing) mark to current position. Identical to OutputStream's `mark(int)'.

markedBits

public byte[] markedBits()
Return bytes written since last "mark".

write2

public void write2(short b)
Write 16 bits in big endian network byte order.
Parameters:
b - 16 bits

write4

public void write4(int b)
Write 32 bits in big endian network byte order.
Parameters:
b - 32 bits

write8

public void write8(long b)
Write 64 bits in big endian network byte order.
Parameters:
b - 64 bits

write

public void write(int b)
Parameters:
b - Eight bit byte value

write

public int write(byte[] b)
Parameters:
b - Non null buffer to copy into the internal buffer.

write

public int write(byte[] b,
                 int off,
                 int len)
Parameters:
b - Non null input buffer to copy into the internal buffer.
off - Offset in input buffer `b' from which to copy
len - Number of bytes to copy from input buffer `b'.

nwrite

public int nwrite(byte ch,
                  int many)
Repeat the byte into the buffer.

print

public void print(java.lang.String s)
Encode string in UTF-8 and append to buffer. Return number of bytes appended to buffer.

println

public void println(java.lang.String s)
Encode string with CRLF newline in UTF-8 and append to buffer.

println

public void println()
Write CRLF newline to buffer.

println

public void println(java.lang.Object obj)
Encode to string (UTF-8) with CRLF newline.

println

public void println(java.lang.Object[] objs)
Add CRLF after each.

append

public void append(boolean[] ary)

append

public void append(short[] ary)

append

public void append(int[] ary)

append

public void append(long[] ary)

append

public void append(float[] ary,
                   bbuf bb)

append

public void append(double[] ary,
                   bbuf bb)

append

public void append(java.lang.Object[] oary)

append

public void append(java.lang.Object o)

flush

public void flush()
Reset reading, writing and "read mark" to the head of the current buffer. (Same as "close".)

close

public void close()
Reset reading, writing and "read mark" to the start. (Same as "flush".)

bufString

public final java.lang.String bufString()
Naively construct a string on a non- null buffer, otherwise return null for an empty buffer. Uses `toByteArray()'.
See Also:
toByteArray()

toByteArray

public final byte[] toByteArray()
Copy readable (available) bytes from buffer. If there are no available bytes, return null. The same bytes remain readable (available). Has no effect on the state of reading or writing.

dump

public final byte[] dump()
Return a copy of the internal buffer, verbatim.

destroy

public final void destroy()
Discard the internal buffer. Any calls on this object will produce null pointer exceptions after this method has been called.

length

public int length()
Same as `available()'.

copyOutLoop

public int copyOutLoop(java.io.OutputStream out)
                throws java.io.IOException
Unsynchronized raw buffer copy to output stream using a loop for TCP based destinations. This should not be done at the same time as read and write ops on this buffer.

Copies from present read point without changing the read or write points.


copyOutArray

public int copyOutArray(java.io.OutputStream out)
                 throws java.io.IOException
Unsynchronized raw buffer copy to output stream using a loop for block devices, etc.. This should not be done at the same time as read and write ops on this buffer.

Copies from present read point without changing the read or write points.


toString

public java.lang.String toString()
Pass the available bits through UTF-8
Overrides:
toString in class java.lang.Object

growbuf

public static final byte[] growbuf(byte[] src,
                                   int to_idx)
Array stretch function.
Parameters:
src - Array, null or extant.
to_idx - Index that must be accomodated in the extent of the source array.

toByteArray

public static final byte[] toByteArray(java.lang.Object[] oary)

toByteArray

public static final byte[] toByteArray(java.lang.Object obj)

toByteArray

public static final bbuf toByteArray(java.lang.Object obj,
                                     bbuf bbu)

toByteArray

public static final bbuf toByteArray(java.lang.Object[] objs,
                                     bbuf bbu)

cat

public static final byte[] cat(byte[] a,
                               byte[] b)

debugPrint

public static final void debugPrint(byte[] buf,
                                    int ofs,
                                    int len,
                                    java.io.PrintStream out)