KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > io > RandomAccessFileInputStream


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.io;
32
33 import java.io.InputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37  * This class allows a section of a RandomAccessFile to be accessed as an
38  * input stream.
39  *
40  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
41  * @version $Revision: 1.5 $
42  */

43 public class RandomAccessFileInputStream extends InputStream JavaDoc
44 {
45     private RandomAccess file;
46     private long currentPosition;
47     private long endPosition;
48
49     /**
50      * Constructor.
51      *
52      * @param raFile The file to read the data from.
53      * @param startPosition The position in the file that this stream starts.
54      * @param length The length of the input stream.
55      */

56     public RandomAccessFileInputStream( RandomAccess raFile, long startPosition, long length )
57     {
58         file = raFile;
59         currentPosition = startPosition;
60         endPosition = currentPosition+length;
61     }
62     /**
63      * {@inheritDoc}
64      */

65     public int available()
66     {
67         return (int)(endPosition - currentPosition);
68     }
69     /**
70      * {@inheritDoc}
71      */

72     public void close()
73     {
74         //do nothing because we want to leave the random access file open.
75
}
76     /**
77      * {@inheritDoc}
78      */

79     public int read() throws IOException JavaDoc
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     /**
94      * {@inheritDoc}
95      */

96     public int read( byte[] b, int offset, int length ) throws IOException JavaDoc
97     {
98         //only allow a read of the amount available.
99
if( length > available() )
100         {
101             length = available();
102         }
103         int amountRead = -1;
104         //only read if there are bytes actually available, otherwise
105
//return -1 if the EOF has been reached.
106
if( available() > 0 )
107         {
108             synchronized(file)
109             {
110                 file.seek( currentPosition );
111                 amountRead = file.read( b, offset, length );
112             }
113         }
114         //update the current cursor position.
115
if( amountRead > 0 )
116         {
117             currentPosition += amountRead;
118         }
119         return amountRead;
120     }
121
122     /**
123      * {@inheritDoc}
124      */

125     public long skip( long amountToSkip )
126     {
127         long amountSkipped = Math.min( amountToSkip, available() );
128         currentPosition+= amountSkipped;
129         return amountSkipped;
130     }
131 }
Popular Tags