com.lowagie.text.pdf

Class ByteBuffer

public class ByteBuffer extends OutputStream

Acts like a StringBuffer but works with byte arrays. Floating point is converted to a format suitable to the PDF.

Author: Paulo Soares (psoares@consiste.pt)

Field Summary
protected byte[]buf
The buffer where the bytes are stored.
static byte[][]byteCache
static intbyteCacheSize
static byte[]bytes
static char[]chars
protected intcount
The count of bytes in the buffer.
static DecimalFormatSymbolsdfs
static booleanHIGH_PRECISION
If true always output floating point numbers with 6 decimal digits.
static byteZERO
Constructor Summary
ByteBuffer()
Creates new ByteBuffer with capacity 128
ByteBuffer(int size)
Creates a byte buffer with a certain capacity.
Method Summary
ByteBufferappend(byte[] b, int off, int len)
Appends the subarray of the byte array.
ByteBufferappend(byte[] b)
Appends an array of bytes.
ByteBufferappend(String str)
Appends a String to the buffer.
ByteBufferappend(char c)
Appends a char to the buffer.
ByteBufferappend(ByteBuffer buf)
Appends another ByteBuffer to this buffer.
ByteBufferappend(int i)
Appends the string representation of an int.
ByteBufferappend(byte b)
ByteBufferappend(float i)
Appends a string representation of a float according to the Pdf conventions.
ByteBufferappend(double d)
Appends a string representation of a double according to the Pdf conventions.
ByteBufferappendHex(byte b)
ByteBufferappend_i(int b)
Appends an int.
static byte[]convertToBytes(int i)
Converts an double (multiplied by 100 and cast to an int) into an array of bytes.
static voidfillCache(int decimals)
You can fill the cache in advance if you want to.
static StringformatDouble(double d)
Outputs a double into a format suitable for the PDF.
static StringformatDouble(double d, ByteBuffer buf)
Outputs a double into a format suitable for the PDF.
byte[]getBuffer()
voidreset()
Sets the size to zero.
static voidsetCacheSize(int size)
Sets the cache size.
voidsetSize(int size)
intsize()
Returns the current size of the buffer.
byte[]toByteArray()
Creates a newly allocated byte array.
StringtoString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
StringtoString(String enc)
Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.
voidwrite(int b)
voidwrite(byte[] b, int off, int len)
voidwriteTo(OutputStream out)
Writes the complete contents of this byte buffer output to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

Field Detail

buf

protected byte[] buf
The buffer where the bytes are stored.

byteCache

private static byte[][] byteCache

byteCacheSize

private static int byteCacheSize

bytes

private static final byte[] bytes

chars

private static final char[] chars

count

protected int count
The count of bytes in the buffer.

dfs

private static final DecimalFormatSymbols dfs

HIGH_PRECISION

public static boolean HIGH_PRECISION
If true always output floating point numbers with 6 decimal digits. If false uses the faster, although less precise, representation.

ZERO

public static final byte ZERO

Constructor Detail

ByteBuffer

public ByteBuffer()
Creates new ByteBuffer with capacity 128

ByteBuffer

public ByteBuffer(int size)
Creates a byte buffer with a certain capacity.

Parameters: size the initial capacity

Method Detail

append

public ByteBuffer append(byte[] b, int off, int len)
Appends the subarray of the byte array. The buffer will grow by len bytes.

Parameters: b the array to be appended off the offset to the start of the array len the length of bytes to append

Returns: a reference to this ByteBuffer object

append

public ByteBuffer append(byte[] b)
Appends an array of bytes.

Parameters: b the array to be appended

Returns: a reference to this ByteBuffer object

append

public ByteBuffer append(String str)
Appends a String to the buffer. The String is converted according to the encoding ISO-8859-1.

Parameters: str the String to be appended

Returns: a reference to this ByteBuffer object

append

public ByteBuffer append(char c)
Appends a char to the buffer. The char is converted according to the encoding ISO-8859-1.

Parameters: c the char to be appended

Returns: a reference to this ByteBuffer object

append

public ByteBuffer append(ByteBuffer buf)
Appends another ByteBuffer to this buffer.

Parameters: buf the ByteBuffer to be appended

Returns: a reference to this ByteBuffer object

append

public ByteBuffer append(int i)
Appends the string representation of an int.

Parameters: i the int to be appended

Returns: a reference to this ByteBuffer object

append

public ByteBuffer append(byte b)

append

public ByteBuffer append(float i)
Appends a string representation of a float according to the Pdf conventions.

Parameters: i the float to be appended

Returns: a reference to this ByteBuffer object

append

public ByteBuffer append(double d)
Appends a string representation of a double according to the Pdf conventions.

Parameters: d the double to be appended

Returns: a reference to this ByteBuffer object

appendHex

public ByteBuffer appendHex(byte b)

append_i

public ByteBuffer append_i(int b)
Appends an int. The size of the array will grow by one.

Parameters: b the int to be appended

Returns: a reference to this ByteBuffer object

convertToBytes

private static byte[] convertToBytes(int i)
Converts an double (multiplied by 100 and cast to an int) into an array of bytes.

Parameters: i the int

Returns: a bytearray

fillCache

public static void fillCache(int decimals)
You can fill the cache in advance if you want to.

Parameters: decimals

formatDouble

public static String formatDouble(double d)
Outputs a double into a format suitable for the PDF.

Parameters: d a double

Returns: the String representation of the double

formatDouble

public static String formatDouble(double d, ByteBuffer buf)
Outputs a double into a format suitable for the PDF.

Parameters: d a double buf a ByteBuffer

Returns: the String representation of the double if buf is null. If buf is not null, then the double is appended directly to the buffer and this methods returns null.

getBuffer

public byte[] getBuffer()

reset

public void reset()
Sets the size to zero.

setCacheSize

public static void setCacheSize(int size)
Sets the cache size.

This can only be used to increment the size. If the size that is passed through is smaller than the current size, nothing happens.

Parameters: size the size of the cache

setSize

public void setSize(int size)

size

public int size()
Returns the current size of the buffer.

Returns: the value of the count field, which is the number of valid bytes in this byte buffer.

toByteArray

public byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.

Returns: the current contents of this output stream, as a byte array.

toString

public String toString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.

Returns: String translated from the buffer's contents.

toString

public String toString(String enc)
Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.

Parameters: enc a character-encoding name.

Returns: String translated from the buffer's contents.

Throws: UnsupportedEncodingException If the named encoding is not supported.

write

public void write(int b)

write

public void write(byte[] b, int off, int len)

writeTo

public void writeTo(OutputStream out)
Writes the complete contents of this byte buffer output to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

Parameters: out the output stream to which to write the data.

Throws: IOException if an I/O error occurs.