FileDocCategorySizeDatePackage
ByteArray.javaAPI DocApache Axis 1.410333Sat Apr 22 18:57:28 BST 2006org.apache.axis.utils

ByteArray

public class ByteArray extends OutputStream
Class ByteArray

Fields Summary
protected static double
DEFAULT_CACHE_INCREMENT
protected static int
DEFAULT_RESIDENT_SIZE
protected static boolean
DEFAULT_ENABLE_BACKING_STORE
protected static int
WORKING_BUFFER_SIZE
protected org.apache.axis.utils.ByteArrayOutputStream
cache
protected int
max_size
protected File
bs_handle
protected OutputStream
bs_stream
protected long
count
protected boolean
enableBackingStore
Constructors Summary
public ByteArray(int max_resident_size)
Constructor ByteArray

param
max_resident_size

        this(0, max_resident_size);
    
public ByteArray(int probable_size, int max_resident_size)
Constructor ByteArray

param
probable_size
param
max_resident_size

        if (probable_size > max_resident_size) {
            probable_size = 0;
        }
        if (probable_size < WORKING_BUFFER_SIZE) {
            probable_size = WORKING_BUFFER_SIZE;
        }
        cache = new org.apache.axis.utils.ByteArrayOutputStream(probable_size);
        max_size = max_resident_size;
    
public ByteArray()
Constructor ByteArray

        String value;
        value = AxisProperties.getProperty(
                AxisEngine.PROP_BYTE_BUFFER_CACHE_INCREMENT,
                "" + DEFAULT_CACHE_INCREMENT);
        DEFAULT_CACHE_INCREMENT = Double.parseDouble(value);
        value = AxisProperties.getProperty(
                AxisEngine.PROP_BYTE_BUFFER_RESIDENT_MAX_SIZE,
                "" + DEFAULT_RESIDENT_SIZE);
        DEFAULT_RESIDENT_SIZE = Integer.parseInt(value);
        value = AxisProperties.getProperty(
                AxisEngine.PROP_BYTE_BUFFER_WORK_BUFFER_SIZE,
                "" + WORKING_BUFFER_SIZE);
        WORKING_BUFFER_SIZE = Integer.parseInt(value);
        value =
                AxisProperties.getProperty(AxisEngine.PROP_BYTE_BUFFER_BACKING,
                        "" + DEFAULT_ENABLE_BACKING_STORE);
        if (value.equalsIgnoreCase("true") ||
                value.equals("1") ||
                value.equalsIgnoreCase("yes")) {
            DEFAULT_ENABLE_BACKING_STORE = true;
        } else {
            DEFAULT_ENABLE_BACKING_STORE = false;
        }
    
        this(DEFAULT_RESIDENT_SIZE);
    
Methods Summary
public voidclose()
Method close

throws
IOException

        if (bs_stream != null) {
            bs_stream.close();
            bs_stream = null;
        }
    
protected java.io.InputStreamcreateBackingStoreInputStream()
Method createBackingStoreInputStream

return
throws
FileNotFoundException

        try {
            return new BufferedInputStream(
                    new FileInputStream(bs_handle.getCanonicalPath()));
        } catch (IOException e) {
            throw new FileNotFoundException(bs_handle.getAbsolutePath());
        }
    
protected voiddiscardBackingStore()
Method discardBackingStore

        if (bs_handle != null) {
            bs_handle.delete();
            bs_handle = null;
        }
    
public synchronized voiddiscardBuffer()
Method discardBuffer

        cache = null;
        if (bs_stream != null) {
            try {
                bs_stream.close();
            } catch (IOException e) {
                // just ignore it...
            }
            bs_stream = null;
        }
        discardBackingStore();
    
protected voidfinalize()
Method finalize

        discardBuffer();
    
public voidflush()
Method flush

throws
IOException

        if (bs_stream != null) {
            bs_stream.flush();
        }
    
public java.lang.StringgetBackingStoreFileName()
Method getBackingStoreFileName

throws
IOException

        String fileName = null;
        if (bs_handle != null) {
            fileName = bs_handle.getCanonicalPath();
        }
        return fileName;
    
