1 18 package org.apache.batik.ext.awt.image.codec; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 29 public class ForwardSeekableStream extends SeekableStream { 30 31 32 private InputStream src; 33 34 35 long pointer = 0L; 36 37 38 long markPos = -1L; 39 40 44 public ForwardSeekableStream(InputStream src) { 45 this.src = src; 46 } 47 48 49 public final int read() throws IOException { 50 int result = src.read(); 51 if (result != -1) { 52 ++pointer; 53 } 54 return result; 55 } 56 57 58 public final int read(byte[] b, int off, int len) throws IOException { 59 int result = src.read(b, off, len); 60 if (result != -1) { 61 pointer += result; 62 } 63 return result; 64 } 65 66 67 public final long skip(long n) throws IOException { 68 long skipped = src.skip(n); 69 pointer += skipped; 70 return skipped; 71 } 72 73 74 public final int available() throws IOException { 75 return src.available(); 76 } 77 78 79 public final void close() throws IOException { 80 src.close(); 81 } 82 83 84 public synchronized final void mark(int readLimit) { 85 markPos = pointer; 86 src.mark(readLimit); 87 } 88 89 90 public synchronized final void reset() throws IOException { 91 if (markPos != -1) { 92 pointer = markPos; 93 } 94 src.reset(); 95 } 96 97 98 public boolean markSupported() { 99 return src.markSupported(); 100 } 101 102 103 public final boolean canSeekBackwards() { 104 return false; 105 } 106 107 108 public final long getFilePointer() { 109 return pointer; 110 } 111 112 118 public final void seek(long pos) throws IOException { 119 while (pos - pointer > 0) { 120 pointer += src.skip(pos - pointer); 121 } 122 } 123 } 124 | Popular Tags |