KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > DocumentOutputStream


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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         
18
19 package org.apache.poi.poifs.filesystem;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 /**
26  * This class provides a wrapper over an OutputStream so that Document
27  * writers can't accidently go over their size limits
28  *
29  * @author Marc Johnson (mjohnson at apache dot org)
30  */

31
32 public class DocumentOutputStream
33     extends OutputStream
34 {
35     private OutputStream stream;
36     private int limit;
37     private int written;
38
39     /**
40      * Create a DocumentOutputStream
41      *
42      * @param stream the OutputStream to which the data is actually
43      * read
44      * @param limit the maximum number of bytes that can be written
45      */

46
47     DocumentOutputStream(final OutputStream stream, final int limit)
48     {
49         this.stream = stream;
50         this.limit = limit;
51         this.written = 0;
52     }
53
54     /**
55      * Writes the specified byte to this output stream. The general
56      * contract for write is that one byte is written to the output
57      * stream. The byte to be written is the eight low-order bits of
58      * the argument b. The 24 high-order bits of b are ignored.
59      *
60      * @param b the byte.
61      * @exception IOException if an I/O error occurs. In particular,
62      * an IOException may be thrown if the
63      * output stream has been closed, or if the
64      * writer tries to write too much data.
65      */

66
67     public void write(final int b)
68         throws IOException
69     {
70         limitCheck(1);
71         stream.write(b);
72     }
73
74     /**
75      * Writes b.length bytes from the specified byte array
76      * to this output stream.
77      *
78      * @param b the data.
79      * @exception IOException if an I/O error occurs.
80      */

81
82     public void write(final byte b[])
83         throws IOException
84     {
85         write(b, 0, b.length);
86     }
87
88     /**
89      * Writes len bytes from the specified byte array starting at
90      * offset off to this output stream. The general contract for
91      * write(b, off, len) is that some of the bytes in the array b are
92      * written to the output stream in order; element b[off] is the
93      * first byte written and b[off+len-1] is the last byte written by
94      * this operation.<p>
95      * If b is null, a NullPointerException is thrown.<p>
96      * If off is negative, or len is negative, or off+len is greater
97      * than the length of the array b, then an
98      * IndexOutOfBoundsException is thrown.
99      *
100      * @param b the data.
101      * @param off the start offset in the data.
102      * @param len the number of bytes to write.
103      * @exception IOException if an I/O error occurs. In particular,
104      * an IOException</code> is thrown if the
105      * output stream is closed or if the writer
106      * tries to write too many bytes.
107      */

108
109     public void write(final byte b[], final int off, final int len)
110         throws IOException
111     {
112         limitCheck(len);
113         stream.write(b, off, len);
114     }
115
116     /**
117      * Flushes this output stream and forces any buffered output bytes
118      * to be written out.
119      *
120      * @exception IOException if an I/O error occurs.
121      */

122
123     public void flush()
124         throws IOException
125     {
126         stream.flush();
127     }
128
129     /**
130      * Closes this output stream and releases any system resources
131      * associated with this stream. The general contract of close is
132      * that it closes the output stream. A closed stream cannot
133      * perform output operations and cannot be reopened.
134      *
135      * @exception IOException if an I/O error occurs.
136      */

137
138     public void close()
139         throws IOException
140     {
141
142         // ignore this call
143
}
144
145     /**
146      * write the rest of the document's data (fill in at the end)
147      *
148      * @param totalLimit the actual number of bytes the corresponding
149      * document must fill
150      * @param fill the byte to fill remaining space with
151      *
152      * @exception IOException on I/O error
153      */

154
155     void writeFiller(final int totalLimit, final byte fill)
156         throws IOException
157     {
158         if (totalLimit > written)
159         {
160             byte[] filler = new byte[ totalLimit - written ];
161
162             Arrays.fill(filler, fill);
163             stream.write(filler);
164         }
165     }
166
167     private void limitCheck(final int toBeWritten)
168         throws IOException
169     {
170         if ((written + toBeWritten) > limit)
171         {
172             throw new IOException("tried to write too much data");
173         }
174         written += toBeWritten;
175     }
176 } // end public class DocumentOutputStream
177

178
Popular Tags