FileDocCategorySizeDatePackage
SSLEngineDataStream.javaAPI DocAndroid 1.5 API2884Wed May 06 22:41:06 BST 2009org.apache.harmony.xnet.provider.jsse

SSLEngineDataStream

public class SSLEngineDataStream extends Object implements DataStream
This class provides the DataStream functionality implemented over the array of ByteBuffer instances. Among with the data chunks read functionality it provides the info about amount of consumed data. The source ByteBuffer objects can be replaced by other. So one instance of this wrapper can be reused for several data sources.

Fields Summary
private ByteBuffer[]
srcs
private int
offset
private int
limit
private int
available
private int
consumed
Constructors Summary
protected SSLEngineDataStream()

Methods Summary
public intavailable()

        return available;
    
protected intconsumed()

        return consumed;
    
public byte[]getData(int length)

        // TODO: optimization work:
        // use ByteBuffer.get(byte[],int,int)
        // and ByteBuffer.hasArray() methods
        int len = (length < available) ? length : available;
        available -= len;
        consumed += len;
        byte[] res = new byte[len];
        int pos = 0;
        loop:
        for (; offset<limit; offset++) {
            while (srcs[offset].hasRemaining()) {
                res[pos++] = srcs[offset].get();
                len --;
                if (len == 0) {
                    break loop;
                }
            }
        }
        return res;
    
public booleanhasData()

        return available > 0;
    
protected voidsetSourceBuffers(java.nio.ByteBuffer[] srcs, int offset, int length)

        this.srcs = srcs;
        this.offset = offset;
        this.limit = offset+length;
        this.consumed = 0;
        this.available = 0;
        for (int i=offset; i<limit; i++) {
            if (srcs[i] == null) {
                throw new IllegalStateException(
                        "Some of the input parameters are null");
            }
            available += srcs[i].remaining();
        }