1 18 package org.apache.activemq.util; 19 20 import java.io.OutputStream ; 21 22 23 28 public class ByteArrayOutputStream extends OutputStream { 29 30 byte buffer[]; 31 int size; 32 33 public ByteArrayOutputStream() { 34 this(1028); 35 } 36 public ByteArrayOutputStream(int capacity) { 37 buffer = new byte[capacity]; 38 } 39 40 public void write(int b) { 41 int newsize = size + 1; 42 checkCapacity(newsize); 43 buffer[size] = (byte) b; 44 size = newsize; 45 } 46 47 public void write(byte b[], int off, int len) { 48 int newsize = size + len; 49 checkCapacity(newsize); 50 System.arraycopy(b, off, buffer, size, len); 51 size = newsize; 52 } 53 54 58 private void checkCapacity(int minimumCapacity) { 59 if (minimumCapacity > buffer.length) { 60 byte b[] = new byte[Math.max(buffer.length << 1, minimumCapacity)]; 61 System.arraycopy(buffer, 0, b, 0, size); 62 buffer = b; 63 } 64 } 65 66 public void reset() { 67 size = 0; 68 } 69 70 public ByteSequence toByteSequence() { 71 return new ByteSequence(buffer, 0, size); 72 } 73 74 public byte[] toByteArray() { 75 byte rc[] = new byte[size]; 76 System.arraycopy(buffer, 0, rc, 0, size); 77 return rc; 78 } 79 80 public int size() { 81 return size; 82 } 83 } 84 | Popular Tags |