FileDocCategorySizeDatePackage
ByteArrayIStream.javaAPI DocAndroid 1.5 API3388Wed May 06 22:41:16 BST 2009com.vladium.util

ByteArrayIStream

public final class ByteArrayIStream extends InputStream
An unsynchronized version of java.io.ByteArrayInputStream.

All argument validation is disabled in release mode.

author
(C) 2001, Vlad Roubtsov

Fields Summary
private final byte[]
m_buf
private final int
m_max
private int
m_pos
private static final int
NATIVE_COPY_THRESHOLD
Constructors Summary
public ByteArrayIStream(byte[] buf)

        this (buf, buf.length);
    
public ByteArrayIStream(byte[] buf, int length)

        if ($assert.ENABLED) $assert.ASSERT ((length >= 0) && (length <= buf.length),
            "invalid length: " + length);
        
        m_buf = buf;
        m_max = length;
    
Methods Summary
public final intavailable()

        return m_max - m_pos;
    
public final voidclose()
Equivalent to {@link #reset()}.

        reset ();
    
public final intread()

        if (m_pos >= m_max)
            return -1;
        else
            return 0xFF & m_buf [m_pos ++];
    
public final intread(byte[] buf, int offset, int length)

        if ($assert.ENABLED)
            $assert.ASSERT ((offset >= 0) && (offset <= buf.length) &&
                (length >= 0) && ((offset + length) <= buf.length),
                "invalid input (" + buf.length + ", " + offset + ", " + length + ")");
        
        final int pos = m_pos;
        final int max = m_max;
        
        if (pos >= max) return -1;
        if (pos + length > max) length = max - pos;
        if (length <= 0) return 0;
        
        final byte [] mbuf = m_buf;
        
        if (length < NATIVE_COPY_THRESHOLD)
            for (int i = 0; i < length; ++ i) buf [offset + i] = mbuf [pos + i];
        else
            System.arraycopy (mbuf, pos, buf, offset, length);
            
        m_pos += length;
        
        return length;
    
public final voidreset()
Differs from the contruct for InputStream.reset() in that this method always resets the stream to the same it was immediately after creation.

        m_pos = 0;
    
public final longskip(long n)

        if (m_pos + n > m_max) n = m_max - m_pos;

        if (n < 0) return 0;        
        m_pos += n;
        
        return n;