Package org.apache.derby.client.am
Class ByteArrayCombinerStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.derby.client.am.ByteArrayCombinerStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class ByteArrayCombinerStream extends java.io.InputStreamA stream whose source is a list of byte arrays. This class was created when first implementing the JDBC 4 length less overloads in the client driver. The reason was missing support for streaming data with unknown length from the client to the server. The purpose of the stream is to avoid having to repeatedly copy data to grow the byte buffer, or doing a single big copy to combine the byte arrays in the end. This is important for the temporary solution, since we must materialize the stream to find the length anyway. If there is less data available than the specified length, an exception is thrown. Available data is determined by the length of the byte arrays, not the contents of them. A byte array with all 0's is considered valid data. Besides from truncation, this stream does not change the underlying data in any way.
-
-
Field Summary
Fields Modifier and Type Field Description private intarrayIndexIndex of the array we are currently reading from.private java.util.ArrayList<byte[]>arraysA list of the arrays to combine.private byte[]curArrayThe array we are currently reading from.private longgOffsetGlobal offset into the whole stream.private intoffThe local offset into the current array.private longspecifiedLengthLength of the stream.
-
Constructor Summary
Constructors Constructor Description ByteArrayCombinerStream(java.util.ArrayList<byte[]> arraysIn, long length)Create a stream whose source is a list of byte arrays.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Return the number of available bytes.private byte[]nextArray()Fetch the next array to read data from.intread()Read a single byte.intread(byte[] buf, int offset, int length)Reads up to len bytes of data from the input stream into an array of bytes.
-
-
-
Field Detail
-
arrays
private final java.util.ArrayList<byte[]> arrays
A list of the arrays to combine.
-
specifiedLength
private final long specifiedLength
Length of the stream.
-
gOffset
private long gOffset
Global offset into the whole stream.
-
arrayIndex
private int arrayIndex
Index of the array we are currently reading from.
-
curArray
private byte[] curArray
The array we are currently reading from.
-
off
private int off
The local offset into the current array.
-
-
Constructor Detail
-
ByteArrayCombinerStream
public ByteArrayCombinerStream(java.util.ArrayList<byte[]> arraysIn, long length)Create a stream whose source is a list of byte arrays.- Parameters:
arraysIn- anArrayListwith references to the source byte arrays. The references are copied to a newArrayListinstance.length- the length of the stream. Never published outside this object. Note that the length specified can be shorter than the actual number of bytes in the byte arrays.- Throws:
java.lang.IllegalArgumentException- if there is less data available than specified bylength, orlengthis negative.
-
-
Method Detail
-
read
public int read() throws java.io.IOExceptionRead a single byte.- Specified by:
readin classjava.io.InputStream- Returns:
- a byte, or
-1if the end-of-stream is reached - Throws:
java.io.IOException
-
read
public int read(byte[] buf, int offset, int length) throws java.io.IOExceptionReads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many aslenbytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.- Overrides:
readin classjava.io.InputStream- Parameters:
buf- the array to copy bytes intooffset- offset into the arraylength- the maximum number of bytes to read- Returns:
- the number of bytes read, or
-1if end-of-stream is reached - Throws:
java.io.IOException
-
available
public int available()
Return the number of available bytes. The method assumes the specified length of the stream is correct.- Overrides:
availablein classjava.io.InputStream- Returns:
- number of available bytes
-
nextArray
private byte[] nextArray()
Fetch the next array to read data from. The reference in theArrayListis cleared when the array is "taken out".- Returns:
- a
byte[]-object, ornullif there are no more arrays
-
-