1 16 17 package org.apache.commons.io.output; 18 19 import java.io.IOException ; 20 import java.io.OutputStream ; 21 import java.io.UnsupportedEncodingException ; 22 import java.util.List ; 23 24 47 public class ByteArrayOutputStream extends OutputStream { 48 49 private List buffers = new java.util.ArrayList (); 50 private int currentBufferIndex; 51 private int filledBufferSum; 52 private byte[] currentBuffer; 53 private int count; 54 55 59 public ByteArrayOutputStream() { 60 this(1024); 61 } 62 63 70 public ByteArrayOutputStream(int size) { 71 if (size < 0) { 72 throw new IllegalArgumentException ( 73 "Negative initial size: " + size); 74 } 75 needNewBuffer(size); 76 } 77 78 private byte[] getBuffer(int index) { 79 return (byte[])buffers.get(index); 80 } 81 82 private void needNewBuffer(int newcount) { 83 if (currentBufferIndex < buffers.size() - 1) { 84 filledBufferSum += currentBuffer.length; 86 87 currentBufferIndex++; 88 currentBuffer = getBuffer(currentBufferIndex); 89 } else { 90 int newBufferSize; 92 if (currentBuffer == null) { 93 newBufferSize = newcount; 94 filledBufferSum = 0; 95 } else { 96 newBufferSize = Math.max( 97 currentBuffer.length << 1, 98 newcount - filledBufferSum); 99 filledBufferSum += currentBuffer.length; 100 } 101 102 currentBufferIndex++; 103 currentBuffer = new byte[newBufferSize]; 104 buffers.add(currentBuffer); 105 } 106 } 107 108 111 public synchronized void write(byte[] b, int off, int len) { 112 if ((off < 0) 113 || (off > b.length) 114 || (len < 0) 115 || ((off + len) > b.length) 116 || ((off + len) < 0)) { 117 throw new IndexOutOfBoundsException (); 118 } else if (len == 0) { 119 return; 120 } 121 int newcount = count + len; 122 int remaining = len; 123 int inBufferPos = count - filledBufferSum; 124 while (remaining > 0) { 125 int part = Math.min(remaining, currentBuffer.length - inBufferPos); 126 System.arraycopy(b, off + len - remaining, currentBuffer, inBufferPos, part); 127 remaining -= part; 128 if (remaining > 0) { 129 needNewBuffer(newcount); 130 inBufferPos = 0; 131 } 132 } 133 count = newcount; 134 } 135 136 141 public synchronized void write(int b) { 142 write(new byte[] {(byte)b}, 0, 1); 143 } 144 145 148 public int size() { 149 return count; 150 } 151 152 158 public void close() throws IOException { 159 } 161 162 165 public synchronized void reset() { 166 count = 0; 167 filledBufferSum = 0; 168 currentBufferIndex = 0; 169 currentBuffer = getBuffer(currentBufferIndex); 170 } 171 172 175 public synchronized void writeTo(OutputStream out) throws IOException { 176 int remaining = count; 177 for (int i = 0; i < buffers.size(); i++) { 178 byte[] buf = getBuffer(i); 179 int c = Math.min(buf.length, remaining); 180 out.write(buf, 0, c); 181 remaining -= c; 182 if (remaining == 0) { 183 break; 184 } 185 } 186 } 187 188 191 public synchronized byte toByteArray()[] { 192 int remaining = count; 193 int pos = 0; 194 byte newbuf[] = new byte[count]; 195 for (int i = 0; i < buffers.size(); i++) { 196 byte[] buf = getBuffer(i); 197 int c = Math.min(buf.length, remaining); 198 System.arraycopy(buf, 0, newbuf, pos, c); 199 pos += c; 200 remaining -= c; 201 if (remaining == 0) { 202 break; 203 } 204 } 205 return newbuf; 206 } 207 208 211 public String toString() { 212 return new String (toByteArray()); 213 } 214 215 218 public String toString(String enc) throws UnsupportedEncodingException { 219 return new String (toByteArray(), enc); 220 } 221 222 } 223 | Popular Tags |