|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.hunnysoft.jmime.ByteStringBuffer
Class that represents a mutable sequence of single-byte characters or bytes.
This class works together with ByteString in the same
way that StringBuffer works with String. The
difference between StringBuffer and
ByteStringBuffer is that StringBuffer contains
character data only, using 16-bit Unicode code points, while
ByteStringBuffer contains 8-bit character data or
non-character byte data.
This class is necessary, because instances of ByteString
are immutable. Therefore, to create a ByteString by
concatenating several sequences of bytes, it is much more efficient to
first create an instance of ByteStringBuffer, perform all
the concatenation operations on it, and in the end convert it to an
immutable instance of ByteString. When an instance of
ByteStringBuffer is converted to an instance of
ByteString by calling the
ByteStringBuffer.toByteString() method, the internal byte
array is passed directly to the new ByteString instance,
making this operation very efficient.
Another difference between StringBuffer and
ByteStringBuffer is that ByteStringBuffer does
not have any synchronized methods. The lack of synchronized methods
means that the methods will execute more quickly. However, it also
means that you must not access a ByteStringBuffer from two
different threads.
Important: This class is not intended for
general use outside of Hunny JMIME. It is highly specialized, and is
designed for maximum efficiency while sacrificing some safety features.
The correct way -- that is, the safe way -- to use
ByteStringBuffer is as follows:
ByteStringBuffer, preferrably
indicating the initial size of the internal byte array. ByteStringBuffer
ByteStringBuffer.toByteString() to create a
ByteString that contains the contents of the
ByteStringBuffer ByteStringBuffer to
null so that it can no longer be used. (It is now unsafe
to use the ByteStringBuffer, because it shares an internal
byte array with the ByteString.)
| Constructor Summary | |
ByteStringBuffer()
Default constructor. |
|
ByteStringBuffer(ByteString bstr)
Constructor with initial value taken from a byte string. |
|
ByteStringBuffer(int length)
Constructor that takes initial length of internal byte array. |
|
| Method Summary | |
ByteStringBuffer |
append(byte c)
Appends a single byte to this buffer. |
ByteStringBuffer |
append(byte[] bytes)
Appends a sequence of bytes to this buffer. |
ByteStringBuffer |
append(byte[] bytes,
int offset,
int length)
Appends a sequence of bytes to this buffer. |
ByteStringBuffer |
append(ByteString bstr)
Appends a byte string to this buffer. |
ByteStringBuffer |
append(char c)
Appends a single character to this buffer. |
ByteStringBuffer |
append(double x)
Appends a double value to this buffer. |
ByteStringBuffer |
append(float f)
Appends a float value to this buffer. |
ByteStringBuffer |
append(int i)
Appends an integer to this buffer. |
ByteStringBuffer |
append(long n)
Appends a long integer to this buffer. |
ByteStringBuffer |
append(java.lang.Object obj)
Appends an object to this buffer. |
ByteStringBuffer |
append(java.lang.String str)
Appends a string to this buffer. |
int |
capacity()
Returns the capacity of the internal byte array. |
void |
ensureCapacity(int n)
Resizes internal byte array. |
int |
length()
Returns the length of the buffer. |
void |
setLength(int newLength)
Sets the length of the buffer. |
ByteString |
toByteString()
Returns byte string with same contents as this buffer. |
java.lang.String |
toString()
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public ByteStringBuffer()
public ByteStringBuffer(int length)
If you know the size your buffer will eventually reach, you can eliminate the copying associated with resizing the internal byte array by specifying the initial size using this constructor.
length - desired initial length of internal byte arraypublic ByteStringBuffer(ByteString bstr)
bstr - string to set as the initial value of the buffer| Method Detail |
public ByteStringBuffer append(java.lang.Object obj)
Internally, this method converts the object to a
String, then converts the String to bytes
assuming the ISO-8859-1 character set.
obj - object to append
public ByteStringBuffer append(ByteString bstr)
bstr - byte string to append
public ByteStringBuffer append(java.lang.String str)
This string is converted to bytes assuming the ISO-8859-1 character set.
str - the string to append
public ByteStringBuffer append(byte[] bytes)
bytes - array of bytes to append
public ByteStringBuffer append(byte[] bytes,
int offset,
int length)
bytes - array containing the bytes to appendoffset - offset of first byte in bytes to appendlength - number of bytes to append
public ByteStringBuffer append(byte c)
c - the byte to append
public ByteStringBuffer append(char c)
The character should be a character from the ISO-8859-1 characters set; that is, it should have a value between 0 and 256. The result of this method for a character that is not an ISO-8859-1 character is undefined.
c - the character to append
public ByteStringBuffer append(int i)
i - the integer to append
public ByteStringBuffer append(long n)
n - long integer to append
public ByteStringBuffer append(float f)
f - float value to append
public ByteStringBuffer append(double x)
x - double value to append
public int capacity()
public void ensureCapacity(int n)
If the internal byte array is smaller than the specified size, then this method will resize the internal byte array to a value that is at least the specified size.
n - requested size of interal byte arraypublic int length()
public void setLength(int newLength)
java.lang.IndexOutOfBoundsException - - if the argument is negativepublic ByteString toByteString()
This method creates a new instance of ByteString
that has the same contents as this buffer. The method passes the
internal byte array directly to the new ByteString
instance, making this operation very efficient.
After this method is called, the ByteStringBuffer
should not be used again. (It is wise to enforce this by immediately
setting the reference to null.)
public java.lang.String toString()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||