1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 31 32 public class DocumentOutputStream 33 extends OutputStream 34 { 35 private OutputStream stream; 36 private int limit; 37 private int written; 38 39 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 66 67 public void write(final int b) 68 throws IOException 69 { 70 limitCheck(1); 71 stream.write(b); 72 } 73 74 81 82 public void write(final byte b[]) 83 throws IOException 84 { 85 write(b, 0, b.length); 86 } 87 88 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 122 123 public void flush() 124 throws IOException 125 { 126 stream.flush(); 127 } 128 129 137 138 public void close() 139 throws IOException 140 { 141 142 } 144 145 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 } 178 | Popular Tags |