Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.common » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.pdmodel.common;
   18   
   19   import java.io.ByteArrayOutputStream;
   20   import java.io.ByteArrayInputStream;
   21   import java.io.InputStream;
   22   import java.io.IOException;
   23   
   24   import org.apache.pdfbox.cos.COSBase;
   25   import org.apache.pdfbox.cos.COSStream;
   26   import org.apache.pdfbox.cos.COSString;
   27   
   28   /**
   29    * A PDTextStream class is used when the PDF specification supports either
   30    * a string or a stream for the value of an object.  This is usually when
   31    * a value could be large or small, for example a JavaScript method.  This
   32    * class will help abstract that and give a single unified interface to
   33    * those types of fields.
   34    *
   35    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   36    * @version $Revision: 1.3 $
   37    */
   38   public class PDTextStream implements COSObjectable
   39   {
   40       private COSString string;
   41       private COSStream stream;
   42   
   43       /**
   44        * Constructor.
   45        *
   46        * @param str The string parameter.
   47        */
   48       public PDTextStream( COSString str )
   49       {
   50           string = str;
   51       }
   52   
   53       /**
   54        * Constructor.
   55        *
   56        * @param str The string parameter.
   57        */
   58       public PDTextStream( String str )
   59       {
   60           string = new COSString( str );
   61       }
   62   
   63       /**
   64        * Constructor.
   65        *
   66        * @param str The stream parameter.
   67        */
   68       public PDTextStream( COSStream str )
   69       {
   70           stream = str;
   71       }
   72   
   73       /**
   74        * This will create the text stream object.  base must either be a string
   75        * or a stream.
   76        *
   77        * @param base The COS text stream object.
   78        *
   79        * @return A PDTextStream that wraps the base object.
   80        */
   81       public static PDTextStream createTextStream( COSBase base )
   82       {
   83           PDTextStream retval = null;
   84           if( base instanceof COSString )
   85           {
   86               retval = new PDTextStream( (COSString) base );
   87           }
   88           else if( base instanceof COSStream )
   89           {
   90               retval = new PDTextStream( (COSStream)base );
   91           }
   92           return retval;
   93       }
   94   
   95       /**
   96        * Convert this standard java object to a COS object.
   97        *
   98        * @return The cos object that matches this Java object.
   99        */
  100       public COSBase getCOSObject()
  101       {
  102           COSBase retval = null;
  103           if( string == null )
  104           {
  105               retval = stream;
  106           }
  107           else
  108           {
  109               retval = string;
  110           }
  111           return retval;
  112       }
  113   
  114       /**
  115        * This will get this value as a string.  If this is a stream then it
  116        * will load the entire stream into memory, so you should only do this when
  117        * the stream is a manageable size.
  118        *
  119        * @return This value as a string.
  120        *
  121        * @throws IOException If an IO error occurs while accessing the stream.
  122        */
  123       public String getAsString() throws IOException
  124       {
  125           String retval = null;
  126           if( string != null )
  127           {
  128               retval = string.getString();
  129           }
  130           else
  131           {
  132               ByteArrayOutputStream out = new ByteArrayOutputStream();
  133               byte[] buffer = new byte[ 1024 ];
  134               int amountRead = -1;
  135               InputStream is = stream.getUnfilteredStream();
  136               while( (amountRead = is.read( buffer ) ) != -1 )
  137               {
  138                   out.write( buffer, 0, amountRead );
  139               }
  140               retval = new String( out.toByteArray() );
  141           }
  142           return retval;
  143       }
  144   
  145       /**
  146        * This is the preferred way of getting data with this class as it uses
  147        * a stream object.
  148        *
  149        * @return The stream object.
  150        *
  151        * @throws IOException If an IO error occurs while accessing the stream.
  152        */
  153       public InputStream getAsStream() throws IOException
  154       {
  155           InputStream retval = null;
  156           if( string != null )
  157           {
  158               retval = new ByteArrayInputStream( string.getBytes() );
  159           }
  160           else
  161           {
  162               retval = stream.getUnfilteredStream();
  163           }
  164           return retval;
  165       }
  166   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.common » [javadoc | source]