KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > ttf > RAFDataStream


1 /**
2  * Copyright (c) 2004, 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.ttf;
32
33 import java.io.File JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.RandomAccessFile JavaDoc;
37
38 import org.pdfbox.cos.COSStream;
39
40 /**
41  * An implementation of the TTFDataStream that goes against a RAF.
42  *
43  * @author Ben Litchfield (ben@csh.rit.edu)
44  * @version $Revision: 1.3 $
45  */

46 public class RAFDataStream extends TTFDataStream
47 {
48     private RandomAccessFile JavaDoc raf = null;
49     /**
50      * Constructor.
51      *
52      * @param name The raf file.
53      * @param mode The mode to open the RAF.
54      *
55      * @throws FileNotFoundException If there is a problem creating the RAF.
56      *
57      * @see RandomAccessFile#RandomAccessFile( String, String )
58      */

59     public RAFDataStream(String JavaDoc name, String JavaDoc mode) throws FileNotFoundException JavaDoc
60     {
61         raf = new RandomAccessFile JavaDoc( name, mode );
62     }
63     
64     /**
65      * Constructor.
66      *
67      * @param file The raf file.
68      * @param mode The mode to open the RAF.
69      *
70      * @throws FileNotFoundException If there is a problem creating the RAF.
71      *
72      * @see RandomAccessFile#RandomAccessFile( File, String )
73      */

74     public RAFDataStream(File JavaDoc file, String JavaDoc mode) throws FileNotFoundException JavaDoc
75     {
76         raf = new RandomAccessFile JavaDoc( file, mode );
77     }
78     
79     /**
80      * Read an signed short.
81      *
82      * @return An signed short.
83      * @throws IOException If there is an error reading the data.
84      */

85     public short readSignedShort() throws IOException JavaDoc
86     {
87         return raf.readShort();
88     }
89     
90     /**
91      * Get the current position in the stream.
92      * @return The current position in the stream.
93      * @throws IOException If an error occurs while reading the stream.
94      */

95     public long getCurrentPosition() throws IOException JavaDoc
96     {
97         return raf.getFilePointer();
98     }
99     
100     /**
101      * Get a COSStream from this TTFDataStream
102      * This permit to pass the data read from an
103      * external source to the COSObjects to keep
104      * a certain persistence layer between specialized
105      * objects like the TTF package and the pdmodel package.
106      *
107      * Created by Pascal Allain
108      * Vertical7 Inc.
109      *
110      * @return COSStream describing this stream (unfiletred)
111      */

112     public COSStream getCOSStream()
113     {
114         COSStream retval = null;
115         
116         retval = new COSStream(raf);
117         
118         return retval;
119     }
120     
121     /**
122      * Close the underlying resources.
123      *
124      * @throws IOException If there is an error closing the resources.
125      */

126     public void close() throws IOException JavaDoc
127     {
128         raf.close();
129         raf = null;
130     }
131     
132     /**
133      * Read an unsigned byte.
134      * @return An unsigned byte.
135      * @throws IOException If there is an error reading the data.
136      */

137     public int read() throws IOException JavaDoc
138     {
139         return raf.read();
140     }
141     
142     /**
143      * Read an unsigned short.
144      *
145      * @return An unsigned short.
146      * @throws IOException If there is an error reading the data.
147      */

148     public int readUnsignedShort() throws IOException JavaDoc
149     {
150         return raf.readUnsignedShort();
151     }
152     
153     /**
154      * Read an unsigned byte.
155      * @return An unsigned byte.
156      * @throws IOException If there is an error reading the data.
157      */

158     public long readLong() throws IOException JavaDoc
159     {
160         return raf.readLong();
161     }
162     
163     /**
164      * Seek into the datasource.
165      *
166      * @param pos The position to seek to.
167      * @throws IOException If there is an error seeking to that position.
168      */

169     public void seek(long pos) throws IOException JavaDoc
170     {
171         raf.seek( pos );
172     }
173     
174     /**
175      * @see java.io.InputStream#read( byte[], int, int )
176      *
177      * @param b The buffer to write to.
178      * @param off The offset into the buffer.
179      * @param len The length into the buffer.
180      *
181      * @return The number of bytes read.
182      *
183      * @throws IOException If there is an error reading from the stream.
184      */

185     public int read(byte[] b,
186             int off,
187             int len)
188      throws IOException JavaDoc
189      {
190         return raf.read(b,off,len);
191      }
192 }
193
Popular Tags