1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import java.io.FilterInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 30 44 public class RememberBytesInputStream extends FilterInputStream 45 { 46 ByteHolder bh; 47 boolean recording = true; 48 49 boolean streamClosed = false; 54 55 61 public RememberBytesInputStream(InputStream in, ByteHolder bh) { 62 super(in); 63 64 if (SanityManager.DEBUG) 65 SanityManager.ASSERT(bh.writingMode()); 66 67 this.bh = bh; 68 69 } 70 71 76 public int read() throws IOException { 77 if (SanityManager.DEBUG) 78 SanityManager.ASSERT(recording, 79 "Must be in record mode to perform a read."); 80 81 int value = -1; 82 83 if ( !streamClosed ) 84 { 85 value = super.read(); 86 if ( value != -1 ) 87 bh.write(value); 88 else 89 streamClosed =true; 90 } 91 92 return value; 93 } 94 95 100 public int read(byte b[], int off, int len) throws IOException { 101 if (SanityManager.DEBUG) 102 SanityManager.ASSERT(recording, 103 "Must be in record mode to perform a read."); 104 105 if ( !streamClosed ) { 106 if ((len + off) > b.length) 107 len = b.length - off; 108 109 len = super.read(b, off, len); 110 if (len > 0 ) 111 bh.write(b, off, len); 112 else 113 streamClosed = true; 114 } else { 115 return -1; 116 } 117 118 return len; 119 } 120 121 129 public long fillBuf(int len) throws IOException { 130 131 long val = 0; 132 133 if ( !streamClosed ) 134 { 135 val = bh.write(this.in, len); 136 137 if ( val < len ) 140 streamClosed=true; 141 } 142 143 return val; 144 } 145 146 152 public int putBuf(OutputStream out, int len) throws IOException { 153 bh.startReading(); 154 return bh.read(out, len); 155 } 156 157 162 public long skip(long count) throws IOException { 163 if (SanityManager.DEBUG) 164 SanityManager.ASSERT(recording, 165 "Must be in record mode to perform a read."); 166 return bh.write(in,count); 167 } 168 169 172 public InputStream getReplayStream() throws IOException { 173 bh.startReading(); 174 recording = false; 175 return new ByteHolderInputStream(bh); 176 } 177 178 181 public ByteHolder getByteHolder() throws IOException { 182 return bh; 183 } 184 185 191 public void clear() throws IOException { 192 bh.clear(); 193 recording = true; 194 } 195 196 202 public void setInput(InputStream in) { 203 this.in = in; 204 streamClosed = false; 205 } 206 207 211 public boolean recording() { 212 return recording; 213 } 214 215 219 public int available() throws IOException { 220 int remainingBytes = bh.available(); 223 remainingBytes = remainingBytes > 0 ? remainingBytes : (-1) * remainingBytes; 224 return remainingBytes; 225 } 226 227 232 public int numBytesSaved() throws IOException 233 { 234 return(bh.numBytesSaved()); 235 } 236 237 242 public int shiftToFront() throws IOException { 243 int bytesShifted = bh.shiftToFront(); 244 return bytesShifted; 245 } 246 247 250 public String toString() 251 { 252 return 253 "RememberBytesInputStream: "+ 254 " recording: "+recording+" "+bh; 255 } 256 } 257 258 | Popular Tags |