Package totalcross.io
Class BufferedStream
- java.lang.Object
-
- totalcross.io.Stream
-
- totalcross.io.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 intREADUsed for opening this buffered stream for reading.static intWRITEUsed for opening this buffered stream for writing.-
Fields inherited from class totalcross.io.Stream
skipBuffer
-
-
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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()This method closes this stream, flushing any pending WRITE data.StreamgetStream()Returns the Stream attached to this BufferedStream.intreadBytes(byte[] buf, int start, int count)Reads bytes from the stream.java.lang.StringreadLine()Reads a line of text from this BufferedStream.voidsetStream(Stream f)Changes the initial Stream to the attached one.intwriteBytes(byte[] buf, int start, int count)Writes bytes to the stream.-
Methods inherited from class totalcross.io.Stream
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
-
-
-
-
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.NullPointerExceptionDescription copied from class:StreamReads 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:
readBytesin classStream- Parameters:
buf- the byte array to read data intostart- the start position in the arraycount- the number of bytes to read- Throws:
IOExceptionIllegalArgumentIOExceptionjava.lang.NullPointerException
-
writeBytes
public final int writeBytes(byte[] buf, int start, int count) throws IllegalArgumentIOException, IOException, java.lang.NullPointerExceptionDescription copied from class:StreamWrites bytes to the stream. Returns the number of bytes actually written or throws anIOExceptionif an error prevented the write operation from occurring.- Specified by:
writeBytesin classStream- Parameters:
buf- the byte array to write data fromstart- the start position in the byte arraycount- the number of bytes to write- Returns:
- the number of bytes actually written
- Throws:
IOExceptionIllegalArgumentIOExceptionjava.lang.NullPointerException
-
close
public final void close() throws IOExceptionThis method closes this stream, flushing any pending WRITE data. It does NOT close the underlying stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.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:
IOExceptionjava.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 IOExceptionReads 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
nullif nothing was read. - Throws:
IOException- Since:
- SuperWaba 5.61
- See Also:
LineReader
-
-