Package pixy.io

Class RandomAccessInputStream

java.lang.Object
java.io.InputStream
pixy.io.RandomAccessInputStream
All Implemented Interfaces:
java.io.Closeable, java.io.DataInput, java.lang.AutoCloseable
Direct Known Subclasses:
FileCacheRandomAccessInputStream, MemoryCacheRandomAccessInputStream

public abstract class RandomAccessInputStream
extends java.io.InputStream
implements java.io.DataInput
Implements a random access input stream

Based on com.sun.media.jai.codec.SeekableStream.

To make it flexible, this class and any of its sub-class doesn't close the underlying stream. It's up to the underlying stream creator to close them. This ensures the actual stream out-lives the random stream itself in case we need to read more content from the underlying stream.

NOTE: for MemoryCacheRandomAccessInputStream, there is the risk of "over read" in which more bytes are cached in the buffer than actually needed. In this case, the underlying stream might not be usable anymore afterwards.

Version:
1.0 01/24/2013
Author:
Wen Yu, yuwen_66@yahoo.com
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected boolean closed  
    protected java.io.InputStream src
    The source stream.
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
    protected RandomAccessInputStream​(java.io.InputStream src)  
  • Method Summary

    Modifier and Type Method Description
    protected void ensureOpen()
    Check to make sure that this stream has not been closed
    protected void finalize()  
    short getEndian()  
    abstract long getStreamPointer()  
    abstract int read()  
    abstract int read​(byte[] b, int off, int len)  
    boolean readBoolean()  
    byte readByte()  
    char readChar()  
    double readDouble()  
    float readFloat()  
    void readFully​(byte[] b)  
    void readFully​(byte[] b, int off, int len)  
    int readInt()  
    java.lang.String readLine()
    Deprecated.
    long readLong()  
    float readS15Fixed16Number()  
    short readShort()  
    float readU16Fixed16Number()  
    float readU8Fixed8Number()  
    int readUnsignedByte()  
    long readUnsignedInt()  
    int readUnsignedShort()  
    java.lang.String readUTF()
    Due to the current implementation, writeUTF and readUTF are the only methods which are machine or byte sequence independent as they are actually both Motorola byte sequence under the hood.
    abstract void seek​(long loc)  
    void setReadStrategy​(ReadStrategy strategy)  
    abstract void shallowClose()
    Closes the RandomAccessInputStream and it's underlying stream
    int skipBytes​(int n)  

    Methods inherited from class java.io.InputStream

    available, close, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • src

      protected java.io.InputStream src
      The source stream.
    • closed

      protected boolean closed
  • Constructor Details

  • Method Details

    • shallowClose

      public abstract void shallowClose() throws java.io.IOException
      Closes the RandomAccessInputStream and it's underlying stream
      Throws:
      java.io.IOException
    • ensureOpen

      protected void ensureOpen() throws java.io.IOException
      Check to make sure that this stream has not been closed
      Throws:
      java.io.IOException
    • finalize

      protected void finalize() throws java.lang.Throwable
      Overrides:
      finalize in class java.lang.Object
      Throws:
      java.lang.Throwable
    • getEndian

      public short getEndian()
    • getStreamPointer

      public abstract long getStreamPointer()
    • read

      public abstract int read() throws java.io.IOException
      Specified by:
      read in class java.io.InputStream
      Throws:
      java.io.IOException
    • read

      public abstract int read​(byte[] b, int off, int len) throws java.io.IOException
      Overrides:
      read in class java.io.InputStream
      Throws:
      java.io.IOException
    • readBoolean

      public final boolean readBoolean() throws java.io.IOException
      Specified by:
      readBoolean in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readByte

      public final byte readByte() throws java.io.IOException
      Specified by:
      readByte in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readChar

      public final char readChar() throws java.io.IOException
      Specified by:
      readChar in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readDouble

      public final double readDouble() throws java.io.IOException
      Specified by:
      readDouble in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFloat

      public final float readFloat() throws java.io.IOException
      Specified by:
      readFloat in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFully

      public final void readFully​(byte[] b) throws java.io.IOException
      Specified by:
      readFully in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readFully

      public final void readFully​(byte[] b, int off, int len) throws java.io.IOException
      Specified by:
      readFully in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readInt

      public final int readInt() throws java.io.IOException
      Specified by:
      readInt in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readLine

      @Deprecated public final java.lang.String readLine() throws java.io.IOException
      Deprecated.
      Specified by:
      readLine in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readLong

      public final long readLong() throws java.io.IOException
      Specified by:
      readLong in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readS15Fixed16Number

      public final float readS15Fixed16Number() throws java.io.IOException
      Throws:
      java.io.IOException
    • readShort

      public final short readShort() throws java.io.IOException
      Specified by:
      readShort in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readU16Fixed16Number

      public final float readU16Fixed16Number() throws java.io.IOException
      Throws:
      java.io.IOException
    • readU8Fixed8Number

      public final float readU8Fixed8Number() throws java.io.IOException
      Throws:
      java.io.IOException
    • readUnsignedByte

      public final int readUnsignedByte() throws java.io.IOException
      Specified by:
      readUnsignedByte in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readUnsignedInt

      public final long readUnsignedInt() throws java.io.IOException
      Throws:
      java.io.IOException
    • readUnsignedShort

      public final int readUnsignedShort() throws java.io.IOException
      Specified by:
      readUnsignedShort in interface java.io.DataInput
      Throws:
      java.io.IOException
    • readUTF

      public final java.lang.String readUTF() throws java.io.IOException
      Due to the current implementation, writeUTF and readUTF are the only methods which are machine or byte sequence independent as they are actually both Motorola byte sequence under the hood. Whereas the following static method is byte sequence dependent as it calls readUnsignedShort of RandomAccessInputStream. DataInputStream.readUTF(this);
      Specified by:
      readUTF in interface java.io.DataInput
      Throws:
      java.io.IOException
    • seek

      public abstract void seek​(long loc) throws java.io.IOException
      Throws:
      java.io.IOException
    • setReadStrategy

      public void setReadStrategy​(ReadStrategy strategy)
    • skipBytes

      public int skipBytes​(int n) throws java.io.IOException
      Specified by:
      skipBytes in interface java.io.DataInput
      Throws:
      java.io.IOException