Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdfwriter » [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.pdfwriter;
   18   
   19   
   20   
   21   import java.io.FilterOutputStream;
   22   import java.io.IOException;
   23   import java.io.OutputStream;
   24   
   25   /**
   26    * simple output stream with some minor features for generating "pretty"
   27    * pdf files.
   28    *
   29    * @author Michael Traut
   30    * @version $Revision: 1.5 $
   31    */
   32   public class COSStandardOutputStream extends FilterOutputStream
   33   {
   34   
   35       /**
   36        * To be used when 2 byte sequence is enforced.
   37        */
   38       public static final byte[] CRLF = "\r\n".getBytes();
   39   
   40       /**
   41        * Line feed character.
   42        */
   43       public static final byte[] LF = "\n".getBytes();
   44   
   45       /**
   46        * standard line separator on this platform.
   47        */
   48       public static final byte[] EOL = System.getProperty("line.separator").getBytes();
   49   
   50       // current byte pos in the output stream
   51       private long pos = 0;
   52       // flag to prevent generating two newlines in sequence
   53       private boolean onNewLine = false;
   54   
   55       /**
   56        * COSOutputStream constructor comment.
   57        *
   58        * @param out The underlying stream to write to.
   59        */
   60       public COSStandardOutputStream(OutputStream out)
   61       {
   62           super(out);
   63       }
   64       /**
   65        * This will get the current position in the stream.
   66        *
   67        * @return The current position in the stream.
   68        */
   69       public long getPos()
   70       {
   71           return pos;
   72       }
   73       /**
   74        * This will tell if we are on a newling.
   75        *
   76        * @return true If we are on a newline.
   77        */
   78       public boolean isOnNewLine()
   79       {
   80           return onNewLine;
   81       }
   82       /**
   83        * This will set a flag telling if we are on a newline.
   84        *
   85        * @param newOnNewLine The new value for the onNewLine attribute.
   86        */
   87       public void setOnNewLine(boolean newOnNewLine)
   88       {
   89           onNewLine = newOnNewLine;
   90       }
   91   
   92       /**
   93        * This will write some byte to the stream.
   94        *
   95        * @param b The source byte array.
   96        * @param off The offset into the array to start writing.
   97        * @param len The number of bytes to write.
   98        *
   99        * @throws IOException If the underlying stream throws an exception.
  100        */
  101       public void write(byte[] b, int off, int len) throws IOException
  102       {
  103           setOnNewLine(false);
  104           out.write(b, off, len);
  105           pos += len;
  106       }
  107   
  108       /**
  109        * This will write a single byte to the stream.
  110        *
  111        * @param b The byte to write to the stream.
  112        *
  113        * @throws IOException If there is an error writing to the underlying stream.
  114        */
  115       public void write(int b) throws IOException
  116       {
  117           setOnNewLine(false);
  118           out.write(b);
  119           pos++;
  120       }
  121   
  122       /**
  123        * This will write a CRLF to the stream.
  124        *
  125        * @throws IOException If there is an error writing the data to the stream.
  126        */
  127       public void writeCRLF() throws IOException
  128       {
  129           write(CRLF);
  130           // setOnNewLine(true);
  131       }
  132   
  133       /**
  134        * This will write an EOL to the stream.
  135        *
  136        * @throws IOException If there is an error writing to the stream
  137        */
  138       public void writeEOL() throws IOException
  139       {
  140           if (!isOnNewLine())
  141           {
  142               write(EOL);
  143               setOnNewLine(true);
  144           }
  145       }
  146   
  147       /**
  148        * This will write a Linefeed to the stream.
  149        *
  150        * @throws IOException If there is an error writing to the underlying stream.
  151        */
  152       public void writeLF() throws IOException
  153       {
  154           write(LF);
  155           // setOnNewLine(true);
  156       }
  157   }

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