org.apache.pdfbox.pdmodel.common
public class: PDTextStream [javadoc |
source]
java.lang.Object
org.apache.pdfbox.pdmodel.common.PDTextStream
All Implemented Interfaces:
COSObjectable
A PDTextStream class is used when the PDF specification supports either
a string or a stream for the value of an object. This is usually when
a value could be large or small, for example a JavaScript method. This
class will help abstract that and give a single unified interface to
those types of fields.
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from org.apache.pdfbox.pdmodel.common.PDTextStream Detail: |
public static PDTextStream createTextStream(COSBase base) {
PDTextStream retval = null;
if( base instanceof COSString )
{
retval = new PDTextStream( (COSString) base );
}
else if( base instanceof COSStream )
{
retval = new PDTextStream( (COSStream)base );
}
return retval;
}
This will create the text stream object. base must either be a string
or a stream. |
public InputStream getAsStream() throws IOException {
InputStream retval = null;
if( string != null )
{
retval = new ByteArrayInputStream( string.getBytes() );
}
else
{
retval = stream.getUnfilteredStream();
}
return retval;
}
This is the preferred way of getting data with this class as it uses
a stream object. |
public String getAsString() throws IOException {
String retval = null;
if( string != null )
{
retval = string.getString();
}
else
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[ 1024 ];
int amountRead = -1;
InputStream is = stream.getUnfilteredStream();
while( (amountRead = is.read( buffer ) ) != -1 )
{
out.write( buffer, 0, amountRead );
}
retval = new String( out.toByteArray() );
}
return retval;
}
This will get this value as a string. If this is a stream then it
will load the entire stream into memory, so you should only do this when
the stream is a manageable size. |
public COSBase getCOSObject() {
COSBase retval = null;
if( string == null )
{
retval = stream;
}
else
{
retval = string;
}
return retval;
}
Convert this standard java object to a COS object. |