1 16 17 package org.apache.axis.utils; 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 Messages.getMessage("illegalArgumentException01", 74 Integer.toString(size))); 75 } 76 needNewBuffer(size); 77 } 78 79 private byte[] getBuffer(int index) { 80 return (byte[]) buffers.get(index); 81 } 82 83 private void needNewBuffer(int newcount) { 84 if (currentBufferIndex < buffers.size() - 1) { 85 filledBufferSum += currentBuffer.length; 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(currentBuffer.length << 1, 97 newcount - filledBufferSum); 98 filledBufferSum += currentBuffer.length; 99 } 100 currentBufferIndex++; 101 currentBuffer = new byte[newBufferSize]; 102 buffers.add(currentBuffer); 103 } 104 } 105 106 109 public synchronized void write(byte[] b, int off, int len) { 110 if ((off < 0) 111 || (off > b.length) 112 || (len < 0) 113 || ((off + len) > b.length) 114 || ((off + len) < 0)) { 115 throw new IndexOutOfBoundsException ( 116 Messages.getMessage("indexOutOfBoundsException00")); 117 } else if (len == 0) { 118 return; 119 } 120 int newcount = count + len; 121 int remaining = len; 122 int inBufferPos = count - filledBufferSum; 123 while (remaining > 0) { 124 int part = Math.min(remaining, currentBuffer.length - inBufferPos); 125 System.arraycopy(b, off + len - remaining, currentBuffer, 126 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 159 public void close() throws IOException { 160 } 162 163 166 public synchronized void reset() { 167 count = 0; 168 filledBufferSum = 0; 169 currentBufferIndex = 0; 170 currentBuffer = getBuffer(currentBufferIndex); 171 } 172 173 176 public synchronized void writeTo(OutputStream out) throws IOException { 177 int remaining = count; 178 for (int i = 0; i < buffers.size(); i++) { 179 byte[] buf = getBuffer(i); 180 int c = Math.min(buf.length, remaining); 181 out.write(buf, 0, c); 182 remaining -= c; 183 if (remaining == 0) { 184 break; 185 } 186 } 187 } 188 189 192 public synchronized byte toByteArray()[] { 193 int remaining = count; 194 int pos = 0; 195 byte newbuf[] = new byte[count]; 196 for (int i = 0; i < buffers.size(); i++) { 197 byte[] buf = getBuffer(i); 198 int c = Math.min(buf.length, remaining); 199 System.arraycopy(buf, 0, newbuf, pos, c); 200 pos += c; 201 remaining -= c; 202 if (remaining == 0) { 203 break; 204 } 205 } 206 return newbuf; 207 } 208 209 212 public String toString() { 213 return new String (toByteArray()); 214 } 215 216 219 public String toString(String enc) throws UnsupportedEncodingException { 220 return new String (toByteArray(), enc); 221 } 222 223 } 224 | Popular Tags |