public static doublegetDEFAULT_CACHE_INCREMENT()

        return DEFAULT_CACHE_INCREMENT;
    
public static intgetDEFAULT_RESIDENT_SIZE()

        return DEFAULT_RESIDENT_SIZE;
    
protected voidincreaseCapacity(int count)
Method increaseCapacity

param
count
throws
IOException

        if (cache == null) {
            return;
        }
        if (count + cache.size() <= max_size) {
            return;
        } else if (enableBackingStore) {
            switchToBackingStore();
        } else {
            throw new IOException("ByteArray can not increase capacity by " +
                    count +
                    " due to max size limit of " + max_size);
        }
    
public static booleanisDEFAULT_ENABLE_BACKING_STORE()

        return DEFAULT_ENABLE_BACKING_STORE;
    
public booleanisEnableBackingStore()


       
        return enableBackingStore;
    
protected java.io.InputStreammakeInputStream()
Method makeInputStream

return
throws
IOException
throws
FileNotFoundException

        close();
        if (cache != null) {
            return new ByteArrayInputStream(cache.toByteArray());
        } else if (bs_handle != null) {
            return createBackingStoreInputStream();
        } else {
            return null;
        }
    
public static voidsetDEFAULT_CACHE_INCREMENT(double DEFAULT_CACHE_INCREMENT)

        ByteArray.DEFAULT_CACHE_INCREMENT = DEFAULT_CACHE_INCREMENT;
    
public static voidsetDEFAULT_ENABLE_BACKING_STORE(boolean DEFAULT_ENABLE_BACKING_STORE)

        ByteArray.DEFAULT_ENABLE_BACKING_STORE = DEFAULT_ENABLE_BACKING_STORE;
    
public static voidsetDEFAULT_RESIDENT_SIZE(int DEFAULT_RESIDENT_SIZE)

        ByteArray.DEFAULT_RESIDENT_SIZE = DEFAULT_RESIDENT_SIZE;
    
public voidsetEnableBackingStore(boolean enableBackingStore)

        this.enableBackingStore = enableBackingStore;
    
public longsize()
Method size

return

        return count;
    
protected voidswitchToBackingStore()
Method switchToBackingStore

throws
IOException

        bs_handle = File.createTempFile("Axis", ".msg");
        bs_handle.createNewFile();
        bs_handle.deleteOnExit();
        bs_stream = new FileOutputStream(bs_handle);
        bs_stream.write(cache.toByteArray());
        cache = null;
    
public byte[]toByteArray()
Method toByteArray

return
throws
IOException

        InputStream inp = this.makeInputStream();
        byte[] buf = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        buf = new byte[WORKING_BUFFER_SIZE];
        int len;
        while ((len = inp.read(buf, 0, WORKING_BUFFER_SIZE)) != -1) {
            baos.write(buf, 0, len);
        }
        inp.close();
        discardBackingStore();
        return baos.toByteArray();
    
public voidwrite(byte[] bytes)
Method write

param
bytes
throws
IOException

        write(bytes, 0, bytes.length);
    
public voidwrite(byte[] bytes, int start, int length)
Method write

param
bytes
param
start
param
length
throws
IOException

        count += length;
        if (cache != null) {
            increaseCapacity(length);
        }
        if (cache != null) {
            cache.write(bytes, start, length);
        } else if (bs_stream != null) {
            bs_stream.write(bytes, start, length);
        } else {
            throw new IOException("ByteArray does not have a backing store!");
        }
    
public voidwrite(int b)
Method write

param
b
throws
IOException

        count += 1;
        if (cache != null) {
            increaseCapacity(1);
        }
        if (cache != null) {
            cache.write(b);
        } else if (bs_stream != null) {
            bs_stream.write(b);
        } else {
            throw new IOException("ByteArray does not have a backing store!");
        }
    
public voidwriteTo(java.io.OutputStream os)
Method writeTo

param
os
throws
IOException

        InputStream inp = this.makeInputStream();
        byte[] buf = null;
        buf = new byte[WORKING_BUFFER_SIZE];
        int len;
        while ((len = inp.read(buf, 0, WORKING_BUFFER_SIZE)) != -1) {
            os.write(buf, 0, len);
        }
        inp.close();
        discardBackingStore();