Package totalcross.io

Class ByteArrayStream

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

    public class ByteArrayStream
    extends RandomAccessStream
    Creates a byte array stream, which is a growable array of bytes. This class cannot be used for output AND input, but only for output OR input.

    If you plan to read or write huge amount of data, consider using the CompressedByteArrayStream class instead.

    See Also:
    CompressedByteArrayStream
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteArrayStream​(byte[] buffer)
      Creates a ByteArrayStream.
      ByteArrayStream​(byte[] buffer, int len)
      Creates a ByteArrayStream.
      ByteArrayStream​(int size)
      Creates a ByteArrayStream.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      int available()
      Returns the number of bytes available in the buffer from the actual read position.
      void close()
      does nothing.
      int count()
      Deprecated.
      use getPos() instead.
      byte[] getBuffer()
      Gets the internal buffer used.
      int getPos()
      Returns the current offset in this stream.
      void mark()
      Sets the current position as the maximum size of the buffer so that no more than the current written data will be read.
      int readBytes​(byte[] buf, int start, int count)
      Reads bytes from the stream.
      void readFully​(Stream inputStream, int retryCount, int bufSize)
      Reads all data from the input stream into our buffer.
      void reset()
      Resets the position to 0 so the buffer can be reused, and sets the mark to the buffer real limits.
      int reuse()
      Reuses the already read part of the buffer.
      void setBuffer​(byte[] buffer)
      Sets the buffer to be used, resetting the current position.
      void setPos​(int newPos)
      Sets the file pointer for read and write operations to the given position.
      void setPos​(int offset, int origin)
      Sets the file pointer for read and write operations to a new position defined by adding offset to a reference position specified by origin.
      void setSize​(int newSize, boolean copyOldData)
      Sets the size of the current byte array.
      int skipBytes​(int n)
      Moves the cursor n bytes from the current position, moving backwards if n is negative, or forward if n is positive.
      The cursor cannot be placed outside the stream limits, stopping at position 0 when moving backwards, or at the last position of the stream, when moving forward.
      byte[] toByteArray()
      Returns a copy of the data inside this buffer.
      int writeBytes​(byte[] buf, int start, int count)
      Writes bytes to the stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ByteArrayStream

        public ByteArrayStream​(byte[] buffer)
        Creates a ByteArrayStream.
        Parameters:
        buffer - The initial buffer from where data will be read or written into.
      • ByteArrayStream

        public ByteArrayStream​(byte[] buffer,
                               int len)
        Creates a ByteArrayStream.
        Parameters:
        buffer - The initial buffer from where data will be read or written into.
        len - The length to read from the buffer.
      • ByteArrayStream

        public ByteArrayStream​(int size)
        Creates a ByteArrayStream.
        Parameters:
        size - The initial size that the byte array will have.
    • Method Detail

      • mark

        public void mark()
        Sets the current position as the maximum size of the buffer so that no more than the current written data will be read. This already resets the read position to zero.
        See Also:
        reset()
      • available

        public int available()
        Returns the number of bytes available in the buffer from the actual read position.
        Since:
        SuperWaba 4.02
      • close

        public void close()
        does nothing.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • getBuffer

        public byte[] getBuffer()
        Gets the internal buffer used. The actual read or written data may differ from the buffer's length; use the count method to get the correct value.
        See Also:
        count()
      • setBuffer

        public void setBuffer​(byte[] buffer)
        Sets the buffer to be used, resetting the current position.
        Parameters:
        buffer - the new internal buffer.
        Since:
        TotalCross 1.0.
      • count

        @Deprecated
        public int count()
        Deprecated.
        use getPos() instead.
        Returns the current position in the buffer.
      • getPos

        public int getPos()
        Description copied from class: RandomAccessStream
        Returns the current offset in this stream.
        Overrides:
        getPos in class RandomAccessStream
        Returns:
        the offset from the beginning of the stream, in bytes, at which the next read or write occurs.
      • readBytes

        public int readBytes​(byte[] buf,
                             int start,
                             int count)
        Description copied from class: Stream
        Reads bytes from the stream. Returns the number of bytes actually read or -1 if the end of the stream was reached. (if applicable to the stream)
        Specified by:
        readBytes in class Stream
        Parameters:
        buf - the byte array to read data into
        start - the start position in the array
        count - the number of bytes to read
      • reset

        public void reset()
        Resets the position to 0 so the buffer can be reused, and sets the mark to the buffer real limits.
        See Also:
        mark()
      • skipBytes

        public int skipBytes​(int n)
        Moves the cursor n bytes from the current position, moving backwards if n is negative, or forward if n is positive.
        The cursor cannot be placed outside the stream limits, stopping at position 0 when moving backwards, or at the last position of the stream, when moving forward.
        Overrides:
        skipBytes in class Stream
        Parameters:
        n - the number of bytes to move.
        Returns:
        the number of bytes actually moved.
      • reuse

        public int reuse()
        Reuses the already read part of the buffer. This method shifts the buffer from the current position to 0, so you can append more data to the buffer.
        Returns:
        The number of bytes shifted
        Since:
        SuperWaba 4.01
      • writeBytes

        public int writeBytes​(byte[] buf,
                              int start,
                              int count)
        Description copied from class: Stream
        Writes bytes to the stream. Returns the number of bytes actually written or throws an IOException if an error prevented the write operation from occurring.
        Specified by:
        writeBytes in class Stream
        Parameters:
        buf - the byte array to write data from
        start - the start position in the byte array
        count - the number of bytes to write
        Returns:
        the number of bytes actually written
      • setSize

        public void setSize​(int newSize,
                            boolean copyOldData)
        Sets the size of the current byte array. If the current size is smaller then the given one, a new byte array is created with the given size. If there's already enough room for the given size, nothing is made. Note that this method does not reset the current position.
        Parameters:
        newSize - the new array size
        copyOldData - If true, the old data up to pos is copied into the new buffer.
        Since:
        SuperWaba 5.1
      • toByteArray

        public byte[] toByteArray()
        Returns a copy of the data inside this buffer. The returned buffer will have the exact size of the stored data
        Since:
        SuperWaba 5.1
      • setPos

        public void setPos​(int offset,
                           int origin)
                    throws IOException
        Description copied from class: RandomAccessStream
        Sets the file pointer for read and write operations to a new position defined by adding offset to a reference position specified by origin.
        Specified by:
        setPos in class RandomAccessStream
        Parameters:
        offset - number of bytes to offset from origin.
        origin - position from where offset is added. It is specified by one of the SEEK_* constants.
        Throws:
        IOException - if the new position is negative or if an I/O error occurs.
      • setPos

        public void setPos​(int newPos)
                    throws IOException
        Description copied from class: RandomAccessStream
        Sets the file pointer for read and write operations to the given position. The position passed is an absolute position, in bytes, from the beginning of the stream.
        Specified by:
        setPos in class RandomAccessStream
        Parameters:
        newPos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
        Throws:
        IOException - if pos is negative or if an I/O error occurs.
      • readFully

        public void readFully​(Stream inputStream,
                              int retryCount,
                              int bufSize)
                       throws IOException
        Reads all data from the input stream into our buffer. When returned, data is marked as ready to be read.
        Parameters:
        inputStream - The input stream from where data will be read
        retryCount - The number of times to retry if no data is read. In remote connections, use at least 5; for files, it can be 0.
        bufSize - The size of buffer used to read data.
        Throws:
        IOException
        Since:
        TotalCross 1.0