KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdfwriter > COSStandardOutputStream


1 /**
2  * Copyright (c) 2003, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdfwriter;
32
33
34
35 import java.io.FilterOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.OutputStream JavaDoc;
38
39 /**
40  * simple output stream with some minor features for generating "pretty"
41  * pdf files.
42  *
43  * @author Michael Traut
44  * @version $Revision: 1.5 $
45  */

46 public class COSStandardOutputStream extends FilterOutputStream JavaDoc
47 {
48
49     /**
50      * To be used when 2 byte sequence is enforced.
51      */

52     public static final byte[] CRLF = "\r\n".getBytes();
53
54     /**
55      * Line feed character.
56      */

57     public static final byte[] LF = "\n".getBytes();
58
59     /**
60      * standard line separator on this platform.
61      */

62     public static final byte[] EOL = System.getProperty("line.separator").getBytes();
63
64     // current byte pos in the output stream
65
private long pos = 0;
66     // flag to prevent generating two newlines in sequence
67
private boolean onNewLine = false;
68
69     /**
70      * COSOutputStream constructor comment.
71      *
72      * @param out The underlying stream to write to.
73      */

74     public COSStandardOutputStream(OutputStream JavaDoc out)
75     {
76         super(out);
77     }
78     /**
79      * This will get the current position in the stream.
80      *
81      * @return The current position in the stream.
82      */

83     public long getPos()
84     {
85         return pos;
86     }
87     /**
88      * This will tell if we are on a newling.
89      *
90      * @return true If we are on a newline.
91      */

92     public boolean isOnNewLine()
93     {
94         return onNewLine;
95     }
96     /**
97      * This will set a flag telling if we are on a newline.
98      *
99      * @param newOnNewLine The new value for the onNewLine attribute.
100      */

101     public void setOnNewLine(boolean newOnNewLine)
102     {
103         onNewLine = newOnNewLine;
104     }
105
106     /**
107      * This will write some byte to the stream.
108      *
109      * @param b The source byte array.
110      * @param off The offset into the array to start writing.
111      * @param len The number of bytes to write.
112      *
113      * @throws IOException If the underlying stream throws an exception.
114      */

115     public void write(byte[] b, int off, int len) throws IOException JavaDoc
116     {
117         setOnNewLine(false);
118         out.write(b, off, len);
119         pos += len;
120     }
121
122     /**
123      * This will write a single byte to the stream.
124      *
125      * @param b The byte to write to the stream.
126      *
127      * @throws IOException If there is an error writing to the underlying stream.
128      */

129     public void write(int b) throws IOException JavaDoc
130     {
131         setOnNewLine(false);
132         out.write(b);
133         pos++;
134     }
135
136     /**
137      * This will write a CRLF to the stream.
138      *
139      * @throws IOException If there is an error writing the data to the stream.
140      */

141     public void writeCRLF() throws IOException JavaDoc
142     {
143         write(CRLF);
144         // setOnNewLine(true);
145
}
146
147     /**
148      * This will write an EOL to the stream.
149      *
150      * @throws IOException If there is an error writing to the stream
151      */

152     public void writeEOL() throws IOException JavaDoc
153     {
154         if (!isOnNewLine())
155         {
156             write(EOL);
157             setOnNewLine(true);
158         }
159     }
160
161     /**
162      * This will write a Linefeed to the stream.
163      *
164      * @throws IOException If there is an error writing to the underlying stream.
165      */

166     public void writeLF() throws IOException JavaDoc
167     {
168         write(LF);
169         // setOnNewLine(true);
170
}
171 }
Popular Tags