Package totalcross.io

Class BufferedStream

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

    public class BufferedStream
    extends Stream
    BufferedStream offers a faster way to read and write data from streams in a buffered manner. This is especially useful when reading or writing large amounts of data. It works like the CompressedByteArrayStream, however it does not compresses the data like that one. Here's a sample code:
       public void writeLargeFile(String path, byte[] largeData) throws IOException
       {
          File f = new File(path, File.CREATE_EMPTY);
          BufferedStream bs = new BufferedStream(f, BufferedStream.WRITE, 4096);
          bs.writeBytes(largeData, 0, largeData.length);
          bs.close(); // important!
          f.close();
       }
     
    Since:
    TotalCross 1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int READ
      Used for opening this buffered stream for reading.
      static int WRITE
      Used for opening this buffered stream for writing.
    • Constructor Summary

      Constructors 
      Constructor Description
      BufferedStream​(Stream stream, int mode)
      Creates a new buffered stream given the underlying stream and the mode to use.
      BufferedStream​(Stream stream, int mode, int bufferSize)
      Creates a new buffered stream given the underlying stream, the mode to use and the buffer size.
    • Field Detail

      • READ

        public static final int READ
        Used for opening this buffered stream for reading.
        See Also:
        Constant Field Values
      • WRITE

        public static final int WRITE
        Used for opening this buffered stream for writing.
        See Also:
        Constant Field Values
    • Constructor Detail

      • BufferedStream

        public BufferedStream​(Stream stream,
                              int mode)
                       throws IllegalArgumentIOException
        Creates a new buffered stream given the underlying stream and the mode to use. This constructor will use the default buffer size: 2048 bytes.
        Parameters:
        stream - The underlying stream.
        mode - The mode to use - READ or WRITE.
        Throws:
        IllegalArgumentIOException - if the mode is invalid.
      • BufferedStream

        public BufferedStream​(Stream stream,
                              int mode,
                              int bufferSize)
                       throws IllegalArgumentIOException,
                              java.lang.NullPointerException
        Creates a new buffered stream given the underlying stream, the mode to use and the buffer size.
        Parameters:
        stream - The underlying stream.
        mode - The mode to use - READ or WRITE.
        bufferSize - The buffer size.
        Throws:
        IllegalArgumentIOException - if the mode is invalid or the bufferSize is not a positive number.
        java.lang.NullPointerException - if stream is null.
    • Method Detail

      • readBytes

        public final int readBytes​(byte[] buf,
                                   int start,
                                   int count)
                            throws IllegalArgumentIOException,
                                   IOException,
                                   java.lang.NullPointerException
        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
        Throws:
        IOException
        IllegalArgumentIOException
        java.lang.NullPointerException
      • writeBytes

        public final int writeBytes​(byte[] buf,
                                    int start,
                                    int count)
                             throws IllegalArgumentIOException,
                                    IOException,
                                    java.lang.NullPointerException
        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
        Throws:
        IOException
        IllegalArgumentIOException
        java.lang.NullPointerException
      • close

        public final void close()
                         throws IOException
        This method closes this stream, flushing any pending WRITE data. It does NOT close the underlying stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        IOException - If an I/O error occurs.
      • setStream

        public void setStream​(Stream f)
                       throws IOException,
                              java.lang.NullPointerException
        Changes the initial Stream to the attached one. Reusing a BufferedStream throught this method can preserve memory.
        Throws:
        IOException
        java.lang.NullPointerException
        Since:
        TotalCross 1.23
      • getStream

        public Stream getStream()
        Returns the Stream attached to this BufferedStream.
        Since:
        TotalCross 1.23
      • readLine

        public java.lang.String readLine()
                                  throws IOException
        Reads a line of text from this BufferedStream. Any char lower than space is considered a new line separator. This method correctly handles newlines with \\n or \\r\\n.

        Note: this method is VERY slow since it reads a single character per time. Consider using LineReader instead.
        Returns:
        the read line or null if nothing was read.
        Throws:
        IOException
        Since:
        SuperWaba 5.61
        See Also:
        LineReader