1 30 31 package com.nightlabs.io; 32 33 import java.io.*; 34 import java.io.IOException ; 35 36 37 41 public class DataBufferInputStream extends InputStream 42 { 43 DataBuffer dataBuffer; 44 45 protected DataBufferInputStream(DataBuffer _dataBuffer) 46 { 47 this.dataBuffer = _dataBuffer; 48 } 49 50 59 protected long markedReadPos = 0; 60 protected long dataReadPos = 0; 61 62 66 protected BufferedInputStream in = null; 67 68 69 public int available() 70 throws IOException 71 { 72 if (in != null) 73 return in.available(); 74 75 long avl = dataBuffer.length - dataReadPos; 76 77 if (avl <= 0) 78 return 0; 79 80 if (avl > Integer.MAX_VALUE) 81 return Integer.MAX_VALUE; 82 83 return (int)avl; 84 } 85 86 protected boolean closed = false; 87 88 public void close() 89 throws IOException 90 { 91 if (in != null) 92 in.close(); 93 94 closed = true; 95 } 96 97 protected int markReadLimit = 0; 98 public void mark(int readlimit) 99 { 100 if (in != null) 101 in.mark(readlimit); 102 103 markReadLimit = readlimit; 104 markedReadPos = dataReadPos; 105 } 106 107 public boolean markSupported() 108 { 109 return true; 110 } 111 112 protected void checkMode() 113 throws IOException 114 { 115 if (in == null && dataBuffer.mode == DataBuffer.MODE_FILE) { 116 in = new BufferedInputStream(dataBuffer.createFileInputStream()); 117 if (markedReadPos > 0) { 118 in.skip(markedReadPos); 119 in.mark(markReadLimit); 120 } 121 in.skip(dataReadPos - markedReadPos); 122 } 123 } 124 125 public int read() 126 throws IOException 127 { 128 checkMode(); 129 130 if (closed) 131 throw new IOException ("InputStream has already been closed!"); 132 133 if (in != null) { 134 int i = in.read(); 135 if (i >= 0) 136 dataReadPos++; 137 return i; 138 } 139 140 if (dataReadPos > Integer.MAX_VALUE - 10) 141 throw new IllegalStateException ("dataReadPos is too big! Should have switched to file mode earlier!"); 142 143 if (dataReadPos < dataBuffer.length) 144 return ((int)dataBuffer.data[(int)dataReadPos++]) & 0xff; 145 else 146 return -1; 147 } 148 149 public int read(byte[] b, int off, int len) 150 throws IOException 151 { 152 checkMode(); 153 154 if (closed) 155 throw new IOException ("InputStream has already been closed!"); 156 157 int dr; 158 159 if (in != null) 160 dr = in.read(b, off, len); 161 else { 162 if (dataReadPos + len > dataBuffer.length) 163 len = (int)(dataBuffer.length - dataReadPos); 164 165 if (dataReadPos > Integer.MAX_VALUE - 10) 166 throw new IllegalStateException ("dataReadPos is too big! Should have switched to file mode earlier!"); 167 168 if (len > 0) { 169 System.arraycopy(dataBuffer.data, (int)dataReadPos, b, off, len); 170 dr = len; 171 } 172 else 173 dr = -1; 174 } 175 176 if (dr > 0) 177 dataReadPos += dr; 178 179 return dr; 180 } 181 182 public int read(byte[] b) 183 throws IOException 184 { 185 return read(b, 0, b.length); 186 } 187 188 189 190 191 192 193 194 212 213 214 215 public void reset() 216 throws IOException 217 { 218 checkMode(); 219 220 if (in != null) 221 in.reset(); 222 223 dataReadPos = markedReadPos; 224 } 225 226 public long skip(long n) 227 throws IOException 228 { 229 checkMode(); 230 if (in != null) 231 n = in.skip(n); 232 else { 233 long max = dataBuffer.length - dataReadPos; 234 if (n > max) n = max; 235 dataReadPos += n; 236 } 237 return n; 238 } 239 240 241 } | Popular Tags |