1 25 package org.archive.io; 26 27 import it.unimi.dsi.fastutil.io.RepositionableStream; 28 29 import java.io.BufferedInputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 33 50 public class RepositionableInputStream extends BufferedInputStream implements 51 RepositionableStream { 52 private long position = 0; 53 private long markPosition = -1; 54 55 public RepositionableInputStream(InputStream in) { 56 super(in); 57 } 58 59 public RepositionableInputStream(InputStream in, int size) { 60 super(in, size); 61 } 62 63 public int read(byte[] b) throws IOException { 64 int read = super.read(b); 65 if (read != -1) { 66 position += read; 67 } 68 return read; 69 } 70 71 public synchronized int read(byte[] b, int offset, int ct) 72 throws IOException { 73 if (!isMarked()) { 79 super.mark((ct > offset)? ct - offset: ct); 80 } 81 int read = super.read(b, offset, ct); 82 if (read != -1) { 83 position += read; 84 } 85 return read; 86 } 87 88 public int read() throws IOException { 89 if (!isMarked()) { 95 super.mark(1); 96 } 97 int c = super.read(); 98 if (c != -1) { 99 position++; 100 } 101 return c; 102 } 103 104 public void position(final long offset) { 105 if (this.position == offset) { 106 return; 107 } 108 int diff = (int)(offset - this.position); 109 long lowerBound = this.position - this.pos; 110 long upperBound = lowerBound + this.count; 111 if (offset < lowerBound || offset >= upperBound) { 112 throw new IllegalAccessError ("Offset goes outside " + 113 "current this.buf (TODO: Do buffer fills if positive)"); 114 } 115 this.position = offset; 116 this.pos += diff; 117 this.markPosition = -1; 119 } 120 121 public void mark(int readlimit) { 122 this.markPosition = this.position; 123 super.mark(readlimit); 124 } 125 126 public void reset() throws IOException { 127 super.reset(); 128 this.position = this.markPosition; 129 this.markPosition = -1; 130 } 131 132 protected boolean isMarked() { 133 return this.markPosition != -1; 134 } 135 136 public long position() { 137 return this.position; 138 } 139 } | Popular Tags |