1 2 /* ==================================================================== 3 * The Apache Software License, Version 1.1 4 * 5 * Copyright (c) 2002 The Apache Software Foundation. All rights 6 * reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The end-user documentation included with the redistribution, 21 * if any, must include the following acknowledgment: 22 * "This product includes software developed by the 23 * Apache Software Foundation (http://www.apache.org/)." 24 * Alternately, this acknowledgment may appear in the software itself, 25 * if and wherever such third-party acknowledgments normally appear. 26 * 27 * 4. The names "Apache" and "Apache Software Foundation" and 28 * "Apache POI" must not be used to endorse or promote products 29 * derived from this software without prior written permission. For 30 * written permission, please contact apache@apache.org. 31 * 32 * 5. Products derived from this software may not be called "Apache", 33 * "Apache POI", nor may "Apache" appear in their name, without 34 * prior written permission of the Apache Software Foundation. 35 * 36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * ==================================================================== 49 * 50 * This software consists of voluntary contributions made by many 51 * individuals on behalf of the Apache Software Foundation. For more 52 * information on the Apache Software Foundation, please see 53 * <http://www.apache.org/>. 54 */ 55 56 package org.apache.poi.poifs.filesystem; 57 58 import java.io.*; 59 60 import java.util.*; 61 62 /** 63 * This class provides a wrapper over an OutputStream so that Document 64 * writers can't accidently go over their size limits 65 * 66 * @author Marc Johnson (mjohnson at apache dot org) 67 */ 68 69 public class DocumentOutputStream 70 extends OutputStream 71 { 72 private OutputStream stream; 73 private int limit; 74 private int written; 75 76 /** 77 * Create a DocumentOutputStream 78 * 79 * @param stream the OutputStream to which the data is actually 80 * read 81 * @param limit the maximum number of bytes that can be written 82 */ 83 84 DocumentOutputStream(final OutputStream stream, final int limit) 85 { 86 this.stream = stream; 87 this.limit = limit; 88 this.written = 0; 89 } 90 91 /** 92 * Writes the specified byte to this output stream. The general 93 * contract for write is that one byte is written to the output 94 * stream. The byte to be written is the eight low-order bits of 95 * the argument b. The 24 high-order bits of b are ignored. 96 * 97 * @param b the byte. 98 * @exception IOException if an I/O error occurs. In particular, 99 * an IOException may be thrown if the 100 * output stream has been closed, or if the 101 * writer tries to write too much data. 102 */ 103 104 public void write(final int b) 105 throws IOException 106 { 107 limitCheck(1); 108 stream.write(b); 109 } 110 111 /** 112 * Writes b.length bytes from the specified byte array 113 * to this output stream. 114 * 115 * @param b the data. 116 * @exception IOException if an I/O error occurs. 117 */ 118 119 public void write(final byte b[]) 120 throws IOException 121 { 122 write(b, 0, b.length); 123 } 124 125 /** 126 * Writes len bytes from the specified byte array starting at 127 * offset off to this output stream. The general contract for 128 * write(b, off, len) is that some of the bytes in the array b are 129 * written to the output stream in order; element b[off] is the 130 * first byte written and b[off+len-1] is the last byte written by 131 * this operation.<p> 132 * If b is null, a NullPointerException is thrown.<p> 133 * If off is negative, or len is negative, or off+len is greater 134 * than the length of the array b, then an 135 * IndexOutOfBoundsException is thrown. 136 * 137 * @param b the data. 138 * @param off the start offset in the data. 139 * @param len the number of bytes to write. 140 * @exception IOException if an I/O error occurs. In particular, 141 * an IOException</code> is thrown if the 142 * output stream is closed or if the writer 143 * tries to write too many bytes. 144 */ 145 146 public void write(final byte b[], final int off, final int len) 147 throws IOException 148 { 149 limitCheck(len); 150 stream.write(b, off, len); 151 } 152 153 /** 154 * Flushes this output stream and forces any buffered output bytes 155 * to be written out. 156 * 157 * @exception IOException if an I/O error occurs. 158 */ 159 160 public void flush() 161 throws IOException 162 { 163 stream.flush(); 164 } 165 166 /** 167 * Closes this output stream and releases any system resources 168 * associated with this stream. The general contract of close is 169 * that it closes the output stream. A closed stream cannot 170 * perform output operations and cannot be reopened. 171 * 172 * @exception IOException if an I/O error occurs. 173 */ 174 175 public void close() 176 throws IOException 177 { 178 179 // ignore this call 180 } 181 182 /** 183 * write the rest of the document's data (fill in at the end) 184 * 185 * @param totalLimit the actual number of bytes the corresponding 186 * document must fill 187 * @param fill the byte to fill remaining space with 188 * 189 * @exception IOException on I/O error 190 */ 191 192 void writeFiller(final int totalLimit, final byte fill) 193 throws IOException 194 { 195 if (totalLimit > written) 196 { 197 byte[] filler = new byte[ totalLimit - written ]; 198 199 Arrays.fill(filler, fill); 200 stream.write(filler); 201 } 202 } 203 204 private void limitCheck(final int toBeWritten) 205 throws IOException 206 { 207 if ((written + toBeWritten) > limit) 208 { 209 throw new IOException("tried to write too much data"); 210 } 211 written += toBeWritten; 212 } 213 } // end public class DocumentOutputStream 214 215