KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > common > PDTextStream


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.pdmodel.common;
32
33 import java.io.ByteArrayOutputStream JavaDoc;
34 import java.io.ByteArrayInputStream JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 import org.pdfbox.cos.COSBase;
39 import org.pdfbox.cos.COSStream;
40 import org.pdfbox.cos.COSString;
41
42 /**
43  * A PDTextStream class is used when the PDF specification supports either
44  * a string or a stream for the value of an object. This is usually when
45  * a value could be large or small, for example a JavaScript method. This
46  * class will help abstract that and give a single unified interface to
47  * those types of fields.
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @version $Revision: 1.3 $
51  */

52 public class PDTextStream implements COSObjectable
53 {
54     private COSString string;
55     private COSStream stream;
56
57     /**
58      * Constructor.
59      *
60      * @param str The string parameter.
61      */

62     public PDTextStream( COSString str )
63     {
64         string = str;
65     }
66
67     /**
68      * Constructor.
69      *
70      * @param str The string parameter.
71      */

72     public PDTextStream( String JavaDoc str )
73     {
74         string = new COSString( str );
75     }
76
77     /**
78      * Constructor.
79      *
80      * @param str The stream parameter.
81      */

82     public PDTextStream( COSStream str )
83     {
84         stream = str;
85     }
86
87     /**
88      * This will create the text stream object. base must either be a string
89      * or a stream.
90      *
91      * @param base The COS text stream object.
92      *
93      * @return A PDTextStream that wraps the base object.
94      */

95     public static PDTextStream createTextStream( COSBase base )
96     {
97         PDTextStream retval = null;
98         if( base instanceof COSString )
99         {
100             retval = new PDTextStream( (COSString) base );
101         }
102         else if( base instanceof COSStream )
103         {
104             retval = new PDTextStream( (COSStream)base );
105         }
106         return retval;
107     }
108
109     /**
110      * Convert this standard java object to a COS object.
111      *
112      * @return The cos object that matches this Java object.
113      */

114     public COSBase getCOSObject()
115     {
116         COSBase retval = null;
117         if( string == null )
118         {
119             retval = stream;
120         }
121         else
122         {
123             retval = string;
124         }
125         return retval;
126     }
127
128     /**
129      * This will get this value as a string. If this is a stream then it
130      * will load the entire stream into memory, so you should only do this when
131      * the stream is a manageable size.
132      *
133      * @return This value as a string.
134      *
135      * @throws IOException If an IO error occurs while accessing the stream.
136      */

137     public String JavaDoc getAsString() throws IOException JavaDoc
138     {
139         String JavaDoc retval = null;
140         if( string != null )
141         {
142             retval = string.getString();
143         }
144         else
145         {
146             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
147             byte[] buffer = new byte[ 1024 ];
148             int amountRead = -1;
149             InputStream JavaDoc is = stream.getUnfilteredStream();
150             while( (amountRead = is.read( buffer ) ) != -1 )
151             {
152                 out.write( buffer, 0, amountRead );
153             }
154             retval = new String JavaDoc( out.toByteArray() );
155         }
156         return retval;
157     }
158
159     /**
160      * This is the preferred way of getting data with this class as it uses
161      * a stream object.
162      *
163      * @return The stream object.
164      *
165      * @throws IOException If an IO error occurs while accessing the stream.
166      */

167     public InputStream JavaDoc getAsStream() throws IOException JavaDoc
168     {
169         InputStream JavaDoc retval = null;
170         if( string != null )
171         {
172             retval = new ByteArrayInputStream JavaDoc( string.getBytes() );
173         }
174         else
175         {
176             retval = stream.getUnfilteredStream();
177         }
178         return retval;
179     }
180 }
Popular Tags