1 27 package org.htmlparser.lexer; 28 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.lang.Runnable ; 32 33 37 public class Stream extends InputStream implements Runnable 38 { 39 public int fills = 0; 40 public int reallocations = 0; 41 public int synchronous = 0; 42 43 46 protected static final int BUFFER_SIZE = 4096; 47 48 51 protected static final int EOF = -1; 52 53 56 protected volatile InputStream mIn; 57 58 61 public volatile byte[] mBuffer; 62 63 66 public volatile int mLevel; 67 68 71 protected int mOffset; 72 73 76 protected int mContentLength; 77 78 81 protected int mMark; 82 83 87 public Stream (InputStream in) 88 { 89 this (in, 0); 90 } 91 92 99 public Stream (InputStream in, int bytes) 100 { 101 mIn = in; 102 mBuffer = null; 103 mLevel = 0; 104 mOffset = 0; 105 mContentLength = bytes < 0 ? 0 : bytes; 106 mMark = -1; 107 } 108 109 119 protected synchronized boolean fill (boolean force) 120 throws 121 IOException 122 { 123 int size; 124 byte[] buffer; 125 int read; 126 boolean ret; 127 128 ret = false; 129 130 if (null != mIn) { 132 if (!force) 133 { if (0 != available ()) 135 return (true); 136 synchronous++; 137 } 138 139 if (0 == mContentLength) 141 { if (null == mBuffer) 143 { 144 mBuffer = new byte[Math.max (BUFFER_SIZE, mIn.available ())]; 145 buffer = mBuffer; 146 } 147 else 148 { 149 if (mBuffer.length - mLevel < BUFFER_SIZE / 2) 150 buffer = new byte[Math.max (mBuffer.length * 2, mBuffer.length + mIn.available ())]; 151 else 152 buffer = mBuffer; 153 } 154 size = buffer.length - mLevel; 155 } 156 else 157 { size = mContentLength - mLevel; 159 if (null == mBuffer) 160 mBuffer = new byte[size]; 161 buffer = mBuffer; 162 } 163 164 read = mIn.read (buffer, mLevel, size); 166 if (-1 == read) 167 { 168 mIn.close (); 169 mIn = null; 170 } 171 else 172 { 173 if (mBuffer != buffer) 174 { System.arraycopy (mBuffer, 0, buffer, 0, mLevel); 176 mBuffer = buffer; 177 reallocations++; 178 } 179 mLevel += read; 180 if ((0 != mContentLength) && (mLevel == mContentLength)) 181 { 182 mIn.close (); 183 mIn = null; 184 } 185 ret = true; 186 fills++; 187 } 188 } 189 190 return (ret); 191 } 192 193 197 201 public void run () 202 { 203 boolean filled; 204 205 do 206 { try 208 { 209 filled = fill (true); 210 } 211 catch (IOException ioe) 212 { 213 ioe.printStackTrace (); 214 filled = false; 217 } 218 } 219 while (filled); 220 } 221 222 226 237 public int read () throws IOException 238 { 239 int ret; 240 241 if (0 == available ()) 254 fill (false); 255 if (0 != available ()) 256 ret = mBuffer[mOffset++] & 0xff; 257 else 258 ret = EOF; 259 260 return (ret); 261 } 262 263 272 public int available () throws IOException 273 { 274 return (mLevel - mOffset); 275 } 276 277 282 public synchronized void close () throws IOException 283 { 284 if (null != mIn) 285 { 286 mIn.close (); 287 mIn = null; 288 } 289 mBuffer = null; 290 mLevel = 0; 291 mOffset = 0; 292 mContentLength =0; 293 mMark = -1; 294 } 295 296 338 public void reset () throws IOException 339 { 340 if (-1 != mMark) 341 mOffset = mMark; 342 else 343 mOffset = 0; 344 } 345 346 358 public boolean markSupported () 359 { 360 return (true); 361 } 362 363 384 public void mark (int readlimit) 385 { 386 mMark = mOffset; 387 } 388 } 389 | Popular Tags |