1 3 package jodd.io; 4 5 import java.io.IOException ; 6 import java.io.OutputStream ; 7 import java.io.UnsupportedEncodingException ; 8 import java.util.List ; 9 10 33 public class FastByteArrayOutputStream extends OutputStream { 34 35 private List buffers = new java.util.ArrayList (); 36 private int currentBufferIndex; 37 private int filledBufferSum; 38 private byte[] currentBuffer; 39 private int count; 40 41 45 public FastByteArrayOutputStream() { 46 this(1024); 47 } 48 49 56 public FastByteArrayOutputStream(int size) { 57 if (size < 0) { 58 throw new IllegalArgumentException ("Negative initial size: " + size); 59 } 60 needNewBuffer(size); 61 } 62 63 private byte[] getBuffer(int index) { 64 return (byte[]) buffers.get(index); 65 } 66 67 private void needNewBuffer(int newcount) { 68 if (currentBufferIndex < buffers.size() - 1) { 69 filledBufferSum += currentBuffer.length; 71 72 currentBufferIndex++; 73 currentBuffer = getBuffer(currentBufferIndex); 74 } else { 75 int newBufferSize; 77 if (currentBuffer == null) { 78 newBufferSize = newcount; 79 filledBufferSum = 0; 80 } else { 81 newBufferSize = Math.max( 82 currentBuffer.length << 1, 83 newcount - filledBufferSum); 84 filledBufferSum += currentBuffer.length; 85 } 86 87 currentBufferIndex++; 88 currentBuffer = new byte[newBufferSize]; 89 buffers.add(currentBuffer); 90 } 91 } 92 93 96 public synchronized void write(byte[] b, int off, int len) { 97 if ((off < 0) 98 || (off > b.length) 99 || (len < 0) 100 || ((off + len) > b.length) 101 || ((off + len) < 0)) { 102 throw new IndexOutOfBoundsException (); 103 } else if (len == 0) { 104 return; 105 } 106 int newcount = count + len; 107 int remaining = len; 108 int inBufferPos = count - filledBufferSum; 109 while (remaining > 0) { 110 int part = Math.min(remaining, currentBuffer.length - inBufferPos); 111 System.arraycopy(b, off + len - remaining, currentBuffer, inBufferPos, part); 112 remaining -= part; 113 if (remaining > 0) { 114 needNewBuffer(newcount); 115 inBufferPos = 0; 116 } 117 } 118 count = newcount; 119 } 120 121 126 public synchronized void write(int b) { 127 write(new byte[]{(byte) b}, 0, 1); 128 } 129 130 133 public int size() { 134 return count; 135 } 136 137 142 public void close() { 143 } 145 146 149 public synchronized void reset() { 150 count = 0; 151 filledBufferSum = 0; 152 currentBufferIndex = 0; 153 currentBuffer = getBuffer(currentBufferIndex); 154 } 155 156 159 public synchronized void writeTo(OutputStream out) throws IOException { 160 int remaining = count; 161 for (int i = 0; i < buffers.size(); i++) { 162 byte[] buf = getBuffer(i); 163 int c = Math.min(buf.length, remaining); 164 out.write(buf, 0, c); 165 remaining -= c; 166 if (remaining == 0) { 167 break; 168 } 169 } 170 } 171 172 175 public synchronized byte toByteArray()[] { 176 int remaining = count; 177 int pos = 0; 178 byte newbuf[] = new byte[count]; 179 for (int i = 0; i < buffers.size(); i++) { 180 byte[] buf = getBuffer(i); 181 int c = Math.min(buf.length, remaining); 182 System.arraycopy(buf, 0, newbuf, pos, c); 183 pos += c; 184 remaining -= c; 185 if (remaining == 0) { 186 break; 187 } 188 } 189 return newbuf; 190 } 191 192 195 public String toString() { 196 return new String (toByteArray()); 197 } 198 199 202 public String toString(String enc) throws UnsupportedEncodingException { 203 return new String (toByteArray(), enc); 204 } 205 206 } 207 | Popular Tags |