1 5 package com.opensymphony.webwork.util; 6 7 import java.io.IOException ; 8 import java.io.OutputStream ; 9 import java.io.RandomAccessFile ; 10 import java.io.Writer ; 11 import java.util.Iterator ; 12 import java.util.LinkedList ; 13 14 15 23 public class FastByteArrayOutputStream extends OutputStream { 24 26 private static final int DEFAULT_BLOCK_SIZE = 8192; 28 29 31 private LinkedList buffers; 32 33 private byte[] buffer; 36 37 private boolean closed; 39 private int blockSize; 40 private int index; 41 private int size; 42 43 45 public FastByteArrayOutputStream() { 47 this(DEFAULT_BLOCK_SIZE); 48 } 49 50 public FastByteArrayOutputStream(int aSize) { 51 blockSize = aSize; 52 buffer = new byte[blockSize]; 53 } 54 55 57 public int getSize() { 58 return size + index; 59 } 60 61 public void close() { 62 closed = true; 63 } 64 65 public byte[] toByteArray() { 66 byte[] data = new byte[getSize()]; 67 68 int pos = 0; 70 71 if (buffers != null) { 72 Iterator iter = buffers.iterator(); 73 74 while (iter.hasNext()) { 75 byte[] bytes = (byte[]) iter.next(); 76 System.arraycopy(bytes, 0, data, pos, blockSize); 77 pos += blockSize; 78 } 79 } 80 81 System.arraycopy(buffer, 0, data, pos, index); 83 84 return data; 85 } 86 87 public String toString() { 88 return new String (toByteArray()); 89 } 90 91 public void write(int datum) throws IOException { 93 if (closed) { 94 throw new IOException ("Stream closed"); 95 } else { 96 if (index == blockSize) { 97 addBuffer(); 98 } 99 100 buffer[index++] = (byte) datum; 102 } 103 } 104 105 public void write(byte[] data, int offset, int length) throws IOException { 106 if (data == null) { 107 throw new NullPointerException (); 108 } else if ((offset < 0) || ((offset + length) > data.length) || (length < 0)) { 109 throw new IndexOutOfBoundsException (); 110 } else if (closed) { 111 throw new IOException ("Stream closed"); 112 } else { 113 if ((index + length) > blockSize) { 114 int copyLength; 115 116 do { 117 if (index == blockSize) { 118 addBuffer(); 119 } 120 121 copyLength = blockSize - index; 122 123 if (length < copyLength) { 124 copyLength = length; 125 } 126 127 System.arraycopy(data, offset, buffer, index, copyLength); 128 offset += copyLength; 129 index += copyLength; 130 length -= copyLength; 131 } while (length > 0); 132 } else { 133 System.arraycopy(data, offset, buffer, index, length); 135 index += length; 136 } 137 } 138 } 139 140 public void writeTo(OutputStream out) throws IOException { 142 if (buffers != null) { 144 Iterator iter = buffers.iterator(); 145 146 while (iter.hasNext()) { 147 byte[] bytes = (byte[]) iter.next(); 148 out.write(bytes, 0, blockSize); 149 } 150 } 151 152 out.write(buffer, 0, index); 154 } 155 156 public void writeTo(RandomAccessFile out) throws IOException { 157 if (buffers != null) { 159 Iterator iter = buffers.iterator(); 160 161 while (iter.hasNext()) { 162 byte[] bytes = (byte[]) iter.next(); 163 out.write(bytes, 0, blockSize); 164 } 165 } 166 167 out.write(buffer, 0, index); 169 } 170 171 public void writeTo(Writer out, String encoding) throws IOException { 172 if (buffers != null) { 174 Iterator iter = buffers.iterator(); 175 176 while (iter.hasNext()) { 177 byte[] bytes = (byte[]) iter.next(); 178 179 if (encoding != null) { 180 out.write(new String (bytes, encoding)); 181 } else { 182 out.write(new String (bytes)); 183 } 184 } 185 } 186 187 if (encoding != null) { 189 out.write(new String (buffer, 0, index, encoding)); 190 } else { 191 out.write(new String (buffer, 0, index)); 192 } 193 } 194 195 199 protected void addBuffer() { 200 if (buffers == null) { 201 buffers = new LinkedList (); 202 } 203 204 buffers.addLast(buffer); 205 206 buffer = new byte[blockSize]; 207 size += index; 208 index = 0; 209 } 210 } 211 | Popular Tags |