Back to index

wims  3.65+svn20090927
ByteVector.java
Go to the documentation of this file.
00001 /*
00002 $Id: ByteVector.java,v 1.2 2003/02/18 11:48:48 sander Exp $
00003 */
00004 
00005 
00006 package fr.ove.utils;
00007 
00008 public class ByteVector implements java.io.Serializable {
00009     protected byte elementData[];
00010     protected int elementCount;
00011     protected int capacityIncrement;
00012 
00013     public ByteVector(int initialCapacity, int capacityIncrement) {
00014        super();
00015        this.elementData = new byte[initialCapacity];
00016        this.capacityIncrement = capacityIncrement;
00017     }
00018 
00019     public ByteVector(int initialCapacity) {
00020        this(initialCapacity, 5);
00021     }
00022 
00023     public ByteVector() {
00024        this(10);
00025     }
00026 
00027     public final synchronized void trimToSize() {
00028        int oldCapacity = elementData.length;
00029        if (elementCount < oldCapacity) {
00030            byte oldData[] = elementData;
00031            elementData = new byte[elementCount];
00032            System.arraycopy(oldData, 0, elementData, 0, elementCount);
00033        }
00034     }
00035 
00036     public final int size() {
00037        return elementCount;
00038     }
00039 
00040     public final boolean isEmpty() {
00041        return elementCount == 0;
00042     }
00043 
00044     public final boolean contains(byte elem) {
00045        return indexOf(elem, 0) >= 0;
00046     }
00047 
00048     public final int indexOf(byte elem) {
00049        return indexOf(elem, 0);
00050     }
00051 
00052     public final synchronized int indexOf(byte elem, int index) {
00053        for (int i = index; i < elementCount; i++) {
00054            if (elem == elementData[i])
00055                      return i;
00056        }
00057        return -1;
00058     }
00059 
00060     public final synchronized byte elementAt(int index) {
00061        if (index >= elementCount)
00062            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
00063 
00064        try {
00065            return elementData[index];
00066        } catch (ArrayIndexOutOfBoundsException e) {
00067            throw new ArrayIndexOutOfBoundsException(index + " < 0");
00068        }
00069     }
00070 
00071     public final synchronized void addElement(byte b) {
00072            ensureCapacity(elementCount + 1);
00073        elementData[elementCount++] = b;
00074     }
00075 
00076     public final synchronized void addElements(byte[] b) {
00077         if (b != null) {
00078            ensureCapacity(elementCount + b.length);
00079             System.arraycopy(b, 0, elementData, elementCount, b.length);
00080             elementCount += b.length;
00081         }
00082         else
00083            throw new NullPointerException("Try to add null elements");
00084     }
00085 
00086     public final synchronized void addElements(byte[] b, int length) {
00087         if (b != null) {
00088             if ((length > 0) && (length <= b.length)) {
00089                   ensureCapacity(elementCount + b.length);
00090                 System.arraycopy(b, 0, elementData, elementCount, length);
00091                 elementCount += length;
00092             }
00093             else
00094                   throw new IllegalArgumentException("Invalid length argument : " + length);
00095         }
00096         else
00097            throw new NullPointerException("Try to add null elements");
00098     }
00099 
00100     public final synchronized boolean removeElement(byte b) {
00101        int i = indexOf(b);
00102        if (i >= 0) {
00103            removeElementAt(i);
00104            return true;
00105        }
00106        return false;
00107     }
00108 
00109     public final synchronized void removeElementAt(int index) {
00110        if (index >= elementCount) {
00111            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
00112        }
00113        else if (index < 0) {
00114            throw new ArrayIndexOutOfBoundsException(index);
00115        }
00116        int j = elementCount - index - 1;
00117        if (j > 0) {
00118            System.arraycopy(elementData, index + 1, elementData, index, j);
00119        }
00120        elementCount--;
00121     }
00122 
00123     public final synchronized void removeAllElements() {
00124        elementCount = 0;
00125        elementData = new byte[capacityIncrement];
00126     }
00127 
00128     public final synchronized byte[] getBytes() {
00129         trimToSize();
00130            return elementData;
00131     }
00132 
00133     public final synchronized void ensureCapacity(int minCapacity) {
00134        int oldCapacity = elementData.length;
00135        if (minCapacity > oldCapacity) {
00136            byte oldData[] = elementData;
00137            int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement) : (oldCapacity * 2);
00138               if (newCapacity < minCapacity)
00139                      newCapacity = minCapacity;
00140            elementData = new byte[newCapacity];
00141            System.arraycopy(oldData, 0, elementData, 0, elementCount);
00142        }
00143     }
00144 
00145     public final synchronized String toString() {
00146         String s = " [ ";
00147        for (int i = 0 ; i <elementCount ; i++)
00148            s += elementData[i] + " ";
00149        s += "]";
00150        return s;
00151     }
00152 }