1 7 8 package org.jboss.cache.util; 9 10 import java.io.ByteArrayOutputStream ; 11 12 32 public class ExposedByteArrayOutputStream extends ByteArrayOutputStream 33 { 34 38 public static final int DEFAULT_DOUBLING_SIZE = 4 * 1024 * 1024; 40 private int maxDoublingSize = DEFAULT_DOUBLING_SIZE; 41 42 public ExposedByteArrayOutputStream() 43 { 44 super(); 45 } 46 47 public ExposedByteArrayOutputStream(int size) 48 { 49 super(size); 50 } 51 52 63 public ExposedByteArrayOutputStream(int size, int maxDoublingSize) 64 { 65 super(size); 66 this.maxDoublingSize = maxDoublingSize; 67 } 68 69 74 public byte[] getRawBuffer() { 75 return buf; 76 } 77 78 public synchronized void write(byte[] b, int off, int len) 79 { 80 if ((off < 0) || (off > b.length) || (len < 0) || 81 ((off + len) > b.length) || ((off + len) < 0)) { 82 throw new IndexOutOfBoundsException (); 83 } 84 else if (len == 0) { 85 return; 86 } 87 88 int newcount = count + len; 89 if (newcount > buf.length) { 90 byte newbuf[] = new byte[getNewBufferSize(buf.length, newcount)]; 91 System.arraycopy(buf, 0, newbuf, 0, count); 92 buf = newbuf; 93 } 94 95 System.arraycopy(b, off, buf, count, len); 96 count = newcount; 97 } 98 99 public synchronized void write(int b) 100 { 101 int newcount = count + 1; 102 if (newcount > buf.length) { 103 byte newbuf[] = new byte[getNewBufferSize(buf.length, newcount)]; 104 System.arraycopy(buf, 0, newbuf, 0, count); 105 buf = newbuf; 106 } 107 buf[count] = (byte)b; 108 count = newcount; 109 } 110 111 115 public int getMaxDoublingSize() 116 { 117 return maxDoublingSize; 118 } 119 120 127 public int getNewBufferSize(int curSize, int minNewSize) 128 { 129 if (curSize <= maxDoublingSize) 130 return Math.max(curSize << 1, minNewSize); 131 else 132 return Math.max(curSize + (curSize >> 2), minNewSize); 133 } 134 135 } 136 | Popular Tags |