KickJava   Java API By Example, From Geeks To Geeks.

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


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

46 public class MemoryTTFDataStream extends TTFDataStream
47 {
48     private byte[] data = null;
49     private int currentPosition = 0;
50     
51     /**
52      * Constructor from a stream.
53      * @param is The stream of read from.
54      * @throws IOException If an error occurs while reading from the stream.
55      */

56     public MemoryTTFDataStream( InputStream JavaDoc is ) throws IOException JavaDoc
57     {
58         try
59         {
60             ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc( is.available() );
61             byte[] buffer = new byte[1024];
62             int amountRead = 0;
63             while( (amountRead = is.read( buffer ) ) != -1 )
64             {
65                 output.write( buffer, 0, amountRead );
66             }
67             data = output.toByteArray();
68         }
69         finally
70         {
71             if( is != null )
72             {
73                 is.close();
74             }
75         }
76     }
77     
78
79     
80     /**
81      * Read an unsigned byte.
82      * @return An unsigned byte.
83      * @throws IOException If there is an error reading the data.
84      */

85     public long readLong() throws IOException JavaDoc
86     {
87         return ((long)(readSignedInt()) << 32) + (readSignedInt() & 0xFFFFFFFFL);
88     }
89     
90     /**
91      * Read a signed integer.
92      *
93      * @return A signed integer.
94      * @throws IOException If there is a problem reading the file.
95      */

96     public int readSignedInt() throws IOException JavaDoc
97     {
98         int ch1 = read();
99         int ch2 = read();
100         int ch3 = read();
101         int ch4 = read();
102         if( (ch1 | ch2 | ch3 | ch4) < 0)
103         {
104             throw new EOFException JavaDoc();
105         }
106         return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
107     }
108     
109     /**
110      * Read an unsigned byte.
111      * @return An unsigned byte.
112      * @throws IOException If there is an error reading the data.
113      */

114     public int read() throws IOException JavaDoc
115     {
116         int retval = -1;
117         if( currentPosition < data.length )
118         {
119             retval = data[currentPosition];
120         }
121         currentPosition++;
122         return retval;
123     }
124     
125     /**
126      * Read an unsigned short.
127      *
128      * @return An unsigned short.
129      * @throws IOException If there is an error reading the data.
130      */

131     public int readUnsignedShort() throws IOException JavaDoc
132     {
133         int ch1 = this.read();
134         int ch2 = this.read();
135         if ((ch1 | ch2) < 0)
136         {
137             throw new EOFException JavaDoc();
138         }
139         return (ch1 << 8) + (ch2 << 0);
140     }
141     
142     /**
143      * Read an signed short.
144      *
145      * @return An signed short.
146      * @throws IOException If there is an error reading the data.
147      */

148     public short readSignedShort() throws IOException JavaDoc
149     {
150         int ch1 = this.read();
151         int ch2 = this.read();
152         if ((ch1 | ch2) < 0)
153         {
154             throw new EOFException JavaDoc();
155         }
156         return (short)((ch1 << 8) + (ch2 << 0));
157     }
158     
159     /**
160      * Close the underlying resources.
161      *
162      * @throws IOException If there is an error closing the resources.
163      */

164     public void close() throws IOException JavaDoc
165     {
166         data = null;
167     }
168     
169     /**
170      * Seek into the datasource.
171      *
172      * @param pos The position to seek to.
173      * @throws IOException If there is an error seeking to that position.
174      */

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

191     public int read(byte[] b,
192             int off,
193             int len)
194      throws IOException JavaDoc
195      {
196         int amountRead = Math.min( len, data.length-currentPosition );
197         System.arraycopy(data,currentPosition,b, off, amountRead );
198         currentPosition+=amountRead;
199         
200         return amountRead;
201      }
202     
203     /**
204      * Get the current position in the stream.
205      * @return The current position in the stream.
206      * @throws IOException If an error occurs while reading the stream.
207      */

208     public long getCurrentPosition() throws IOException JavaDoc
209     {
210         return currentPosition;
211     }
212     
213     /**
214      * Get a COSStream from this TTFDataStream
215      * This permit to pass the data read from an
216      * external source to the COSObjects to keep
217      * a certain persistence layer between specialized
218      * objects like the TTF package and the pdmodel package.
219      *
220      * Created by Pascal Allain
221      * Vertical7 Inc.
222      *
223      * @return COSStream describing this stream
224      */

225     public COSStream getCOSStream()
226     {
227         throw new UnsupportedOperationException JavaDoc( "not yet implemented" );
228     }
229
230 }
Popular Tags