1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import java.io.*; 22 23 29 30 public class DocumentInputStream 31 extends InputStream 32 { 33 34 private int _current_offset; 36 37 private int _marked_offset; 40 41 private int _document_size; 43 44 private boolean _closed; 46 47 private POIFSDocument _document; 49 50 private byte[] _tiny_buffer; 52 53 static private final int EOD = -1; 55 56 64 65 public DocumentInputStream(final DocumentEntry document) 66 throws IOException 67 { 68 _current_offset = 0; 69 _marked_offset = 0; 70 _document_size = document.getSize(); 71 _closed = false; 72 _tiny_buffer = null; 73 if (document instanceof DocumentNode) 74 { 75 _document = (( DocumentNode ) document).getDocument(); 76 } 77 else 78 { 79 throw new IOException("Cannot open internal document storage"); 80 } 81 } 82 83 91 92 public DocumentInputStream(final POIFSDocument document) 93 throws IOException 94 { 95 _current_offset = 0; 96 _marked_offset = 0; 97 _document_size = document.getSize(); 98 _closed = false; 99 _tiny_buffer = null; 100 _document = document; 101 } 102 103 115 116 public int available() 117 throws IOException 118 { 119 dieIfClosed(); 120 return _document_size - _current_offset; 121 } 122 123 129 130 public void close() 131 throws IOException 132 { 133 _closed = true; 134 } 135 136 159 160 public void mark(int ignoredReadlimit) 161 { 162 _marked_offset = _current_offset; 163 } 164 165 170 171 public boolean markSupported() 172 { 173 return true; 174 } 175 176 188 189 public int read() 190 throws IOException 191 { 192 dieIfClosed(); 193 if (atEOD()) 194 { 195 return EOD; 196 } 197 if (_tiny_buffer == null) 198 { 199 _tiny_buffer = new byte[ 1 ]; 200 } 201 _document.read(_tiny_buffer, _current_offset++); 202 return ((int)_tiny_buffer[ 0 ]) & 0x000000FF; 203 } 204 205 241 242 public int read(final byte [] b) 243 throws IOException, NullPointerException 244 { 245 return read(b, 0, b.length); 246 } 247 248 295 296 public int read(final byte [] b, final int off, final int len) 297 throws IOException, NullPointerException , IndexOutOfBoundsException 298 { 299 dieIfClosed(); 300 if (b == null) 301 { 302 throw new NullPointerException ("buffer is null"); 303 } 304 if ((off < 0) || (len < 0) || (b.length < (off + len))) 305 { 306 throw new IndexOutOfBoundsException ( 307 "can't read past buffer boundaries"); 308 } 309 if (len == 0) 310 { 311 return 0; 312 } 313 if (atEOD()) 314 { 315 return EOD; 316 } 317 int limit = Math.min(available(), len); 318 319 if ((off == 0) && (limit == b.length)) 320 { 321 _document.read(b, _current_offset); 322 } 323 else 324 { 325 byte[] buffer = new byte[ limit ]; 326 327 _document.read(buffer, _current_offset); 328 System.arraycopy(buffer, 0, b, off, limit); 329 } 330 _current_offset += limit; 331 return limit; 332 } 333 334 387 388 public void reset() 389 { 390 _current_offset = _marked_offset; 391 } 392 393 408 409 public long skip(final long n) 410 throws IOException 411 { 412 dieIfClosed(); 413 if (n < 0) 414 { 415 return 0; 416 } 417 int new_offset = _current_offset + ( int ) n; 418 419 if (new_offset < _current_offset) 420 { 421 422 new_offset = _document_size; 424 } 425 else if (new_offset > _document_size) 426 { 427 new_offset = _document_size; 428 } 429 long rval = new_offset - _current_offset; 430 431 _current_offset = new_offset; 432 return rval; 433 } 434 435 private void dieIfClosed() 436 throws IOException 437 { 438 if (_closed) 439 { 440 throw new IOException( 441 "cannot perform requested operation on a closed stream"); 442 } 443 } 444 445 private boolean atEOD() 446 { 447 return _current_offset == _document_size; 448 } 449 } 451 | Popular Tags |