- java.lang.Object
-
- java.io.OutputStream
-
- com.itextpdf.text.pdf.ByteBuffer
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class ByteBuffer extends java.io.OutputStreamActs like aStringBufferbut works withbytearrays. Floating point is converted to a format suitable to the PDF.- Author:
- Paulo Soares (psoares@consiste.pt)
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufThe buffer where the bytes are stored.protected intcountThe count of bytes in the buffer.static booleanHIGH_PRECISIONIftruealways output floating point numbers with 6 decimal digits.static byteZERO
-
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 ByteBufferappend(byte b)ByteBufferappend(byte[] b)Appends an array of bytes.ByteBufferappend(byte[] b, int off, int len)Appends the subarray of thebytearray.ByteBufferappend(char c)Appends acharto the buffer.ByteBufferappend(double d)Appends a string representation of adoubleaccording to the Pdf conventions.ByteBufferappend(float i)Appends a string representation of afloataccording to the Pdf conventions.ByteBufferappend(int i)Appends the string representation of anint.ByteBufferappend(ByteBuffer buf)Appends anotherByteBufferto this buffer.ByteBufferappend(java.lang.String str)Appends aStringto the buffer.ByteBufferappend_i(int b)Appends anint.ByteBufferappendHex(byte b)static voidfillCache(int decimals)You can fill the cache in advance if you want to.static java.lang.StringformatDouble(double d)Outputs adoubleinto a format suitable for the PDF.static java.lang.StringformatDouble(double d, ByteBuffer buf)Outputs adoubleinto 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.java.lang.StringtoString()Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.java.lang.StringtoString(java.lang.String enc)Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.voidwrite(byte[] b, int off, int len)voidwrite(int b)voidwriteTo(java.io.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).
-
-
-
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
Iftruealways output floating point numbers with 6 decimal digits. Iffalseuses 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
ByteBufferobject
-
append
public ByteBuffer append(byte[] b, int off, int len)
Appends the subarray of thebytearray. The buffer will grow bylenbytes.- 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
ByteBufferobject
-
append
public ByteBuffer append(byte[] b)
Appends an array of bytes.- Parameters:
b- the array to be appended- Returns:
- a reference to this
ByteBufferobject
-
append
public ByteBuffer append(java.lang.String str)
Appends aStringto the buffer. TheStringis converted according to the encoding ISO-8859-1.- Parameters:
str- theStringto be appended- Returns:
- a reference to this
ByteBufferobject
-
append
public ByteBuffer append(char c)
Appends acharto the buffer. Thecharis converted according to the encoding ISO-8859-1.- Parameters:
c- thecharto be appended- Returns:
- a reference to this
ByteBufferobject
-
append
public ByteBuffer append(ByteBuffer buf)
Appends anotherByteBufferto this buffer.- Parameters:
buf- theByteBufferto be appended- Returns:
- a reference to this
ByteBufferobject
-
append
public ByteBuffer append(int i)
Appends the string representation of anint.- Parameters:
i- theintto be appended- Returns:
- a reference to this
ByteBufferobject
-
append
public ByteBuffer append(byte b)
-
appendHex
public ByteBuffer appendHex(byte b)
-
append
public ByteBuffer append(float i)
Appends a string representation of afloataccording to the Pdf conventions.- Parameters:
i- thefloatto be appended- Returns:
- a reference to this
ByteBufferobject
-
append
public ByteBuffer append(double d)
Appends a string representation of adoubleaccording to the Pdf conventions.- Parameters:
d- thedoubleto be appended- Returns:
- a reference to this
ByteBufferobject
-
formatDouble
public static java.lang.String formatDouble(double d)
Outputs adoubleinto a format suitable for the PDF.- Parameters:
d- a double- Returns:
- the
Stringrepresentation of thedouble
-
formatDouble
public static java.lang.String formatDouble(double d, ByteBuffer buf)Outputs adoubleinto a format suitable for the PDF.- Parameters:
d- a doublebuf- a ByteBuffer- Returns:
- the
Stringrepresentation of thedoubleifbufisnull. Ifbufis 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
countfield, which is the number of valid bytes in this byte buffer.
-
setSize
public void setSize(int size)
-
toString
public java.lang.String toString()
Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.- Overrides:
toStringin classjava.lang.Object- Returns:
- String translated from the buffer's contents.
-
toString
public java.lang.String toString(java.lang.String enc) throws java.io.UnsupportedEncodingExceptionConverts 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:
java.io.UnsupportedEncodingException- If the named encoding is not supported.
-
writeTo
public void writeTo(java.io.OutputStream out) throws java.io.IOExceptionWrites 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:
java.io.IOException- if an I/O error occurs.
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len)- Overrides:
writein classjava.io.OutputStream
-
getBuffer
public byte[] getBuffer()
-
-