diff options
Diffstat (limited to 'src/com/github/junrar/io')
-rw-r--r-- | src/com/github/junrar/io/IReadOnlyAccess.java | 61 | ||||
-rw-r--r-- | src/com/github/junrar/io/InputStreamReadOnlyAccessFile.java | 59 | ||||
-rw-r--r-- | src/com/github/junrar/io/RandomAccessStream.java | 203 | ||||
-rw-r--r-- | src/com/github/junrar/io/Raw.java | 305 | ||||
-rw-r--r-- | src/com/github/junrar/io/ReadOnlyAccessByteArray.java | 90 | ||||
-rw-r--r-- | src/com/github/junrar/io/ReadOnlyAccessFile.java | 55 | ||||
-rw-r--r-- | src/com/github/junrar/io/ReadOnlyAccessInputStream.java | 81 |
7 files changed, 854 insertions, 0 deletions
diff --git a/src/com/github/junrar/io/IReadOnlyAccess.java b/src/com/github/junrar/io/IReadOnlyAccess.java new file mode 100644 index 0000000..e0cde6e --- /dev/null +++ b/src/com/github/junrar/io/IReadOnlyAccess.java @@ -0,0 +1,61 @@ +/*
+ * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
+ * Original author: Edmund Wagner
+ * Creation date: 23.05.2007
+ *
+ * Source: $HeadURL$
+ * Last changed: $LastChangedDate$
+ *
+ * the unrar licence applies to all junrar source and binary distributions
+ * you are not allowed to use this source to re-create the RAR compression algorithm
+ *
+ * Here some html entities which can be used for escaping javadoc tags:
+ * "&": "&" or "&"
+ * "<": "<" or "<"
+ * ">": ">" or ">"
+ * "@": "@"
+ */
+package com.github.junrar.io;
+
+import java.io.IOException;
+
+
+/**
+ * DOCUMENT ME
+ *
+ * @author $LastChangedBy$
+ * @version $LastChangedRevision$
+ */
+public interface IReadOnlyAccess {
+
+ /**
+ * @return the current position in the file
+ */
+ public long getPosition() throws IOException;
+
+ /**
+ * @param pos the position in the file
+ * @return success ? true : false
+ */
+ public void setPosition(long pos) throws IOException;
+
+ /** Read a single byte of data. */
+ public int read() throws IOException;
+
+ /**
+ * Read up to <tt>count</tt> bytes to the specified buffer.
+ */
+ public int read(byte[] buffer, int off, int count) throws IOException;
+
+ /**
+ * Read exactly <tt>count</tt> bytes to the specified buffer.
+ *
+ * @param buffer where to store the read data
+ * @param count how many bytes to read
+ * @return bytes read || -1 if IO problem
+ */
+ public int readFully(byte[] buffer, int count) throws IOException;
+
+ /** Close this file. */
+ public void close() throws IOException;
+}
diff --git a/src/com/github/junrar/io/InputStreamReadOnlyAccessFile.java b/src/com/github/junrar/io/InputStreamReadOnlyAccessFile.java new file mode 100644 index 0000000..3c7eedd --- /dev/null +++ b/src/com/github/junrar/io/InputStreamReadOnlyAccessFile.java @@ -0,0 +1,59 @@ +package com.github.junrar.io;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+
+
+
+/**
+ * InputStream based implementation of the <code>IReadOnlyAccess</code> interface.
+ *
+ * @see http://rsbweb.nih.gov/ij/
+ * @author martinr
+ */
+public class InputStreamReadOnlyAccessFile implements IReadOnlyAccess {
+ private RandomAccessStream is;
+
+ /**
+ * Create new instance.
+ *
+ * @param is The input stream to wrap.
+ */
+ public InputStreamReadOnlyAccessFile(final InputStream is) {
+ this.is = new RandomAccessStream(new BufferedInputStream(is));
+ }
+
+ @Override
+ public long getPosition() throws IOException {
+ return is.getLongFilePointer();
+ }
+
+ @Override
+ public void setPosition(long pos) throws IOException {
+ is.seek(pos);
+ }
+
+ @Override
+ public int read() throws IOException {
+ return is.read();
+ }
+
+ @Override
+ public int read(byte[] buffer, int off, int count) throws IOException {
+ return is.read(buffer, off, count);
+ }
+
+ @Override
+ public int readFully(byte[] buffer, int count) throws IOException {
+ is.readFully(buffer, count);
+ return count;
+ }
+
+ @Override
+ public void close() throws IOException {
+ is.close();
+ }
+
+}
diff --git a/src/com/github/junrar/io/RandomAccessStream.java b/src/com/github/junrar/io/RandomAccessStream.java new file mode 100644 index 0000000..49442bf --- /dev/null +++ b/src/com/github/junrar/io/RandomAccessStream.java @@ -0,0 +1,203 @@ +/* + * public domain as of http://rsbweb.nih.gov/ij/disclaimer.html + */ +package com.github.junrar.io; + +import java.io.*; +import java.util.Vector; + +/** + * This is a class that uses a memory cache to allow seeking within an + * InputStream. Based on the JAI MemoryCacheSeekableStream class. Can also be + * constructed from a RandomAccessFile, which uses less memory since the memory + * cache is not required. + */ +@SuppressWarnings("rawtypes") +public final class RandomAccessStream extends InputStream { + + private static final int BLOCK_SIZE = 512; + private static final int BLOCK_MASK = 511; + private static final int BLOCK_SHIFT = 9; + + private InputStream src; + private RandomAccessFile ras; + private long pointer; + private Vector data; + private int length; + private boolean foundEOS; + + /** + * Constructs a RandomAccessStream from an InputStream. Seeking backwards is + * supported using a memory cache. + */ + public RandomAccessStream(InputStream inputstream) { + pointer = 0L; + data = new Vector(); + length = 0; + foundEOS = false; + src = inputstream; + } + + /** Constructs a RandomAccessStream from an RandomAccessFile. */ + public RandomAccessStream(RandomAccessFile ras) { + this.ras = ras; + } + + public int getFilePointer() throws IOException { + if (ras != null) + return (int) ras.getFilePointer(); + else + return (int) pointer; + } + + public long getLongFilePointer() throws IOException { + if (ras != null) + return ras.getFilePointer(); + else + return pointer; + } + + public int read() throws IOException { + if (ras != null) + return ras.read(); + long l = pointer + 1L; + long l1 = readUntil(l); + if (l1 >= l) { + byte abyte0[] = (byte[]) data + .elementAt((int) (pointer >> BLOCK_SHIFT)); + return abyte0[(int) (pointer++ & BLOCK_MASK)] & 0xff; + } else + return -1; + } + + public int read(byte[] bytes, int off, int len) throws IOException { + if (bytes == null) + throw new NullPointerException(); + if (ras != null) + return ras.read(bytes, off, len); + if (off < 0 || len < 0 || off + len > bytes.length) + throw new IndexOutOfBoundsException(); + if (len == 0) + return 0; + long l = readUntil(pointer + len); + if (l <= pointer) + return -1; + else { + byte abyte1[] = (byte[]) data + .elementAt((int) (pointer >> BLOCK_SHIFT)); + int k = Math.min(len, BLOCK_SIZE - (int) (pointer & BLOCK_MASK)); + System.arraycopy(abyte1, (int) (pointer & BLOCK_MASK), bytes, off, + k); + pointer += k; + return k; + } + } + + public final void readFully(byte[] bytes) throws IOException { + readFully(bytes, bytes.length); + } + + public final void readFully(byte[] bytes, int len) throws IOException { + int read = 0; + do { + int l = read(bytes, read, len - read); + if (l < 0) + break; + read += l; + } while (read < len); + } + + @SuppressWarnings("unchecked") + private long readUntil(long l) throws IOException { + if (l < length) + return l; + if (foundEOS) + return length; + int i = (int) (l >> BLOCK_SHIFT); + int j = length >> BLOCK_SHIFT; + for (int k = j; k <= i; k++) { + byte abyte0[] = new byte[BLOCK_SIZE]; + data.addElement(abyte0); + int i1 = BLOCK_SIZE; + int j1 = 0; + while (i1 > 0) { + int k1 = src.read(abyte0, j1, i1); + if (k1 == -1) { + foundEOS = true; + return length; + } + j1 += k1; + i1 -= k1; + length += k1; + } + + } + + return length; + } + + public void seek(long loc) throws IOException { + if (ras != null) { + ras.seek(loc); + return; + } + if (loc < 0L) + pointer = 0L; + else + pointer = loc; + } + + public void seek(int loc) throws IOException { + long lloc = ((long) loc) & 0xffffffffL; + if (ras != null) { + ras.seek(lloc); + return; + } + if (lloc < 0L) + pointer = 0L; + else + pointer = lloc; + } + + public final int readInt() throws IOException { + int i = read(); + int j = read(); + int k = read(); + int l = read(); + if ((i | j | k | l) < 0) + throw new EOFException(); + else + return (i << 24) + (j << 16) + (k << 8) + l; + } + + public final long readLong() throws IOException { + return ((long) readInt() << 32) + ((long) readInt() & 0xffffffffL); + } + + public final double readDouble() throws IOException { + return Double.longBitsToDouble(readLong()); + } + + public final short readShort() throws IOException { + int i = read(); + int j = read(); + if ((i | j) < 0) + throw new EOFException(); + else + return (short) ((i << 8) + j); + } + + public final float readFloat() throws IOException { + return Float.intBitsToFloat(readInt()); + } + + public void close() throws IOException { + if (ras != null) + ras.close(); + else { + data.removeAllElements(); + src.close(); + } + } + +}
\ No newline at end of file diff --git a/src/com/github/junrar/io/Raw.java b/src/com/github/junrar/io/Raw.java new file mode 100644 index 0000000..87affda --- /dev/null +++ b/src/com/github/junrar/io/Raw.java @@ -0,0 +1,305 @@ +/*
+ * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
+ * Original author: Edmund Wagner
+ * Creation date: 18.06.2007
+ *
+ * Source: $HeadURL$
+ * Last changed: $LastChangedDate$
+ *
+ * the unrar licence applies to all junrar source and binary distributions
+ * you are not allowed to use this source to re-create the RAR compression algorithm
+ *
+ * Here some html entities which can be used for escaping javadoc tags:
+ * "&": "&" or "&"
+ * "<": "<" or "<"
+ * ">": ">" or ">"
+ * "@": "@"
+ */
+package com.github.junrar.io;
+
+/**
+ * Read / write numbers to a byte[] regarding the endianness of the array
+ *
+ * @author $LastChangedBy$
+ * @version $LastChangedRevision$
+ */
+public class Raw {
+ /**
+ * Read a short value from the byte array at the given position (Big Endian)
+ *
+ * @param array
+ * the array to read from
+ * @param pos
+ * the position
+ * @return the value
+ */
+ public static final short readShortBigEndian(byte[] array, int pos) {
+ short temp = 0;
+ temp |= array[pos] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 1] & 0xff;
+ return temp;
+ }
+
+ /**
+ * Read a int value from the byte array at the given position (Big Endian)
+ *
+ * @param array
+ * the array to read from
+ * @param pos
+ * the offset
+ * @return the value
+ */
+ public static final int readIntBigEndian(byte[] array, int pos) {
+ int temp = 0;
+ temp |= array[pos] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 1] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 2] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 3] & 0xff;
+ return temp;
+ }
+
+ /**
+ * Read a long value from the byte array at the given position (Big Endian)
+ *
+ * @param array
+ * the array to read from
+ * @param pos
+ * the offset
+ * @return the value
+ */
+ public static final long readLongBigEndian(byte[] array, int pos) {
+ int temp = 0;
+ temp |= array[pos] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 1] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 2] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 3] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 4] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 5] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 6] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 7] & 0xff;
+ return temp;
+ }
+
+ /**
+ * Read a short value from the byte array at the given position (little
+ * Endian)
+ *
+ * @param array
+ * the array to read from
+ * @param pos
+ * the offset
+ * @return the value
+ */
+ public static final short readShortLittleEndian(byte[] array, int pos) {
+ short result = 0;
+ result += array[pos + 1] & 0xff;
+ result <<= 8;
+ result += array[pos] & 0xff;
+ return result;
+ }
+
+ /**
+ * Read an int value from the byte array at the given position (little
+ * Endian)
+ *
+ * @param array
+ * the array to read from
+ * @param pos
+ * the offset
+ * @return the value
+ */
+ public static final int readIntLittleEndian(byte[] array, int pos) {
+ return ((array[pos + 3] & 0xff) << 24)
+ | ((array[pos + 2] & 0xff) << 16)
+ | ((array[pos + 1] & 0xff) << 8) | ((array[pos] & 0xff));
+ }
+
+ /**
+ * Read an long value(unsigned int) from the byte array at the given
+ * position (little Endian)
+ *
+ * @param array
+ * @param pos
+ * @return
+ */
+ public static final long readIntLittleEndianAsLong(byte[] array, int pos) {
+ return (((long) array[pos + 3] & 0xff) << 24)
+ | (((long) array[pos + 2] & 0xff) << 16)
+ | (((long) array[pos + 1] & 0xff) << 8)
+ | (((long) array[pos] & 0xff));
+ }
+
+ /**
+ * Read a long value from the byte array at the given position (little
+ * Endian)
+ *
+ * @param array
+ * the array to read from
+ * @param pos
+ * the offset
+ * @return the value
+ */
+ public static final long readLongLittleEndian(byte[] array, int pos) {
+ int temp = 0;
+ temp |= array[pos + 7] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 6] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 5] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 4] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 3] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 2] & 0xff;
+ temp <<= 8;
+ temp |= array[pos + 1] & 0xff;
+ temp <<= 8;
+ temp |= array[pos];
+ return temp;
+ }
+
+ /**
+ * Write a short value into the byte array at the given position (Big
+ * endian)
+ *
+ * @param array
+ * the array
+ * @param pos
+ * the offset
+ * @param value
+ * the value to write
+ */
+ public static final void writeShortBigEndian(byte[] array, int pos,
+ short value) {
+ array[pos] = (byte) (value >>> 8);
+ array[pos + 1] = (byte) (value & 0xFF);
+
+ }
+
+ /**
+ * Write an int value into the byte array at the given position (Big endian)
+ *
+ * @param array
+ * the array
+ * @param pos
+ * the offset
+ * @param value
+ * the value to write
+ */
+ public static final void writeIntBigEndian(byte[] array, int pos, int value) {
+ array[pos] = (byte) ((value >>> 24) & 0xff);
+ array[pos + 1] = (byte) ((value >>> 16) & 0xff);
+ array[pos + 2] = (byte) ((value >>> 8) & 0xff);
+ array[pos + 3] = (byte) ((value) & 0xff);
+
+ }
+
+ /**
+ * Write a long value into the byte array at the given position (Big endian)
+ *
+ * @param array
+ * the array
+ * @param pos
+ * the offset
+ * @param value
+ * the value to write
+ */
+ public static final void writeLongBigEndian(byte[] array, int pos,
+ long value) {
+ array[pos] = (byte) (value >>> 56);
+ array[pos + 1] = (byte) (value >>> 48);
+ array[pos + 2] = (byte) (value >>> 40);
+ array[pos + 3] = (byte) (value >>> 32);
+ array[pos + 4] = (byte) (value >>> 24);
+ array[pos + 5] = (byte) (value >>> 16);
+ array[pos + 6] = (byte) (value >>> 8);
+ array[pos + 7] = (byte) (value & 0xFF);
+
+ }
+
+ /**
+ * Write a short value into the byte array at the given position (little
+ * endian)
+ *
+ * @param array
+ * the array
+ * @param pos
+ * the offset
+ * @param value
+ * the value to write
+ */
+ public static final void writeShortLittleEndian(byte[] array, int pos,
+ short value) {
+ array[pos + 1] = (byte) (value >>> 8);
+ array[pos] = (byte) (value & 0xFF);
+
+ }
+
+ /**
+ * Increment a short value at the specified position by the specified amount
+ * (little endian).
+ */
+ public static final void incShortLittleEndian(byte[] array, int pos, int dv) {
+ int c = ((array[pos] & 0xff) + (dv & 0xff)) >>> 8;
+ array[pos] += dv & 0xff;
+ if ((c > 0) || ((dv & 0xff00) != 0)) {
+ array[pos + 1] += ((dv >>> 8) & 0xff) + c;
+ }
+ }
+
+ /**
+ * Write an int value into the byte array at the given position (little
+ * endian)
+ *
+ * @param array
+ * the array
+ * @param pos
+ * the offset
+ * @param value
+ * the value to write
+ */
+ public static final void writeIntLittleEndian(byte[] array, int pos,
+ int value) {
+ array[pos + 3] = (byte) (value >>> 24);
+ array[pos + 2] = (byte) (value >>> 16);
+ array[pos + 1] = (byte) (value >>> 8);
+ array[pos] = (byte) (value & 0xFF);
+
+ }
+
+ /**
+ * Write a long value into the byte array at the given position (little
+ * endian)
+ *
+ * @param array
+ * the array
+ * @param pos
+ * the offset
+ * @param value
+ * the value to write
+ */
+ public static final void writeLongLittleEndian(byte[] array, int pos,
+ long value) {
+ array[pos + 7] = (byte) (value >>> 56);
+ array[pos + 6] = (byte) (value >>> 48);
+ array[pos + 5] = (byte) (value >>> 40);
+ array[pos + 4] = (byte) (value >>> 32);
+ array[pos + 3] = (byte) (value >>> 24);
+ array[pos + 2] = (byte) (value >>> 16);
+ array[pos + 1] = (byte) (value >>> 8);
+ array[pos] = (byte) (value & 0xFF);
+
+ }
+}
diff --git a/src/com/github/junrar/io/ReadOnlyAccessByteArray.java b/src/com/github/junrar/io/ReadOnlyAccessByteArray.java new file mode 100644 index 0000000..bbdb687 --- /dev/null +++ b/src/com/github/junrar/io/ReadOnlyAccessByteArray.java @@ -0,0 +1,90 @@ +/*
+ * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
+ * Original author: Edmund Wagner
+ * Creation date: 30.05.2007
+ *
+ * Source: $HeadURL$
+ * Last changed: $LastChangedDate$
+ *
+ * the unrar licence applies to all junrar source and binary distributions
+ * you are not allowed to use this source to re-create the RAR compression algorithm
+ *
+ * Here some html entities which can be used for escaping javadoc tags:
+ * "&": "&" or "&"
+ * "<": "<" or "<"
+ * ">": ">" or ">"
+ * "@": "@"
+ */
+package com.github.junrar.io;
+
+import java.io.EOFException;
+import java.io.IOException;
+
+/**
+ * A File like access to a byte array.
+ * (seek and read certain number of bytes)
+ *
+ * @author $LastChangedBy$
+ * @version $LastChangedRevision$
+ */
+public class ReadOnlyAccessByteArray implements IReadOnlyAccess{
+
+ private int positionInFile;
+ private byte[] file;
+
+ /**
+ * Initialize with byte[ ]
+ * @param file the file given as byte array
+ */
+ public ReadOnlyAccessByteArray(byte[] file){
+ if(file == null){
+ throw new NullPointerException("file must not be null!!");
+ }
+ this.file = file;
+ this.positionInFile = 0;
+ }
+
+ public long getPosition() throws IOException {
+ return positionInFile;
+ }
+
+ public void setPosition(long pos) throws IOException {
+ if (pos < file.length && pos >= 0){
+ this.positionInFile = (int)pos;
+ }
+ else{
+ throw new EOFException();
+ }
+ }
+
+ /** Read a single byte of data. */
+ public int read() throws IOException {
+ return file[positionInFile++];
+ }
+
+ /**
+ * Read up to <tt>count</tt> bytes to the specified buffer.
+ */
+ public int read(byte[] buffer, int off, int count) throws IOException {
+ int read = Math.min(count, file.length-positionInFile);
+ System.arraycopy(file, positionInFile, buffer, off, read);
+ positionInFile += read;
+ return read;
+ }
+
+ public int readFully(byte[] buffer, int count) throws IOException {
+ if(buffer == null ){
+ throw new NullPointerException("buffer must not be null");
+ }
+ if(count == 0){
+ throw new IllegalArgumentException("cannot read 0 bytes ;-)");
+ }
+ int read = Math.min(count, file.length-(int)positionInFile-1);
+ System.arraycopy(file, (int)positionInFile, buffer, 0, read );
+ positionInFile+=read;
+ return read;
+ }
+
+ public void close() throws IOException {
+ }
+}
diff --git a/src/com/github/junrar/io/ReadOnlyAccessFile.java b/src/com/github/junrar/io/ReadOnlyAccessFile.java new file mode 100644 index 0000000..77ad35a --- /dev/null +++ b/src/com/github/junrar/io/ReadOnlyAccessFile.java @@ -0,0 +1,55 @@ +/*
+ * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
+ * Original author: Edmund Wagner
+ * Creation date: 23.05.2007
+ *
+ * Source: $HeadURL$
+ * Last changed: $LastChangedDate$
+ *
+ * the unrar licence applies to all junrar source and binary distributions
+ * you are not allowed to use this source to re-create the RAR compression algorithm
+ *
+ * Here some html entities which can be used for escaping javadoc tags:
+ * "&": "&" or "&"
+ * "<": "<" or "<"
+ * ">": ">" or ">"
+ * "@": "@"
+ */
+package com.github.junrar.io;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+/**
+ * DOCUMENT ME
+ *
+ * @author $LastChangedBy$
+ * @version $LastChangedRevision$
+ */
+public class ReadOnlyAccessFile extends RandomAccessFile
+ implements IReadOnlyAccess{
+
+ /**
+ * @param file the file
+ * @throws FileNotFoundException
+ */
+ public ReadOnlyAccessFile(File file) throws FileNotFoundException {
+ super(file, "r");
+ }
+
+ public int readFully(byte[] buffer, int count) throws IOException {
+ assert (count > 0) : count;
+ this.readFully(buffer, 0, count);
+ return count;
+ }
+
+ public long getPosition() throws IOException {
+ return this.getFilePointer();
+ }
+
+ public void setPosition(long pos) throws IOException {
+ this.seek(pos);
+ }
+}
diff --git a/src/com/github/junrar/io/ReadOnlyAccessInputStream.java b/src/com/github/junrar/io/ReadOnlyAccessInputStream.java new file mode 100644 index 0000000..80c022e --- /dev/null +++ b/src/com/github/junrar/io/ReadOnlyAccessInputStream.java @@ -0,0 +1,81 @@ +/*
+ * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
+ * Original author: Edmund Wagner
+ * Creation date: 26.06.2007
+ *
+ * Source: $HeadURL$
+ * Last changed: $LastChangedDate$
+ *
+ * the unrar licence applies to all junrar source and binary distributions
+ * you are not allowed to use this source to re-create the RAR compression
+ * algorithm
+ *
+ * Here some html entities which can be used for escaping javadoc tags:
+ * "&": "&" or "&"
+ * "<": "<" or "<"
+ * ">": ">" or ">"
+ * "@": "@"
+ */
+package com.github.junrar.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * DOCUMENT ME
+ *
+ * @author $LastChangedBy$
+ * @version $LastChangedRevision$
+ */
+public class ReadOnlyAccessInputStream extends InputStream {
+ private IReadOnlyAccess file;
+
+ private long curPos;
+ private final long startPos;
+ private final long endPos;
+
+ public ReadOnlyAccessInputStream(IReadOnlyAccess file, long startPos,
+ long endPos) throws IOException {
+ super();
+ this.file = file;
+ this.startPos = startPos;
+ curPos = startPos;
+ this.endPos = endPos;
+ file.setPosition(curPos);
+ }
+
+ @Override
+ public int read() throws IOException {
+ if (curPos == endPos) {
+ return -1;
+ }
+ else {
+ int b = file.read();
+ curPos++;
+ return b;
+ }
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (len == 0) {
+ return 0;
+ }
+ if (curPos == endPos) {
+ return -1;
+ }
+ int bytesRead = file.read(b, off,
+ (int)Math.min(len, endPos - curPos));
+ curPos += bytesRead;
+ return bytesRead;
+ }
+
+ @Override
+ public int read(byte[] b) throws IOException {
+ return read(b, 0, b.length);
+ }
+//
+// public void close() throws IOException {
+// file.close();
+// }
+}
|