001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.io;
031:
032: import java.io.InputStream;
033: import java.io.IOException;
034:
035: /**
036: * This class allows a section of a RandomAccessFile to be accessed as an
037: * input stream.
038: *
039: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
040: * @version $Revision: 1.5 $
041: */
042: public class RandomAccessFileInputStream extends InputStream {
043: private RandomAccess file;
044: private long currentPosition;
045: private long endPosition;
046:
047: /**
048: * Constructor.
049: *
050: * @param raFile The file to read the data from.
051: * @param startPosition The position in the file that this stream starts.
052: * @param length The length of the input stream.
053: */
054: public RandomAccessFileInputStream(RandomAccess raFile,
055: long startPosition, long length) {
056: file = raFile;
057: currentPosition = startPosition;
058: endPosition = currentPosition + length;
059: }
060:
061: /**
062: * {@inheritDoc}
063: */
064: public int available() {
065: return (int) (endPosition - currentPosition);
066: }
067:
068: /**
069: * {@inheritDoc}
070: */
071: public void close() {
072: //do nothing because we want to leave the random access file open.
073: }
074:
075: /**
076: * {@inheritDoc}
077: */
078: public int read() throws IOException {
079: synchronized (file) {
080: int retval = -1;
081: if (currentPosition < endPosition) {
082: file.seek(currentPosition);
083: currentPosition++;
084: retval = file.read();
085: }
086: return retval;
087: }
088: }
089:
090: /**
091: * {@inheritDoc}
092: */
093: public int read(byte[] b, int offset, int length)
094: throws IOException {
095: //only allow a read of the amount available.
096: if (length > available()) {
097: length = available();
098: }
099: int amountRead = -1;
100: //only read if there are bytes actually available, otherwise
101: //return -1 if the EOF has been reached.
102: if (available() > 0) {
103: synchronized (file) {
104: file.seek(currentPosition);
105: amountRead = file.read(b, offset, length);
106: }
107: }
108: //update the current cursor position.
109: if (amountRead > 0) {
110: currentPosition += amountRead;
111: }
112: return amountRead;
113: }
114:
115: /**
116: * {@inheritDoc}
117: */
118: public long skip(long amountToSkip) {
119: long amountSkipped = Math.min(amountToSkip, available());
120: currentPosition += amountSkipped;
121: return amountSkipped;
122: }
123: }
|