KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2004-2005, 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.EOFException JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 import java.util.Calendar JavaDoc;
37 import java.util.GregorianCalendar JavaDoc;
38
39 import org.pdfbox.cos.COSStream;
40
41 /**
42  * An interface into a data stream.
43  *
44  * @author Ben Litchfield (ben@csh.rit.edu)
45  * @version $Revision: 1.3 $
46  */

47 public abstract class TTFDataStream
48 {
49
50     /**
51      * Read a 16.16 fixed value, where the first 16 bits are the decimal and the last
52      * 16 bits are the fraction.
53      * @return A 32 bit value.
54      * @throws IOException If there is an error reading the data.
55      */

56     public float read32Fixed() throws IOException JavaDoc
57     {
58         float retval = 0;
59         retval = readSignedShort();
60         retval += (readUnsignedShort()/65536);
61         return retval;
62     }
63     
64     /**
65      * Read a fixed length ascii string.
66      * @param length The length of the string to read.
67      * @return A string of the desired length.
68      * @throws IOException If there is an error reading the data.
69      */

70     public String JavaDoc readString( int length ) throws IOException JavaDoc
71     {
72         return readString( length, "ISO-8859-1" );
73     }
74     
75     /**
76      * Read a fixed length ascii string.
77      * @param length The length of the string to read in bytes.
78      * @param charset The expected character set of the string.
79      * @return A string of the desired length.
80      * @throws IOException If there is an error reading the data.
81      */

82     public String JavaDoc readString( int length, String JavaDoc charset ) throws IOException JavaDoc
83     {
84         byte[] buffer = read( length );
85         return new String JavaDoc(buffer, charset);
86     }
87     
88     /**
89      * Read an unsigned byte.
90      * @return An unsigned byte.
91      * @throws IOException If there is an error reading the data.
92      */

93     public abstract int read() throws IOException JavaDoc;
94     
95     /**
96      * Read an unsigned byte.
97      * @return An unsigned byte.
98      * @throws IOException If there is an error reading the data.
99      */

100     public abstract long readLong() throws IOException JavaDoc;
101     
102     
103     /**
104      * Read an unsigned byte.
105      * @return An unsigned byte.
106      * @throws IOException If there is an error reading the data.
107      */

108     public int readSignedByte() throws IOException JavaDoc
109     {
110         return read() - 255;
111     }
112     
113     /**
114      * Read an unsigned integer.
115      * @return An unsiged integer.
116      * @throws IOException If there is an error reading the data.
117      */

118     public long readUnsignedInt() throws IOException JavaDoc
119     {
120         long byte1 = read();
121         long byte2 = read();
122         long byte3 = read();
123         long byte4 = read();
124         if( byte4 < 0 )
125         {
126             throw new EOFException JavaDoc();
127         }
128         return (byte1 << 24) + (byte2 << 16) + (byte3 << 8) + (byte4 << 0);
129     }
130     
131     /**
132      * Read an unsigned short.
133      *
134      * @return An unsigned short.
135      * @throws IOException If there is an error reading the data.
136      */

137     public abstract int readUnsignedShort() throws IOException JavaDoc;
138     
139     /**
140      * Read an unsigned short array.
141      *
142      * @param length The length of the array to read.
143      * @return An unsigned short array.
144      * @throws IOException If there is an error reading the data.
145      */

146     public int[] readUnsignedShortArray( int length ) throws IOException JavaDoc
147     {
148         int[] array = new int[ length ];
149         for( int i=0; i<length; i++ )
150         {
151             array[i] = readUnsignedShort();
152         }
153         return array;
154     }
155     
156     /**
157      * Read an signed short.
158      *
159      * @return An signed short.
160      * @throws IOException If there is an error reading the data.
161      */

162     public abstract short readSignedShort() throws IOException JavaDoc;
163     
164     /**
165      * Read an eight byte international date.
166      *
167      * @return An signed short.
168      * @throws IOException If there is an error reading the data.
169      */

170     public Calendar JavaDoc readInternationalDate() throws IOException JavaDoc
171     {
172         long secondsSince1904 = readLong();
173         GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc( 1904, 0, 1 );
174         long millisFor1904 = cal.getTimeInMillis();
175         millisFor1904 += (secondsSince1904*1000);
176         cal.setTimeInMillis( millisFor1904 );
177         return cal;
178     }
179     
180     /**
181      * Close the underlying resources.
182      *
183      * @throws IOException If there is an error closing the resources.
184      */

185     public abstract void close() throws IOException JavaDoc;
186     
187     /**
188      * Seek into the datasource.
189      *
190      * @param pos The position to seek to.
191      * @throws IOException If there is an error seeking to that position.
192      */

193     public abstract void seek(long pos) throws IOException JavaDoc;
194     
195     /**
196      * Read a specific number of bytes from the stream.
197      * @param numberOfBytes The number of bytes to read.
198      * @return The byte buffer.
199      * @throws IOException If there is an error while reading.
200      */

201     public byte[] read( int numberOfBytes ) throws IOException JavaDoc
202     {
203         byte[] data = new byte[ numberOfBytes ];
204         int amountRead = 0;
205         int totalAmountRead = 0;
206         while( (amountRead = read( data, totalAmountRead, numberOfBytes-totalAmountRead ) ) != -1 &&
207                totalAmountRead < numberOfBytes )
208         {
209             totalAmountRead += amountRead;
210             //read at most numberOfBytes bytes from the stream.
211
}
212         return data;
213     }
214     
215     /**
216      * @see java.io.InputStream#read( byte[], int, int )
217      *
218      * @param b The buffer to write to.
219      * @param off The offset into the buffer.
220      * @param len The length into the buffer.
221      *
222      * @return The number of bytes read.
223      *
224      * @throws IOException If there is an error reading from the stream.
225      */

226     public abstract int read(byte[] b,
227             int off,
228             int len)
229      throws IOException JavaDoc;
230     
231     /**
232      * Get the current position in the stream.
233      * @return The current position in the stream.
234      * @throws IOException If an error occurs while reading the stream.
235      */

236     public abstract long getCurrentPosition() throws IOException JavaDoc;
237     
238     /**
239      * Get a COSStream from this TTFDataStream
240      * This permit to pass the data read from an
241      * external source to the COSObjects to keep
242      * a certain persistence layer between specialized
243      * objects like the TTF package and the pdmodel package.
244      *
245      * Created by Pascal Allain
246      * Vertical7 Inc.
247      *
248      * @return COSStream describing this stream
249      */

250     public abstract COSStream getCOSStream();
251
252 }
Popular Tags