Package com.itextpdf.text.pdf
Class ByteBuffer
- java.lang.Object
-
- java.io.OutputStream
-
- com.itextpdf.text.pdf.ByteBuffer
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
public class ByteBuffer extends OutputStream
Acts like aStringBuffer
but works withbyte
arrays. Floating point is converted to a format suitable to the PDF.- Author:
- Paulo Soares
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
buf
The buffer where the bytes are stored.protected int
count
The count of bytes in the buffer.static boolean
HIGH_PRECISION
Iftrue
always output floating point numbers with 6 decimal digits.static byte
ZERO
-
Constructor Summary
Constructors Constructor Description ByteBuffer()
Creates new ByteBuffer with capacity 128ByteBuffer(int size)
Creates a byte buffer with a certain capacity.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteBuffer
append(byte b)
ByteBuffer
append(byte[] b)
Appends an array of bytes.ByteBuffer
append(byte[] b, int off, int len)
Appends the subarray of thebyte
array.ByteBuffer
append(char c)
Appends achar
to the buffer.ByteBuffer
append(double d)
Appends a string representation of adouble
according to the Pdf conventions.ByteBuffer
append(float i)
Appends a string representation of afloat
according to the Pdf conventions.ByteBuffer
append(int i)
Appends the string representation of anint
.ByteBuffer
append(long i)
Appends the string representation of along
.ByteBuffer
append(ByteBuffer buf)
Appends anotherByteBuffer
to this buffer.ByteBuffer
append(String str)
Appends aString
to the buffer.ByteBuffer
append_i(int b)
Appends anint
.ByteBuffer
appendHex(byte b)
static void
fillCache(int decimals)
You can fill the cache in advance if you want to.static String
formatDouble(double d)
Outputs adouble
into a format suitable for the PDF.static String
formatDouble(double d, ByteBuffer buf)
Outputs adouble
into a format suitable for the PDF.byte[]
getBuffer()
void
reset()
Sets the size to zero.static void
setCacheSize(int size)
Sets the cache size.void
setSize(int size)
int
size()
Returns the current size of the buffer.byte[]
toByteArray()
Creates a newly allocated byte array.String
toString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.String
toString(String enc)
Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.void
write(byte[] b, int off, int len)
void
write(int b)
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 usingout.write(buf, 0, count)
.-
Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write
-
-
-
-
Field Detail
-
count
protected int count
The count of bytes in the buffer.
-
buf
protected byte[] buf
The buffer where the bytes are stored.
-
ZERO
public static final byte ZERO
- See Also:
- Constant Field Values
-
HIGH_PRECISION
public static boolean HIGH_PRECISION
Iftrue
always output floating point numbers with 6 decimal digits. Iffalse
uses the faster, although less precise, representation.
-
-
Method Detail
-
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
-
fillCache
public static void fillCache(int decimals)
You can fill the cache in advance if you want to.- Parameters:
decimals
-
-
append_i
public ByteBuffer append_i(int b)
Appends anint
. The size of the array will grow by one.- Parameters:
b
- the int to be appended- Returns:
- a reference to this
ByteBuffer
object
-
append
public ByteBuffer append(byte[] b, int off, int len)
Appends the subarray of thebyte
array. The buffer will grow bylen
bytes.- Parameters:
b
- the array to be appendedoff
- the offset to the start of the arraylen
- 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 aString
to the buffer. TheString
is converted according to the encoding ISO-8859-1.- Parameters:
str
- theString
to be appended- Returns:
- a reference to this
ByteBuffer
object
-
append
public ByteBuffer append(char c)
Appends achar
to the buffer. Thechar
is converted according to the encoding ISO-8859-1.- Parameters:
c
- thechar
to be appended- Returns:
- a reference to this
ByteBuffer
object
-
append
public ByteBuffer append(ByteBuffer buf)
Appends anotherByteBuffer
to this buffer.- Parameters:
buf
- theByteBuffer
to be appended- Returns:
- a reference to this
ByteBuffer
object
-
append
public ByteBuffer append(int i)
Appends the string representation of anint
.- Parameters:
i
- theint
to be appended- Returns:
- a reference to this
ByteBuffer
object
-
append
public ByteBuffer append(long i)
Appends the string representation of along
.- Parameters:
i
- thelong
to be appended- Returns:
- a reference to this
ByteBuffer
object
-
append
public ByteBuffer append(byte b)
-
appendHex
public ByteBuffer appendHex(byte b)
-
append
public ByteBuffer append(float i)
Appends a string representation of afloat
according to the Pdf conventions.- Parameters:
i
- thefloat
to be appended- Returns:
- a reference to this
ByteBuffer
object
-
append
public ByteBuffer append(double d)
Appends a string representation of adouble
according to the Pdf conventions.- Parameters:
d
- thedouble
to be appended- Returns:
- a reference to this
ByteBuffer
object
-
formatDouble
public static String formatDouble(double d)
Outputs adouble
into a format suitable for the PDF.- Parameters:
d
- a double- Returns:
- the
String
representation of thedouble
-
formatDouble
public static String formatDouble(double d, ByteBuffer buf)
Outputs adouble
into a format suitable for the PDF.- Parameters:
d
- a doublebuf
- a ByteBuffer- Returns:
- the
String
representation of thedouble
ifbuf
isnull
. Ifbuf
is notnull
, then the double is appended directly to the buffer and this methods returnsnull
.
-
reset
public void reset()
Sets the size to zero.
-
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.
-
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.
-
setSize
public void setSize(int size)
-
toString
public String toString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
-
toString
public String toString(String enc) throws UnsupportedEncodingException
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.
-
writeTo
public void writeTo(OutputStream out) throws IOException
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 usingout.write(buf, 0, count)
.- Parameters:
out
- the output stream to which to write the data.- Throws:
IOException
- if an I/O error occurs.
-
write
public void write(int b) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b, int off, int len)
- Overrides:
write
in classOutputStream
-
getBuffer
public byte[] getBuffer()
-
-