1 16 package com.blandware.atleap.common.parsers.html; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 22 38 class RewindableInputStreamWrapper extends InputStream { 39 private InputStream stream; 40 private ByteArrayOutputStream readBytes; 41 private boolean remembering; 42 43 private RewindableInputStreamWrapper() {} 44 45 RewindableInputStreamWrapper(InputStream stream) { 46 this.stream = stream; 47 readBytes = new ByteArrayOutputStream (); 48 remembering = true; 49 } 50 51 public int available() throws IOException { 52 return stream.available(); 53 } 54 55 public void close() throws IOException {} 56 57 public synchronized void reset() throws IOException { 58 stream.reset(); 59 } 60 61 public boolean markSupported() { 62 return stream.markSupported(); 63 } 64 65 public synchronized void mark(int readlimit) { 66 stream.mark(readlimit); 67 } 68 69 public long skip(long n) throws IOException { 70 return stream.skip(n); 71 } 72 73 public int read(byte[] b) throws IOException { 74 return super.read(b); 75 } 76 77 public int read(byte[] b, int off, int len) throws IOException { 78 return super.read(b, off, len); 79 } 80 81 public int read() throws IOException { 82 int res = stream.read(); 83 if (remembering) { 84 readBytes.write(res); 85 } 86 return res; 87 } 88 89 93 public InputStream getStream() { 94 return stream; 95 } 96 97 103 public byte[] getReadBytes() { 104 return (readBytes == null) ? null : readBytes.toByteArray(); 105 } 106 107 110 public void stopRemembering() { 111 remembering = false; 112 readBytes = null; 113 } 114 } 115 | Popular Tags |