Package totalcross.io
Class ByteArrayStream
- java.lang.Object
-
- totalcross.io.Stream
-
- totalcross.io.RandomAccessStream
-
- totalcross.io.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
-
-
Field Summary
-
Fields inherited from class totalcross.io.RandomAccessStream
pos, SEEK_CUR, SEEK_END, SEEK_SET
-
Fields inherited from class totalcross.io.Stream
skipBuffer
-
-
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 intavailable()Returns the number of bytes available in the buffer from the actual read position.voidclose()does nothing.intcount()Deprecated.usegetPos()instead.byte[]getBuffer()Gets the internal buffer used.intgetPos()Returns the current offset in this stream.voidmark()Sets the current position as the maximum size of the buffer so that no more than the current written data will be read.intreadBytes(byte[] buf, int start, int count)Reads bytes from the stream.voidreadFully(Stream inputStream, int retryCount, int bufSize)Reads all data from the input stream into our buffer.voidreset()Resets the position to 0 so the buffer can be reused, and sets the mark to the buffer real limits.intreuse()Reuses the already read part of the buffer.voidsetBuffer(byte[] buffer)Sets the buffer to be used, resetting the current position.voidsetPos(int newPos)Sets the file pointer for read and write operations to the given position.voidsetPos(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.voidsetSize(int newSize, boolean copyOldData)Sets the size of the current byte array.intskipBytes(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.intwriteBytes(byte[] buf, int start, int count)Writes bytes to the stream.-
Methods inherited from class totalcross.io.Stream
asInputStream, asOutputStream, asStream, asStream, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
-
-
-
-
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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.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.usegetPos()instead.Returns the current position in the buffer.
-
getPos
public int getPos()
Description copied from class:RandomAccessStreamReturns the current offset in this stream.- Overrides:
getPosin classRandomAccessStream- 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: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)
-
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.
-
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: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
-
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 sizecopyOldData- If true, the old data up toposis 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 IOExceptionDescription copied from class:RandomAccessStreamSets 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:
setPosin classRandomAccessStream- 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 IOExceptionDescription copied from class:RandomAccessStreamSets 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:
setPosin classRandomAccessStream- 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 readretryCount- 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
-
-