001 /*
002 * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
003 *
004 * http://uio.imagero.com
005 *
006 * Redistribution and use in source and binary forms, with or without
007 * modification, are permitted provided that the following conditions are met:
008 *
009 * o Redistributions of source code must retain the above copyright notice,
010 * this list of conditions and the following disclaimer.
011 *
012 * o Redistributions in binary form must reproduce the above copyright notice,
013 * this list of conditions and the following disclaimer in the documentation
014 * and/or other materials provided with the distribution.
015 *
016 * o Neither the name of Andrey Kuznetsov nor the names of
017 * its contributors may be used to endorse or promote products derived
018 * from this software without specific prior written permission.
019 *
020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
029 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
030 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 */
032
033 package com.imagero.uio.io;
034
035 import com.imagero.uio.RandomAccessInput;
036
037 import java.io.IOException;
038 import java.io.InputStream;
039
040 /**
041 * same as FilterInputStream but with RandomAccessIO
042 * @author Kouznetsov Andrei
043 */
044 public class RandomAccessInputStream extends InputStream {
045 protected static long MARK_UNDEFINED = -1L;
046
047 protected RandomAccessInput ro;
048 protected long pos;
049 protected long mark = MARK_UNDEFINED;
050 protected long startPos;
051
052
053 public RandomAccessInputStream(RandomAccessInput ro) {
054 this(ro, 0L);
055 }
056
057 public RandomAccessInputStream(RandomAccessInput ro, long startPos) {
058 this.ro = ro;
059 this.pos = startPos;
060 this.startPos = startPos;
061 }
062
063 synchronized public int read() throws IOException {
064 checkPos();
065 int a = ro.read();
066 pos++;
067 return a;
068 }
069
070 public int read(byte[] b) throws IOException {
071 return read(b, 0, b.length);
072 }
073
074 synchronized public int read(byte[] b, int off, int len) throws IOException {
075 checkPos();
076 if(ro == null) {
077 return -1;
078 }
079 int r = ro.read(b, off, len);
080 pos += r;
081 return r;
082 }
083
084 protected void checkPos() throws IOException {
085 moved = false;
086 if(ro != null && ro.getFilePointer() != pos) {
087 ro.seek(pos);
088 }
089 }
090
091 boolean moved;
092
093 /**
094 * releases reference to RandomAccessRO, but does not closes it
095 */
096 public void close() throws IOException {
097 if(moved) {
098 ro.seek(pos);
099 }
100 ro = null;
101 }
102
103 public int available() throws IOException {
104 return (int)(ro.length() - pos);
105 }
106
107 public boolean markSupported() {
108 return true;
109 }
110
111 public void mark(int i) {
112 mark = pos;
113 }
114
115 public void reset() throws IOException {
116 if(mark == MARK_UNDEFINED) {
117 throw new IOException("mark undefined");
118 }
119 moved = true;
120 pos = mark;
121 }
122
123 public long skip(long l) throws IOException {
124 long skip = Math.min(ro.length() - pos, l);
125 pos += skip;
126 moved = true;
127 return skip;
128 }
129 }