KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fonts > FontFileReader


1 /*
2  * $Id: FontFileReader.java,v 1.3.2.5 2003/05/27 14:14:18 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.fonts;
52
53 import java.io.InputStream JavaDoc;
54 import java.io.File JavaDoc;
55 import java.io.IOException JavaDoc;
56
57 import org.apache.fop.tools.IOUtil;
58
59 /**
60  * Reads a file into an array and
61  * provides file like functions for array access.
62  */

63 public class FontFileReader {
64     private int fsize; // file size
65
private int current; // current position in file
66
private byte[] file;
67
68     /**
69      * Initializes class and reads stream. Init does not close stream
70      * @param in InputStream to read from
71      * new array with size + inc
72      */

73     private void init(InputStream JavaDoc in) throws java.io.IOException JavaDoc {
74         java.io.ByteArrayOutputStream JavaDoc bout = new java.io.ByteArrayOutputStream JavaDoc();
75         try {
76             this.file = IOUtil.toByteArray(in, 50000);
77             this.fsize = this.file.length;
78             this.current = 0;
79         } finally {
80             bout.close();
81         }
82     }
83
84
85     /**
86      * Constructor
87      * @param fileName filename to read
88      */

89     public FontFileReader(String JavaDoc fileName) throws java.io.IOException JavaDoc {
90         File JavaDoc f = new File JavaDoc(fileName);
91         InputStream JavaDoc in = new java.io.FileInputStream JavaDoc(f);
92         try {
93             init(in);
94         } finally {
95             in.close();
96         }
97     }
98
99
100     /**
101      * Constructor
102      * @param in InputStream to read from
103      */

104     public FontFileReader(InputStream JavaDoc in) throws java.io.IOException JavaDoc {
105         init(in);
106     }
107
108
109     /**
110      * Set current file position to offset
111      */

112     public void seek_set(long offset) throws IOException JavaDoc {
113         if (offset > fsize || offset < 0)
114             throw new java.io.EOFException JavaDoc("Reached EOF, file size=" + fsize
115                                            + " offset=" + offset);
116         current = (int)offset;
117     }
118
119     /**
120      * Set current file position to offset
121      */

122     public void seek_add(long add) throws IOException JavaDoc {
123         seek_set(current + add);
124     }
125
126     public void skip(long add) throws IOException JavaDoc {
127         seek_add(add);
128     }
129
130     /**
131      * return current file position
132      */

133     public int getCurrentPos() {
134         return current;
135     }
136
137     public int getFileSize() {
138         return fsize;
139     }
140
141
142     /**
143      * Read 1 byte, throws EOFException on end of file
144      */

145     public byte read() throws IOException JavaDoc {
146         if (current > fsize)
147             throw new java.io.EOFException JavaDoc("Reached EOF, file size=" + fsize);
148
149         byte ret = file[current++];
150         return ret;
151     }
152
153
154
155     /**
156      * Read 1 signed byte from InputStream
157      */

158     public final byte readTTFByte() throws IOException JavaDoc {
159         return read();
160     }
161
162     /**
163      * Read 1 unsigned byte from InputStream
164      */

165     public final int readTTFUByte() throws IOException JavaDoc {
166         byte buf = read();
167
168         if (buf < 0)
169             return (int)(256 + buf);
170         else
171             return (int)buf;
172     }
173
174     /**
175      * Read 2 bytes signed from InputStream
176      */

177     public final short readTTFShort() throws IOException JavaDoc {
178         int ret = (readTTFUByte() << 8) + readTTFUByte();
179         short sret = (short)ret;
180
181         return sret;
182     }
183
184     /**
185      * Read 2 bytes unsigned from InputStream
186      */

187     public final int readTTFUShort() throws IOException JavaDoc {
188         int ret = (readTTFUByte() << 8) + readTTFUByte();
189
190         return (int)ret;
191     }
192
193     /**
194      * Write a USHort at a given position
195      */

196     public final void writeTTFUShort(int pos, int val) throws IOException JavaDoc {
197         if ((pos + 2) > fsize)
198             throw new java.io.EOFException JavaDoc("Reached EOF");
199         byte b1 = (byte)((val >> 8) & 0xff);
200         byte b2 = (byte)(val & 0xff);
201         file[pos] = b1;
202         file[pos + 1] = b2;
203     }
204
205     /**
206      * Read 2 bytes signed from InputStream at position pos
207      * without changing current position
208      */

209     public final short readTTFShort(long pos) throws IOException JavaDoc {
210         long cp = getCurrentPos();
211         seek_set(pos);
212         short ret = readTTFShort();
213         seek_set(cp);
214         return ret;
215     }
216
217     /**
218      * Read 2 bytes unsigned from InputStream at position pos
219      * without changing current position
220      */

221     public final int readTTFUShort(long pos) throws IOException JavaDoc {
222         long cp = getCurrentPos();
223         seek_set(pos);
224         int ret = readTTFUShort();
225         seek_set(cp);
226         return ret;
227     }
228
229     /**
230      * Read 4 bytes from InputStream
231      */

232     public final int readTTFLong() throws IOException JavaDoc {
233         long ret = readTTFUByte(); // << 8;
234
ret = (ret << 8) + readTTFUByte();
235         ret = (ret << 8) + readTTFUByte();
236         ret = (ret << 8) + readTTFUByte();
237
238         return (int)ret;
239     }
240
241     /**
242      * Read 4 bytes from InputStream
243      */

244     public final long readTTFULong() throws IOException JavaDoc {
245         long ret = readTTFUByte();
246         ret = (ret << 8) + readTTFUByte();
247         ret = (ret << 8) + readTTFUByte();
248         ret = (ret << 8) + readTTFUByte();
249
250         return ret;
251     }
252
253     /**
254      * Read a 0 terminatet ISO-8859-1 string
255      */

256     public final String JavaDoc readTTFString() throws IOException JavaDoc {
257         int i = current;
258         while (file[i++] != 0) {
259             if (i > fsize)
260                 throw new java.io.EOFException JavaDoc("Reached EOF, file size="
261                                                + fsize);
262         }
263
264         byte[] tmp = new byte[i - current];
265         System.arraycopy(file, current, tmp, 0, i - current);
266         return new String JavaDoc(tmp, "ISO-8859-1");
267     }
268
269
270     /**
271      * Read an ISO-8859-1 string of len bytes
272      */

273     public final String JavaDoc readTTFString(int len) throws IOException JavaDoc {
274         if ((len + current) > fsize)
275             throw new java.io.EOFException JavaDoc("Reached EOF, file size=" + fsize);
276
277         byte[] tmp = new byte[len];
278         System.arraycopy(file, current, tmp, 0, len);
279         current += len;
280         final String JavaDoc encoding;
281         if ((tmp.length > 0) && (tmp[0] == 0)) {
282             encoding = "UnicodeBig";
283         } else {
284             encoding = "ISO-8859-1";
285         }
286         return new String JavaDoc(tmp, encoding);
287     }
288
289     /**
290      * Return a copy of the internal array
291      * @throws IOException if out of bounds
292      */

293     public byte[] getBytes(int offset,
294                            int length) throws java.io.IOException JavaDoc {
295         if ((offset + length) > fsize)
296             throw new java.io.IOException JavaDoc("Reached EOF");
297
298         byte[] ret = new byte[length];
299         System.arraycopy(file, offset, ret, 0, length);
300         return ret;
301     }
302
303
304 }
305
Popular Tags