001 package com.imagero.uio;
002
003 import com.imagero.uio.impl.AbstractRandomAccessInput;
004
005 import java.io.ByteArrayInputStream;
006 import java.io.IOException;
007 import java.io.InputStream;
008
009 /**
010 * Wrapper for ByteArrayInputStream which gives possibility to use it as AbstractRandomAccessInput.
011 * Date: 19.12.2007
012 *
013 * @author Andrey Kuznetsov
014 */
015 public class BaisWrapper extends AbstractRandomAccessInput {
016 long pos;
017 long mark;
018 long count;
019
020 long offset;
021
022 ByteArrayInputStream in;
023
024 public BaisWrapper(ByteArrayInputStream in) {
025 this.in = in;
026 in.mark(0);
027 count = in.available();
028 }
029
030 public BaisWrapper(BaisWrapper in, long offset, int byteOrder) {
031 this.in = in.in;
032 this.offset = offset;
033 count = in.count - (in.offset + (int)offset);
034 setByteOrder(byteOrder);
035 }
036
037 synchronized public int read() throws IOException {
038 seek0(pos);
039 int k = in.read();
040 pos++;
041 return k;
042 }
043
044 synchronized public int read(byte b[], int off, int len) throws IOException {
045 seek0(pos);
046 int read = in.read(b, off, len);
047 pos += read;
048 return read;
049 }
050
051 synchronized public long skip(long n) throws IOException {
052 seek0(pos);
053 long length = in.skip(n);
054 pos += length;
055 return length;
056 }
057
058 synchronized public void seek(long position) throws IOException {
059 seek0(position);
060 }
061
062 private void seek0(long position) throws IOException {
063 in.reset();
064 pos = 0;
065 if(position + offset > 0) {
066 skip(position + offset);
067 }
068 }
069
070 public boolean markSupported() {
071 return true;
072 }
073
074 public synchronized void mark(int readlimit) {
075 mark = pos;
076 }
077
078 public synchronized void reset() throws IOException {
079 seek0(mark);
080 }
081
082 public long getFilePointer() {
083 return pos;
084 }
085
086 public long length() {
087 return count - offset;
088 }
089
090 public RandomAccessInput createInputChild(long offset, int byteOrder, boolean syncPointer) throws IOException {
091 return new BaisWrapper(this, offset, byteOrder);
092 }
093
094 public void close() throws IOException {
095 in.close();
096 }
097
098 public InputStream createInputStream(long offset) {
099 return new BaisWrapper(this, offset, byteOrder);
100 }
101 }