1 16 package org.apache.axis.utils; 17 18 import org.apache.axis.AxisEngine; 19 import org.apache.axis.AxisProperties; 20 21 import java.io.BufferedInputStream ; 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 32 35 public class ByteArray extends OutputStream { 36 37 protected static double DEFAULT_CACHE_INCREMENT = 2.5; 38 protected static int DEFAULT_RESIDENT_SIZE = 512 * 1024 * 1024; protected static boolean DEFAULT_ENABLE_BACKING_STORE = true; 40 protected static int WORKING_BUFFER_SIZE = 8192; 41 42 protected org.apache.axis.utils.ByteArrayOutputStream cache = null; 43 44 protected int max_size = 0; 45 protected File bs_handle = null; 46 protected OutputStream bs_stream = null; 47 protected long count = 0; 48 protected boolean enableBackingStore = DEFAULT_ENABLE_BACKING_STORE; 49 50 public boolean isEnableBackingStore() { 51 return enableBackingStore; 52 } 53 54 public void setEnableBackingStore(boolean enableBackingStore) { 55 this.enableBackingStore = enableBackingStore; 56 } 57 58 public static boolean isDEFAULT_ENABLE_BACKING_STORE() { 59 return DEFAULT_ENABLE_BACKING_STORE; 60 } 61 62 public static void setDEFAULT_ENABLE_BACKING_STORE( 63 boolean DEFAULT_ENABLE_BACKING_STORE) { 64 ByteArray.DEFAULT_ENABLE_BACKING_STORE = DEFAULT_ENABLE_BACKING_STORE; 65 } 66 67 public static int getDEFAULT_RESIDENT_SIZE() { 68 return DEFAULT_RESIDENT_SIZE; 69 } 70 71 public static void setDEFAULT_RESIDENT_SIZE(int DEFAULT_RESIDENT_SIZE) { 72 ByteArray.DEFAULT_RESIDENT_SIZE = DEFAULT_RESIDENT_SIZE; 73 } 74 75 public static double getDEFAULT_CACHE_INCREMENT() { 76 return DEFAULT_CACHE_INCREMENT; 77 } 78 79 public static void setDEFAULT_CACHE_INCREMENT( 80 double DEFAULT_CACHE_INCREMENT) { 81 ByteArray.DEFAULT_CACHE_INCREMENT = DEFAULT_CACHE_INCREMENT; 82 } 83 84 static { 85 String value; 86 value = AxisProperties.getProperty( 87 AxisEngine.PROP_BYTE_BUFFER_CACHE_INCREMENT, 88 "" + DEFAULT_CACHE_INCREMENT); 89 DEFAULT_CACHE_INCREMENT = Double.parseDouble(value); 90 value = AxisProperties.getProperty( 91 AxisEngine.PROP_BYTE_BUFFER_RESIDENT_MAX_SIZE, 92 "" + DEFAULT_RESIDENT_SIZE); 93 DEFAULT_RESIDENT_SIZE = Integer.parseInt(value); 94 value = AxisProperties.getProperty( 95 AxisEngine.PROP_BYTE_BUFFER_WORK_BUFFER_SIZE, 96 "" + WORKING_BUFFER_SIZE); 97 WORKING_BUFFER_SIZE = Integer.parseInt(value); 98 value = 99 AxisProperties.getProperty(AxisEngine.PROP_BYTE_BUFFER_BACKING, 100 "" + DEFAULT_ENABLE_BACKING_STORE); 101 if (value.equalsIgnoreCase("true") || 102 value.equals("1") || 103 value.equalsIgnoreCase("yes")) { 104 DEFAULT_ENABLE_BACKING_STORE = true; 105 } else { 106 DEFAULT_ENABLE_BACKING_STORE = false; 107 } 108 } 109 110 113 public ByteArray() { 114 this(DEFAULT_RESIDENT_SIZE); 115 } 116 117 122 public ByteArray(int max_resident_size) { 123 this(0, max_resident_size); 124 } 125 126 132 public ByteArray(int probable_size, int max_resident_size) { 133 if (probable_size > max_resident_size) { 134 probable_size = 0; 135 } 136 if (probable_size < WORKING_BUFFER_SIZE) { 137 probable_size = WORKING_BUFFER_SIZE; 138 } 139 cache = new org.apache.axis.utils.ByteArrayOutputStream(probable_size); 140 max_size = max_resident_size; 141 } 142 143 149 public void write(byte bytes[]) throws IOException { 150 write(bytes, 0, bytes.length); 151 } 152 153 161 public void write(byte bytes[], int start, int length) throws IOException { 162 count += length; 163 if (cache != null) { 164 increaseCapacity(length); 165 } 166 if (cache != null) { 167 cache.write(bytes, start, length); 168 } else if (bs_stream != null) { 169 bs_stream.write(bytes, start, length); 170 } else { 171 throw new IOException ("ByteArray does not have a backing store!"); 172 } 173 } 174 175 181 public void write(int b) throws IOException { 182 count += 1; 183 if (cache != null) { 184 increaseCapacity(1); 185 } 186 if (cache != null) { 187 cache.write(b); 188 } else if (bs_stream != null) { 189 bs_stream.write(b); 190 } else { 191 throw new IOException ("ByteArray does not have a backing store!"); 192 } 193 } 194 195 200 public void close() throws IOException { 201 if (bs_stream != null) { 202 bs_stream.close(); 203 bs_stream = null; 204 } 205 } 206 207 212 public long size() { 213 return count; 214 } 215 216 221 public void flush() throws IOException { 222 if (bs_stream != null) { 223 bs_stream.flush(); 224 } 225 } 226 227 233 protected void increaseCapacity(int count) throws IOException { 234 if (cache == null) { 235 return; 236 } 237 if (count + cache.size() <= max_size) { 238 return; 239 } else if (enableBackingStore) { 240 switchToBackingStore(); 241 } else { 242 throw new IOException ("ByteArray can not increase capacity by " + 243 count + 244 " due to max size limit of " + max_size); 245 } 246 } 247 248 251 public synchronized void discardBuffer() { 252 cache = null; 253 if (bs_stream != null) { 254 try { 255 bs_stream.close(); 256 } catch (IOException e) { 257 } 259 bs_stream = null; 260 } 261 discardBackingStore(); 262 } 263 264 271 protected InputStream makeInputStream() 272 throws IOException , FileNotFoundException { 273 close(); 274 if (cache != null) { 275 return new ByteArrayInputStream (cache.toByteArray()); 276 } else if (bs_handle != null) { 277 return createBackingStoreInputStream(); 278 } else { 279 return null; 280 } 281 } 282 283 286 protected void finalize() { 287 discardBuffer(); 288 } 289 290 295 protected void switchToBackingStore() throws IOException { 296 bs_handle = File.createTempFile("Axis", ".msg"); 297 bs_handle.createNewFile(); 298 bs_handle.deleteOnExit(); 299 bs_stream = new FileOutputStream (bs_handle); 300 bs_stream.write(cache.toByteArray()); 301 cache = null; 302 } 303 304 309 public String getBackingStoreFileName() throws IOException { 310 String fileName = null; 311 if (bs_handle != null) { 312 fileName = bs_handle.getCanonicalPath(); 313 } 314 return fileName; 315 } 316 317 320 protected void discardBackingStore() { 321 if (bs_handle != null) { 322 bs_handle.delete(); 323 bs_handle = null; 324 } 325 } 326 327 333 protected InputStream createBackingStoreInputStream() 334 throws FileNotFoundException { 335 try { 336 return new BufferedInputStream ( 337 new FileInputStream (bs_handle.getCanonicalPath())); 338 } catch (IOException e) { 339 throw new FileNotFoundException (bs_handle.getAbsolutePath()); 340 } 341 } 342 343 349 public byte[] toByteArray() throws IOException { 350 InputStream inp = this.makeInputStream(); 351 byte[] buf = null; 352 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 353 buf = new byte[WORKING_BUFFER_SIZE]; 354 int len; 355 while ((len = inp.read(buf, 0, WORKING_BUFFER_SIZE)) != -1) { 356 baos.write(buf, 0, len); 357 } 358 inp.close(); 359 discardBackingStore(); 360 return baos.toByteArray(); 361 } 362 363 369 public void writeTo(OutputStream os) throws IOException { 370 InputStream inp = this.makeInputStream(); 371 byte[] buf = null; 372 buf = new byte[WORKING_BUFFER_SIZE]; 373 int len; 374 while ((len = inp.read(buf, 0, WORKING_BUFFER_SIZE)) != -1) { 375 os.write(buf, 0, len); 376 } 377 inp.close(); 378 discardBackingStore(); 379 } 380 } 381 | Popular Tags |