1 21 22 package org.apache.derby.iapi.services.io; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import java.io.InputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 30 36 public class DynamicByteArrayOutputStream extends OutputStream { 37 38 private static int INITIAL_SIZE = 4096; 39 40 private byte[] buf; 41 private int position; 42 private int used; private int beginPosition; 44 45 public DynamicByteArrayOutputStream() { 46 this(INITIAL_SIZE); 47 } 48 49 public DynamicByteArrayOutputStream(int size) { 50 super(); 51 52 buf = new byte[size]; 53 } 54 55 public DynamicByteArrayOutputStream(byte[] data) { 56 super(); 57 58 buf = data; 59 } 60 61 public DynamicByteArrayOutputStream(DynamicByteArrayOutputStream toBeCloned) { 62 63 byte[] cbuf = toBeCloned.getByteArray(); 64 buf = new byte[cbuf.length]; 65 66 write(cbuf, 0, cbuf.length); 67 position = toBeCloned.getPosition(); 68 used = toBeCloned.getUsed(); 69 beginPosition = toBeCloned.getBeginPosition(); 70 } 71 72 75 public void write(int b) 76 { 77 if (position >= buf.length) 78 expandBuffer(INITIAL_SIZE); 79 80 buf[position++] = (byte) b; 81 82 if (position > used) 83 used = position; 84 } 85 86 public void write(byte[] b, int off, int len) 87 { 88 if ((position+len) > buf.length) 89 expandBuffer(len); 90 91 System.arraycopy(b, off, buf, position, len); 92 position += len; 93 94 if (position > used) 95 used = position; 96 } 97 98 void writeCompleteStream(InputStream dataIn, int len) throws IOException 99 { 100 if ((position+len) > buf.length) 101 expandBuffer(len); 102 103 org.apache.derby.iapi.services.io.InputStreamUtil.readFully(dataIn, buf, position, len); 104 position += len; 105 106 if (position > used) 107 used = position; 108 } 109 110 public void close() 111 { 112 buf = null; 113 reset(); 114 } 115 116 119 120 123 public void reset() 124 { 125 position = 0; 126 beginPosition = 0; 127 used = 0; 128 } 129 130 135 public byte[] getByteArray() 136 { 137 return buf; 138 } 139 140 143 public int getUsed() 144 { 145 return used; 146 } 147 148 151 public int getPosition() 152 { 153 return position; 154 } 155 156 159 public int getBeginPosition() 160 { 161 return beginPosition; 162 } 163 164 170 public void setPosition(int newPosition) 171 { 172 if (newPosition > position) 173 { 174 if (newPosition > buf.length) 175 expandBuffer(newPosition - buf.length); 176 } 177 178 position = newPosition; 179 180 if (position > used) 181 used = position; 182 183 return ; 184 } 185 186 191 public void setBeginPosition(int newBeginPosition) 192 { 193 194 if (newBeginPosition > buf.length) 195 return; 196 197 beginPosition = newBeginPosition; 198 } 199 200 204 public void discardLeft(int amountToShrinkBy) { 205 206 System.arraycopy(buf, amountToShrinkBy, buf, 0, 207 used - amountToShrinkBy); 208 209 position -= amountToShrinkBy; 210 used -= amountToShrinkBy; 211 } 212 213 228 private void expandBuffer(int minExtension) 229 { 230 if (buf.length < (128 * 1024)) { 231 if (minExtension < INITIAL_SIZE) 232 minExtension = INITIAL_SIZE; 233 } else if (buf.length < (1024 * 1024)) { 234 235 if (minExtension < (128 * 1024)) 236 minExtension = (128 * 1024); 237 } else { 238 if (minExtension < (1024 * 1024)) 239 minExtension = 1024 * 1024; 240 } 241 242 int newsize = buf.length + minExtension; 243 244 byte[] newbuf = new byte[newsize]; 245 System.arraycopy(buf, 0, newbuf, 0, buf.length); 246 buf = newbuf; 247 } 248 249 } 250 | Popular Tags |