1 47 48 package com.lowagie.text.rtf.document.output; 49 50 import java.io.*; 51 52 59 public final class RtfByteArrayBuffer extends OutputStream 60 { 61 private final java.util.List arrays = new java.util.ArrayList (); 62 private byte[] buffer; 63 private int pos = 0; 64 private int size = 0; 65 66 69 public RtfByteArrayBuffer() 70 { 71 this(256); 72 } 73 78 public RtfByteArrayBuffer(final int bufferSize) 79 { 80 if((bufferSize <= 0) || (bufferSize > 1<<30)) throw(new IllegalArgumentException ("bufferSize "+bufferSize)); 81 82 int n = 1<<5; 83 while(n < bufferSize) { 84 n <<= 1; 85 } 86 buffer = new byte[n]; 87 } 88 89 public String toString() 90 { 91 return("RtfByteArrayBuffer: size="+size()+" #arrays="+arrays.size()+" pos="+pos); 92 } 93 94 97 public void reset() 98 { 99 arrays.clear(); 100 pos = 0; 101 size = 0; 102 } 103 104 109 public long size() 110 { 111 return(size); 112 } 113 114 private void flushBuffer() 115 { 116 flushBuffer(1); 117 } 118 private void flushBuffer(final int reqSize) 119 { 120 if(reqSize < 0) throw(new IllegalArgumentException ()); 121 122 if(pos == 0) return; 123 124 if(pos == buffer.length) { 125 arrays.add(buffer); 127 int newSize = buffer.length; 128 buffer = null; 129 final int MAX = Math.max(1, size>>24) << 16; 130 while(newSize < MAX) { 131 newSize <<= 1; 132 if(newSize >= reqSize) break; 133 } 134 buffer = new byte[newSize]; 135 } else { 136 final byte[] c = new byte[pos]; 138 System.arraycopy(buffer, 0, c, 0, pos); 139 arrays.add(c); 140 } 141 pos = 0; 142 } 143 144 149 public void write(final int b) 150 { 151 buffer[pos] = (byte)b; 152 size++; 153 if(++pos == buffer.length) flushBuffer(); 154 } 155 160 public void write(final byte[] src) 161 { 162 if(src == null) throw(new NullPointerException ()); 163 164 if(src.length < buffer.length - pos) { 165 System.arraycopy(src, 0, buffer, pos, src.length); 166 pos += src.length; 167 size += src.length; 168 return; 169 } 170 writeLoop(src, 0, src.length); 171 } 172 179 public void write(final byte[] src, int off, int len) 180 { 181 if(src == null) throw(new NullPointerException ()); 182 if((off < 0) || (off > src.length) || (len < 0) || ((off + len) > src.length) || ((off + len) < 0)) throw new IndexOutOfBoundsException (); 183 184 writeLoop(src, off, len); 185 } 186 private void writeLoop(final byte[] src, int off, int len) 187 { 188 while(len > 0) { 189 final int room = buffer.length - pos; 190 final int n = len > room ? room : len; 191 System.arraycopy(src, off, buffer, pos, n); 192 len -= n; 193 off += n; 194 pos += n; 195 size += n; 196 if(pos == buffer.length) flushBuffer(len); 197 } 198 } 199 200 207 public long write(final InputStream in) throws IOException 208 { 209 if(in == null) throw(new NullPointerException ()); 210 211 final long sizeStart = size; 212 while(true) { 213 final int n = in.read(buffer, pos, buffer.length - pos); 214 if(n < 0) break; 215 pos += n; 216 size += n; 217 if(pos == buffer.length) flushBuffer(); 218 } 219 return(size - sizeStart); 220 } 221 222 227 public void append(final byte[] a) 228 { 229 if(a == null) throw(new NullPointerException ()); 230 if(a.length == 0) return; 231 232 if(a.length <= 8) { 233 write(a, 0, a.length); 234 } else 235 if((a.length <= 16) && (pos > 0) && ((buffer.length - pos) > a.length)) { 236 write(a, 0, a.length); 237 } else { 238 flushBuffer(); 239 arrays.add(a); 240 size += a.length; 241 } 242 } 243 248 public void append(final byte[][] a) 249 { 250 if(a == null) throw(new NullPointerException ()); 251 252 for(int k = 0; k < a.length; k++) { 253 append(a[k]); 254 } 255 } 256 257 262 public byte[][] toByteArrayArray() 263 { 264 flushBuffer(); 265 return(byte[][])arrays.toArray(new byte[arrays.size()][]); 266 } 267 268 273 public byte[] toByteArray() 274 { 275 final byte[] r = new byte[size]; 276 int off = 0; 277 final int n = arrays.size(); 278 for(int k = 0; k < n; k++) { 279 byte[] src = (byte[])arrays.get(k); 280 System.arraycopy(src, 0, r, off, src.length); 281 off += src.length; 282 } 283 if(pos > 0) System.arraycopy(buffer, 0, r, off, pos); 284 return(r); 285 } 286 287 293 public void writeTo(final OutputStream out) throws IOException 294 { 295 if(out == null) throw(new NullPointerException ()); 296 297 final int n = arrays.size(); 298 for(int k = 0; k < n; k++) { 299 byte[] src = (byte[])arrays.get(k); 300 out.write(src); 301 } 302 if(pos > 0) out.write(buffer, 0, pos); 303 } 304 } 305 | Popular Tags |