1 16 package com.blandware.atleap.common.parsers.html; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 21 29 class PrefilledInputStream extends InputStream { 30 private InputStream stream; 31 private byte[] prefilledBytes; 32 private int passedBytesNumber; 33 34 private PrefilledInputStream() {} 35 36 43 PrefilledInputStream(RewindableInputStreamWrapper rewindableStreamWrapper) { 44 this.stream = rewindableStreamWrapper.getStream(); 45 this.prefilledBytes = rewindableStreamWrapper.getReadBytes(); 46 passedBytesNumber = 0; 47 } 48 49 52 public int available() throws IOException { 53 return prefilledBytes.length - passedBytesNumber + stream.available(); 54 } 55 56 59 public void close() throws IOException { 60 prefilledBytes = new byte[0]; 61 passedBytesNumber = 0; 62 stream.close(); 63 } 64 65 68 public synchronized void reset() throws IOException { 69 passedBytesNumber = 0; 70 stream.reset(); 71 } 72 73 76 public boolean markSupported() { 77 return false; 78 } 79 80 83 public synchronized void mark(int readlimit) { 84 } 85 86 89 public long skip(long n) throws IOException { 90 int prefilledBytesLeft = prefilledBytes.length - passedBytesNumber; 91 long skippedByStream; 92 long skippedAmongPrefilled; 93 94 if (n <= prefilledBytesLeft) { 95 passedBytesNumber += n; 96 skippedByStream = 0; 97 skippedAmongPrefilled = n; 98 } else { 99 passedBytesNumber = prefilledBytes.length; 100 skippedByStream = stream.skip(n - prefilledBytesLeft); 101 skippedAmongPrefilled = prefilledBytesLeft; 102 } 103 return skippedAmongPrefilled + skippedByStream; 104 } 105 106 109 public int read(byte[] b) throws IOException { 110 return super.read(b); 111 } 112 113 116 public int read(byte[] b, int off, int len) throws IOException { 117 return super.read(b, off, len); 118 } 119 120 123 public int read() throws IOException { 124 if (passedBytesNumber < prefilledBytes.length) { 125 return prefilledBytes[passedBytesNumber++]; 126 } else { 127 return stream.read(); 128 } 129 } 130 } 131 | Popular Tags |