001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.common;
031:
032: import java.io.ByteArrayOutputStream;
033: import java.io.ByteArrayInputStream;
034: import java.io.InputStream;
035: import java.io.IOException;
036:
037: import org.pdfbox.cos.COSBase;
038: import org.pdfbox.cos.COSStream;
039: import org.pdfbox.cos.COSString;
040:
041: /**
042: * A PDTextStream class is used when the PDF specification supports either
043: * a string or a stream for the value of an object. This is usually when
044: * a value could be large or small, for example a JavaScript method. This
045: * class will help abstract that and give a single unified interface to
046: * those types of fields.
047: *
048: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
049: * @version $Revision: 1.3 $
050: */
051: public class PDTextStream implements COSObjectable {
052: private COSString string;
053: private COSStream stream;
054:
055: /**
056: * Constructor.
057: *
058: * @param str The string parameter.
059: */
060: public PDTextStream(COSString str) {
061: string = str;
062: }
063:
064: /**
065: * Constructor.
066: *
067: * @param str The string parameter.
068: */
069: public PDTextStream(String str) {
070: string = new COSString(str);
071: }
072:
073: /**
074: * Constructor.
075: *
076: * @param str The stream parameter.
077: */
078: public PDTextStream(COSStream str) {
079: stream = str;
080: }
081:
082: /**
083: * This will create the text stream object. base must either be a string
084: * or a stream.
085: *
086: * @param base The COS text stream object.
087: *
088: * @return A PDTextStream that wraps the base object.
089: */
090: public static PDTextStream createTextStream(COSBase base) {
091: PDTextStream retval = null;
092: if (base instanceof COSString) {
093: retval = new PDTextStream((COSString) base);
094: } else if (base instanceof COSStream) {
095: retval = new PDTextStream((COSStream) base);
096: }
097: return retval;
098: }
099:
100: /**
101: * Convert this standard java object to a COS object.
102: *
103: * @return The cos object that matches this Java object.
104: */
105: public COSBase getCOSObject() {
106: COSBase retval = null;
107: if (string == null) {
108: retval = stream;
109: } else {
110: retval = string;
111: }
112: return retval;
113: }
114:
115: /**
116: * This will get this value as a string. If this is a stream then it
117: * will load the entire stream into memory, so you should only do this when
118: * the stream is a manageable size.
119: *
120: * @return This value as a string.
121: *
122: * @throws IOException If an IO error occurs while accessing the stream.
123: */
124: public String getAsString() throws IOException {
125: String retval = null;
126: if (string != null) {
127: retval = string.getString();
128: } else {
129: ByteArrayOutputStream out = new ByteArrayOutputStream();
130: byte[] buffer = new byte[1024];
131: int amountRead = -1;
132: InputStream is = stream.getUnfilteredStream();
133: while ((amountRead = is.read(buffer)) != -1) {
134: out.write(buffer, 0, amountRead);
135: }
136: retval = new String(out.toByteArray());
137: }
138: return retval;
139: }
140:
141: /**
142: * This is the preferred way of getting data with this class as it uses
143: * a stream object.
144: *
145: * @return The stream object.
146: *
147: * @throws IOException If an IO error occurs while accessing the stream.
148: */
149: public InputStream getAsStream() throws IOException {
150: InputStream retval = null;
151: if (string != null) {
152: retval = new ByteArrayInputStream(string.getBytes());
153: } else {
154: retval = stream.getUnfilteredStream();
155: }
156: return retval;
157: }
158: }
|