1 31 package org.pdfbox.pdmodel.common; 32 33 import java.io.ByteArrayOutputStream ; 34 import java.io.ByteArrayInputStream ; 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 38 import org.pdfbox.cos.COSBase; 39 import org.pdfbox.cos.COSStream; 40 import org.pdfbox.cos.COSString; 41 42 52 public class PDTextStream implements COSObjectable 53 { 54 private COSString string; 55 private COSStream stream; 56 57 62 public PDTextStream( COSString str ) 63 { 64 string = str; 65 } 66 67 72 public PDTextStream( String str ) 73 { 74 string = new COSString( str ); 75 } 76 77 82 public PDTextStream( COSStream str ) 83 { 84 stream = str; 85 } 86 87 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 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 137 public String getAsString() throws IOException 138 { 139 String retval = null; 140 if( string != null ) 141 { 142 retval = string.getString(); 143 } 144 else 145 { 146 ByteArrayOutputStream out = new ByteArrayOutputStream (); 147 byte[] buffer = new byte[ 1024 ]; 148 int amountRead = -1; 149 InputStream is = stream.getUnfilteredStream(); 150 while( (amountRead = is.read( buffer ) ) != -1 ) 151 { 152 out.write( buffer, 0, amountRead ); 153 } 154 retval = new String ( out.toByteArray() ); 155 } 156 return retval; 157 } 158 159 167 public InputStream getAsStream() throws IOException 168 { 169 InputStream retval = null; 170 if( string != null ) 171 { 172 retval = new ByteArrayInputStream ( string.getBytes() ); 173 } 174 else 175 { 176 retval = stream.getUnfilteredStream(); 177 } 178 return retval; 179 } 180 } | Popular Tags |