1 31 package org.pdfbox.io; 32 33 import java.io.InputStream ; 34 import java.io.IOException ; 35 36 43 public class RandomAccessFileInputStream extends InputStream 44 { 45 private RandomAccess file; 46 private long currentPosition; 47 private long endPosition; 48 49 56 public RandomAccessFileInputStream( RandomAccess raFile, long startPosition, long length ) 57 { 58 file = raFile; 59 currentPosition = startPosition; 60 endPosition = currentPosition+length; 61 } 62 65 public int available() 66 { 67 return (int)(endPosition - currentPosition); 68 } 69 72 public void close() 73 { 74 } 76 79 public int read() throws IOException 80 { 81 synchronized(file) 82 { 83 int retval = -1; 84 if( currentPosition < endPosition ) 85 { 86 file.seek( currentPosition ); 87 currentPosition++; 88 retval = file.read(); 89 } 90 return retval; 91 } 92 } 93 96 public int read( byte[] b, int offset, int length ) throws IOException 97 { 98 if( length > available() ) 100 { 101 length = available(); 102 } 103 int amountRead = -1; 104 if( available() > 0 ) 107 { 108 synchronized(file) 109 { 110 file.seek( currentPosition ); 111 amountRead = file.read( b, offset, length ); 112 } 113 } 114 if( amountRead > 0 ) 116 { 117 currentPosition += amountRead; 118 } 119 return amountRead; 120 } 121 122 125 public long skip( long amountToSkip ) 126 { 127 long amountSkipped = Math.min( amountToSkip, available() ); 128 currentPosition+= amountSkipped; 129 return amountSkipped; 130 } 131 } | Popular Tags |