Class ByteBuffer

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class ByteBuffer
    extends java.io.OutputStream
    Acts like a StringBuffer but works with byte arrays. Floating point is converted to a format suitable to the PDF.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buf
      The buffer where the bytes are stored.
      private static byte[][] byteCache  
      private static int byteCacheSize  
      private static byte[] bytes  
      private static char[] chars  
      private int count
      The count of bytes in the buffer.
      private static java.text.DecimalFormatSymbols dfs  
      private static boolean HIGH_PRECISION
      If true always output floating point numbers with 6 decimal digits.
      private static byte ZERO  
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBuffer()
      Creates new ByteBuffer with capacity 128
      ByteBuffer​(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)  
      (package private) ByteBuffer append​(byte[] b)
      Appends an array of bytes.
      (package private) ByteBuffer append​(byte[] b, int off, int len)
      Appends the subarray of the byte array.
      (package private) ByteBuffer append​(char c)
      Appends a char to the buffer.
      private ByteBuffer append​(double d)
      Appends a string representation of a double according to the Pdf conventions.
      (package private) ByteBuffer append​(float i)
      Appends a string representation of a float according to the Pdf conventions.
      (package private) ByteBuffer append​(int i)
      Appends the string representation of an int.
      (package private) ByteBuffer append​(ByteBuffer buf)
      Appends another ByteBuffer to this buffer.
      (package private) ByteBuffer append​(java.lang.String str)
      Appends a String to the buffer.
      ByteBuffer append_i​(int b)
      Appends an int.
      (package private) ByteBuffer appendHex​(byte b)  
      private static byte[] convertToBytes​(int i)
      Converts an double (multiplied by 100 and cast to an int) into an array of bytes.
      (package private) static java.lang.String formatDouble​(double d)
      Outputs a double into a format suitable for the PDF.
      private static java.lang.String formatDouble​(double d, ByteBuffer buf)
      Outputs a double into a format suitable for the PDF.
      byte[] getBuffer()  
      (package private) void reset()
      Sets the size to zero.
      void setSize​(int size)  
      (package private) int size()
      Returns the current size of the buffer.
      byte[] toByteArray()
      Creates a newly allocated byte array.
      java.lang.String toString()
      Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.
      void write​(byte[] b, int off, int len)  
      void write​(int b)  
      (package private) void writeTo​(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 using out.write(buf, 0, count).
      • Methods inherited from class java.io.OutputStream

        close, flush, nullOutputStream, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • count

        private int count
        The count of bytes in the buffer.
      • buf

        private byte[] buf
        The buffer where the bytes are stored.
      • byteCacheSize

        private static int byteCacheSize
      • byteCache

        private static byte[][] byteCache
      • chars

        private static final char[] chars
      • bytes

        private static final byte[] bytes
      • HIGH_PRECISION

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

        private static final java.text.DecimalFormatSymbols dfs
    • 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

      • 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 byte array
      • 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
      • append

        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

        ByteBuffer append​(byte[] b)
        Appends an array of bytes.
        Parameters:
        b - the array to be appended
        Returns:
        a reference to this ByteBuffer object
      • append

        ByteBuffer append​(java.lang.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

        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

        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

        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

        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

        private 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
      • formatDouble

        static java.lang.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

        private static java.lang.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.
      • reset

        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

        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 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:
        toString in class java.lang.Object
        Returns:
        String translated from the buffer's contents.
      • writeTo

        void writeTo​(java.io.OutputStream out)
              throws java.io.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 using out.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:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
        Overrides:
        write in class java.io.OutputStream
      • getBuffer

        public byte[] getBuffer()