totalcross.io
Class BufferedStream

totalcross.lang.Object
  extended by totalcross.io.Stream
      extended by totalcross.io.BufferedStream

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(totalcross.lang.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
static int READ
          Used for opening this buffered stream for reading.
static int WRITE
          Used for opening this buffered stream for writing.
 
Fields inherited from class totalcross.io.Stream
skipBuffer
 
Constructor Summary
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.
 
Method Summary
 void close()
          This method closes this stream, flushing any pending WRITE data.
 int readBytes(byte[] buf, int start, int count)
          Reads bytes from the stream.
 int writeBytes(byte[] buf, int start, int count)
          Writes bytes to the stream.
 
Methods inherited from class totalcross.io.Stream
skipBytes, writeBytes, writeBytes
 
Methods inherited from class totalcross.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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
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.
Method Detail

readBytes

public final int readBytes(byte[] buf,
                           int start,
                           int count)
                    throws IllegalArgumentIOException,
                           IOException
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

writeBytes

public final int writeBytes(byte[] buf,
                            int start,
                            int count)
                     throws IllegalArgumentIOException,
                            IOException
Description copied from class: Stream
Writes bytes to the stream. Returns the number of bytes actually written or -1 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

close

public final void close()
                 throws IOException
This method closes this stream, flushing any pending WRITE data. It does NOT close the underlying stream.

Throws:
IOException - If an I/O error occurs.