Package netscape.ldap.util
Class ByteBuf
java.lang.Object
netscape.ldap.util.ByteBuf
- All Implemented Interfaces:
Serializable
This class is similar to the
java.lang.StringBuffer
class. Instead of storing a string, an object of this class
stores an array of bytes. (This is referred to as a "byte buffer".)
This class also differs from StringBuffer in the
following ways:
- None of the methods are synchronized. You cannot share a byte buffer among multiple threads.
- Converting to a String requires a copy of the character data.
- In order to speed up memory allocation,
AllocandRecyclemethods are provided. You can "recycle" anyByteBufobjects you no longer need by using theRecyclemethod. Calling theAllocmethod will reuse objects that have been "recycled." To To clear out the cache of these "recycled" objects, use theEmptyRecyclermethod. - Additional "helper" methods are provided (for example, functions for comparing data).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int(package private) static final longprivate byte[] -
Constructor Summary
ConstructorsConstructorDescriptionByteBuf()Constructs an empty byte buffer with the default length of 16.ByteBuf(byte[] bytes, int offset, int length) Constructs a byte buffer with the specified length.ByteBuf(int length) Constructs an empty byte buffer with the specified initial length.Constructs a byte buffer with the specified initial value. -
Method Summary
Modifier and TypeMethodDescriptionappend(boolean b) Appends a boolean to the end of this byte buffer.append(byte b) Appends a byte to the end of this byte buffer.append(byte[] str) Appends an array of bytes to the end of this byte buffer.append(byte[] str, int offset, int len) Appends a part of an array of bytes to the end of this byte buffer.append(double d) Appends adoubleto the end of this byte buffer.append(float f) Appends afloatto the end of this byte buffer.append(int i) Appends an integer to the end of this byte buffer.append(long l) Appends alongvalue to the end of this byte buffer.Appends an object to the end of this byte buffer.Appends a string to the end of this byte buffer.Appends a byte buffer to the end of this byte buffer.bytebyteAt(int index) Returns the byte at the specified index.intcapacity()Returns the current capacity of the byte buffer.voidensureCapacity(int minimumCapacity) Ensures that the capacity of the buffer is at least equal to the specified minimum capacity.voidgetBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) Copies the bytes (from the section of the byte buffer from the indexsrcBeginto the indexsrcEnd - 1) into the specified byte array.intlength()Returns the length (character count) of the byte buffer.intread(InputStream file, int max_bytes) Invokes theInputStream.readmethod and appends the the bytes read to this byte buffer.intread(RandomAccessFile file, int max_bytes) Invokes theRandomAccessFile.readmethod, appending the bytes read to this byte buffer.voidsetByteAt(int index, byte b) Sets the value of the byte at the specified index.voidsetLength(int newLength) Sets the length of the byte buffer.byte[]toBytes()Returns the data in the byte buffer as a byte array.toString()Returns the data in the byte buffer to a string.voidwrite(OutputStream out) Writes the contents of the byte buffer to the specified output stream.voidwrite(RandomAccessFile out) Writes the contents of the byte buffer to the specifiedRandomAccessFileobject.
-
Field Details
-
serialVersionUID
static final long serialVersionUID- See Also:
-
value
private byte[] value -
count
private int count
-
-
Constructor Details
-
ByteBuf
public ByteBuf()Constructs an empty byte buffer with the default length of 16. -
ByteBuf
public ByteBuf(int length) Constructs an empty byte buffer with the specified initial length.- Parameters:
length- initial number of bytes that this buffer should hold
-
ByteBuf
Constructs a byte buffer with the specified initial value. The new byte buffer is 16 bytes longer than the initial value.- Parameters:
str- initial value that this buffer should hold
-
ByteBuf
public ByteBuf(byte[] bytes, int offset, int length) Constructs a byte buffer with the specified length. An initial value is stored in this buffer, starting at the specified offset.- Parameters:
bytes- array of bytes to initially store in the bufferoffset- index where you want the initial value to start in the arraylength- length of the buffer to allocate
-
-
Method Details
-
length
public int length()Returns the length (character count) of the byte buffer. -
capacity
public int capacity()Returns the current capacity of the byte buffer. The capacity is the amount of storage available for newly inserted bytes. If the capacity is exceeded, more space will be allocated. -
ensureCapacity
public void ensureCapacity(int minimumCapacity) Ensures that the capacity of the buffer is at least equal to the specified minimum capacity.- Parameters:
minimumCapacity- the minimum number of bytes that you want the byte buffer to hold
-
setLength
public void setLength(int newLength) Sets the length of the byte buffer. If you set a length that is shorter than the current length, bytes at the end of the buffer are lost. If you increase the length of the buffer, the values of the new bytes in the buffer are set to 0.- Parameters:
newLength- the new length of the buffer- Throws:
StringIndexOutOfBoundsException- You have specified an invalid length.
-
byteAt
public byte byteAt(int index) Returns the byte at the specified index. The value of an index can range from 0 to length - 1.- Parameters:
index- index of the byte to find- Throws:
StringIndexOutOfBoundsException- You have specified an invalid index.
-
getBytes
public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) Copies the bytes (from the section of the byte buffer from the indexsrcBeginto the indexsrcEnd - 1) into the specified byte array. The copied bytes are inserted in the byte array at the index specified bydstBegin. BothsrcBeginandsrcEndmust be valid indexes in the buffer.- Parameters:
srcBegin- index identifying the start of the section of the byte buffer to copysrcEnd- index identifying the end of the section of the byte buffer to copy. (Copy all bytes before the byte identified by this index.)dst- the byte array to copy the data todstBegin- index of the byte array identifying the location to which the byte buffer is copied- Throws:
StringIndexOutOfBoundsException- You specified an invalid index into the buffer.
-
setByteAt
public void setByteAt(int index, byte b) Sets the value of the byte at the specified index.- Parameters:
index- the index of the byte to setb- the new value to set- Throws:
StringIndexOutOfBoundsException- You have specified an invalid index.
-
append
Appends an object to the end of this byte buffer.- Parameters:
obj- the object to append to the buffer- Returns:
- the same
ByteBufobject (not a new object) with the string representation of the specified object appended.
-
append
Appends a string to the end of this byte buffer. This method appends one byte for each character in the string. The upper 8 bits of the character are stripped off.If you want to retain all bits in the character (not just the lower 8 bits), use
append(String.getBytes())instead.- Parameters:
str- the string that you want appended to the buffer- Returns:
- the same
ByteBufobject (not a new object) with the specified string appended
-
append
Appends an array of bytes to the end of this byte buffer.- Parameters:
str- the array of bytes to append to this buffer- Returns:
- the same
ByteBufobject (not a new object) with the specified bytes appended.
-
append
Appends a part of an array of bytes to the end of this byte buffer.- Parameters:
str- the array of bytes to append to this bufferoffset- the index in the array marking the start of the section to copylen- the number of bytes to add- Returns:
- the same
ByteBufobject (not a new object) with the specified section of the byte array appended
-
append
Appends a byte buffer to the end of this byte buffer.- Parameters:
buf- the byte buffer to append to this buffer- Returns:
- the original
ByteBufobject (not a new object) with bytes from the specified byte buffer appended.
-
append
Appends a boolean to the end of this byte buffer.- Parameters:
b- the boolean value that you want appended to this buffer- Returns:
- the original
ByteBufobject (not a new object) with bytes from the string representation of the boolean value appended.
-
append
Appends a byte to the end of this byte buffer.- Parameters:
b- the byte to append to this buffer- Returns:
- the original
ByteBufobject (not a new object) with the specified byte appended.
-
append
Appends an integer to the end of this byte buffer.- Parameters:
i- the integer to append to this byte buffer- Returns:
- the original
ByteBufobject (not a new object) with the string representation of the specified integer appended.
-
append
Appends alongvalue to the end of this byte buffer.- Parameters:
l- thelongvalue to append to this buffer- Returns:
- the original
ByteBufobject (not a new object) with the string representation of the specifiedlongvalue appended.
-
append
Appends afloatto the end of this byte buffer.- Parameters:
f- thefloatvalue to append to this buffer- Returns:
- the original
ByteBufobject (not a new object) with the string representation of the specifiedfloatvalue appended.
-
append
Appends adoubleto the end of this byte buffer.- Parameters:
d- thedoublevalue to append to this buffer- Returns:
- the original
ByteBufobject (not a new object) with the string representation of the specifieddoublevalue appended.
-
toString
Returns the data in the byte buffer to a string. -
toBytes
public byte[] toBytes()Returns the data in the byte buffer as a byte array.- Returns:
- the byte array containing data in the byte buffer.
-
read
Invokes theInputStream.readmethod and appends the the bytes read to this byte buffer.- Parameters:
file- the input stream from which to read the bytesmax_bytes- the maximum number of bytes to read into the byte buffer- Returns:
- the number of bytes read, or -1 if there is no more data to read.
- Throws:
IOException- An I/O error has occurred.
-
read
Invokes theRandomAccessFile.readmethod, appending the bytes read to this byte buffer.- Parameters:
file- theRandomAccessFileobject from which to read the bytesmax_bytes- the maximum number of bytes to read into the byte buffer- Returns:
- the number of bytes read, or -1 if there is no more data to read.
- Throws:
IOException- An I/O error has occurred.
-
write
Writes the contents of the byte buffer to the specified output stream.- Parameters:
out- the output stream- Throws:
IOException- An I/O error has occurred.
-
write
Writes the contents of the byte buffer to the specifiedRandomAccessFileobject.- Parameters:
out- theRandomAccessFileobject dexception IOException An I/O error has occurred.- Throws:
IOException
-