]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - jars/icu4j-4_2_1-src/src/com/ibm/icu/util/ByteArrayWrapper.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / util / ByteArrayWrapper.java
old mode 100755 (executable)
new mode 100644 (file)
index d84ee12..92bff19
-//##header\r
-/**\r
- *******************************************************************************\r
- * Copyright (C) 1996-2009, International Business Machines Corporation and    *\r
- * others. All Rights Reserved.                                                *\r
- *******************************************************************************\r
- */\r
-\r
-package com.ibm.icu.util;\r
-\r
-//#if defined(FOUNDATION10) || defined(J2SE13)\r
-//##import com.ibm.icu.impl.ByteBuffer;\r
-//#else\r
-import java.nio.ByteBuffer;\r
-//#endif\r
-import com.ibm.icu.impl.Utility;\r
-\r
-/**\r
- * <p>\r
- * A simple utility class to wrap a byte array.\r
- * </p>\r
- * <p>\r
- * Generally passed as an argument object into a method. The method takes\r
- * responsibility of writing into the internal byte array and increasing its\r
- * size when necessary.\r
- * </p> \r
- * @author syn wee\r
- * @stable ICU 2.8\r
- */\r
-public class ByteArrayWrapper implements Comparable\r
-{\r
-    // public data member ------------------------------------------------\r
-    \r
-    /**\r
-     * Internal byte array.\r
-     * @stable ICU 2.8\r
-     */\r
-    public byte[] bytes;\r
-\r
-    /**\r
-     * Size of the internal byte array used. \r
-     * Different from bytes.length, size will be &lt;= bytes.length. \r
-     * Semantics of size is similar to java.util.Vector.size().\r
-     * @stable ICU 2.8\r
-     */\r
-    public int size;\r
-    \r
-    // public constructor ------------------------------------------------\r
-\r
-    /** \r
-     * Construct a new ByteArrayWrapper with no data.\r
-     * @stable ICU 2.8\r
-     */\r
-    public ByteArrayWrapper() {\r
-        // leave bytes null, don't allocate twice\r
-    }\r
-\r
-    /**\r
-     * Construct a new ByteArrayWrapper from a byte array and size\r
-     * @param bytesToAdopt the byte array to adopt\r
-     * @param size the length of valid data in the byte array\r
-     * @throws IndexOutOfBoundsException if bytesToAdopt == null and size != 0, or\r
-     * size < 0, or size > bytesToAdopt.length.\r
-     * @stable ICU 3.2\r
-     */\r
-    public ByteArrayWrapper(byte[] bytesToAdopt, int size) {\r
-        if ((bytesToAdopt == null && size != 0) || size < 0 || size > bytesToAdopt.length) {\r
-            throw new IndexOutOfBoundsException("illegal size: " + size);\r
-        }\r
-        this.bytes = bytesToAdopt;\r
-        this.size = size;\r
-    }\r
-\r
-    /**\r
-     * Construct a new ByteArrayWrapper from the contents of a ByteBuffer.\r
-     * @param source the ByteBuffer from which to get the data.\r
-     * @stable ICU 3.2\r
-     */\r
-    public ByteArrayWrapper(ByteBuffer source) {\r
-        size = source.limit();\r
-        bytes = new byte[size];\r
-        source.get(bytes,0,size);\r
-    }\r
-\r
-    /**\r
-     * Create from ByteBuffer\r
-     * @param byteBuffer\r
-    public ByteArrayWrapper(ByteArrayWrapper source) {\r
-        size = source.size;\r
-        bytes = new byte[size];\r
-        copyBytes(source.bytes, 0, bytes, 0, size);\r
-    }\r
-     */\r
-\r
-    /**\r
-     * create from byte buffer\r
-     * @param src\r
-     * @param start\r
-     * @param limit\r
-    public ByteArrayWrapper(byte[] src, int start, int limit) {\r
-        size = limit - start;\r
-        bytes = new byte[size];\r
-        copyBytes(src, start, bytes, 0, size);\r
-    }\r
-     */\r
-\r
-    // public methods ----------------------------------------------------\r
-\r
-    /**\r
-     * Ensure that the internal byte array is at least of length capacity.     \r
-     * If the byte array is null or its length is less than capacity, a new \r
-     * byte array of length capacity will be allocated.  \r
-     * The contents of the array (between 0 and size) remain unchanged. \r
-     * @param capacity minimum length of internal byte array.\r
-     * @return this ByteArrayWrapper\r
-     * @stable ICU 3.2\r
-     */\r
-    public ByteArrayWrapper ensureCapacity(int capacity) \r
-    {\r
-        if (bytes == null || bytes.length < capacity) {\r
-            byte[] newbytes = new byte[capacity];\r
-            copyBytes(bytes, 0, newbytes, 0, size);\r
-            bytes = newbytes;\r
-        }\r
-        return this;\r
-    }\r
-    \r
-    /**\r
-     * Set the internal byte array from offset 0 to (limit - start) with the \r
-     * contents of src from offset start to limit. If the byte array is null or its length is less than capacity, a new \r
-     * byte array of length (limit - start) will be allocated.  \r
-     * This resets the size of the internal byte array to (limit - start).\r
-     * @param src source byte array to copy from\r
-     * @param start start offset of src to copy from\r
-     * @param limit end + 1 offset of src to copy from\r
-     * @return this ByteArrayWrapper\r
-     * @stable ICU 3.2\r
-     */\r
-    public final ByteArrayWrapper set(byte[] src, int start, int limit) \r
-    {\r
-        size = 0;\r
-        append(src, start, limit);\r
-        return this;\r
-    }\r
-    \r
-    /*\r
-    public final ByteArrayWrapper get(byte[] target, int start, int limit) \r
-    {\r
-        int len = limit - start;\r
-        if (len > size) throw new IllegalArgumentException("limit too long");\r
-        copyBytes(bytes, 0, target, start, len);\r
-        return this;\r
-    }\r
-    */\r
-\r
-    /**\r
-     * Appends the internal byte array from offset size with the \r
-     * contents of src from offset start to limit. This increases the size of\r
-     * the internal byte array to (size + limit - start).\r
-     * @param src source byte array to copy from\r
-     * @param start start offset of src to copy from\r
-     * @param limit end + 1 offset of src to copy from\r
-     * @return this ByteArrayWrapper\r
-     * @stable ICU 3.2\r
-     */\r
-    public final ByteArrayWrapper append(byte[] src, int start, int limit) \r
-    {\r
-        int len = limit - start;\r
-        ensureCapacity(size + len);\r
-        copyBytes(src, start, bytes, size, len);\r
-        size += len;\r
-        return this;\r
-    }\r
-\r
-    /*\r
-    public final ByteArrayWrapper append(ByteArrayWrapper other) \r
-    {\r
-        return append(other.bytes, 0, other.size);\r
-    }\r
-    */\r
-\r
-    /**\r
-     * Releases the internal byte array to the caller, resets the internal\r
-     * byte array to null and its size to 0.\r
-     * @return internal byte array.\r
-     * @stable ICU 2.8\r
-     */\r
-    public final byte[] releaseBytes()\r
-    {\r
-        byte result[] = bytes;\r
-        bytes = null;\r
-        size = 0;\r
-        return result;\r
-    }\r
-    \r
-    // Boilerplate ----------------------------------------------------\r
-    \r
-    /**\r
-     * Returns string value for debugging\r
-     * @stable ICU 3.2\r
-     */\r
-    public String toString() {\r
-        StringBuffer result = new StringBuffer();\r
-        for (int i = 0; i < size; ++i) {\r
-            if (i != 0) result.append(" ");\r
-            result.append(Utility.hex(bytes[i]&0xFF,2));\r
-        }\r
-        return result.toString();\r
-    }\r
-\r
-    /**\r
-     * Return true if the bytes in each wrapper are equal.\r
-     * @param other the object to compare to.\r
-     * @return true if the two objects are equal.\r
-     * @stable ICU 3.2\r
-     */\r
-    public boolean equals(Object other) {\r
-        if (this == other) return true;\r
-        if (other == null) return false;\r
-        try {\r
-            ByteArrayWrapper that = (ByteArrayWrapper)other;\r
-            if (size != that.size) return false;\r
-            for (int i = 0; i < size; ++i) {\r
-                if (bytes[i] != that.bytes[i]) return false;\r
-            }\r
-            return true;\r
-        }\r
-        catch (ClassCastException e) {\r
-        }\r
-        return false;\r
-    }\r
-\r
-    /**\r
-     * Return the hashcode.\r
-     * @return the hashcode.\r
-     * @stable ICU 3.2\r
-     */\r
-    public int hashCode() {\r
-        int result = bytes.length;\r
-        for (int i = 0; i < size; ++i) {\r
-            result = 37*result + bytes[i];\r
-        }\r
-        return result;\r
-    }\r
-\r
-    /**\r
-     * Compare this object to another ByteArrayWrapper, which must not be null.\r
-     * @param other the object to compare to.\r
-     * @return a value <0, 0, or >0 as this compares less than, equal to, or\r
-     * greater than other.\r
-     * @throws ClassCastException if the other object is not a ByteArrayWrapper\r
-     * @stable ICU 3.2\r
-     */\r
-    public int compareTo(Object other) {\r
-        if (this == other) return 0;\r
-        ByteArrayWrapper that = (ByteArrayWrapper) other;\r
-        int minSize = size < that.size ? size : that.size;\r
-        for (int i = 0; i < minSize; ++i) {\r
-            if (bytes[i] != that.bytes[i]) {\r
-                return (bytes[i] & 0xFF) - (that.bytes[i] & 0xFF);\r
-            }\r
-        }\r
-        return size - that.size;\r
-    }\r
-    \r
-    // private methods -----------------------------------------------------\r
-    \r
-    /**\r
-     * Copies the contents of src byte array from offset srcoff to the \r
-     * target of tgt byte array at the offset tgtoff.\r
-     * @param src source byte array to copy from\r
-     * @param srcoff start offset of src to copy from\r
-     * @param tgt target byte array to copy to\r
-     * @param tgtoff start offset of tgt to copy to\r
-     * @param length size of contents to copy\r
-     */\r
-    private static final void copyBytes(byte[] src, int srcoff, byte[] tgt, \r
-                                       int tgtoff, int length) {\r
-        if (length < 64) {\r
-            for (int i = srcoff, n = tgtoff; -- length >= 0; ++ i, ++ n) {\r
-                tgt[n] = src[i];\r
-            }\r
-        } \r
-        else {\r
-            System.arraycopy(src, srcoff, tgt, tgtoff, length);\r
-        }\r
-    }      \r
-}\r
+//##header J2SE15
+/**
+ *******************************************************************************
+ * Copyright (C) 1996-2009, International Business Machines Corporation and    *
+ * others. All Rights Reserved.                                                *
+ *******************************************************************************
+ */
+
+package com.ibm.icu.util;
+
+//#if defined(FOUNDATION10) || defined(J2SE13)
+//##import com.ibm.icu.impl.ByteBuffer;
+//#else
+import java.nio.ByteBuffer;
+//#endif
+import com.ibm.icu.impl.Utility;
+
+/**
+ * <p>
+ * A simple utility class to wrap a byte array.
+ * </p>
+ * <p>
+ * Generally passed as an argument object into a method. The method takes
+ * responsibility of writing into the internal byte array and increasing its
+ * size when necessary.
+ * </p> 
+ * @author syn wee
+ * @stable ICU 2.8
+ */
+public class ByteArrayWrapper implements Comparable
+{
+    // public data member ------------------------------------------------
+    
+    /**
+     * Internal byte array.
+     * @stable ICU 2.8
+     */
+    public byte[] bytes;
+
+    /**
+     * Size of the internal byte array used. 
+     * Different from bytes.length, size will be &lt;= bytes.length. 
+     * Semantics of size is similar to java.util.Vector.size().
+     * @stable ICU 2.8
+     */
+    public int size;
+    
+    // public constructor ------------------------------------------------
+
+    /** 
+     * Construct a new ByteArrayWrapper with no data.
+     * @stable ICU 2.8
+     */
+    public ByteArrayWrapper() {
+        // leave bytes null, don't allocate twice
+    }
+
+    /**
+     * Construct a new ByteArrayWrapper from a byte array and size
+     * @param bytesToAdopt the byte array to adopt
+     * @param size the length of valid data in the byte array
+     * @throws IndexOutOfBoundsException if bytesToAdopt == null and size != 0, or
+     * size < 0, or size > bytesToAdopt.length.
+     * @stable ICU 3.2
+     */
+    public ByteArrayWrapper(byte[] bytesToAdopt, int size) {
+        if ((bytesToAdopt == null && size != 0) || size < 0 || size > bytesToAdopt.length) {
+            throw new IndexOutOfBoundsException("illegal size: " + size);
+        }
+        this.bytes = bytesToAdopt;
+        this.size = size;
+    }
+
+    /**
+     * Construct a new ByteArrayWrapper from the contents of a ByteBuffer.
+     * @param source the ByteBuffer from which to get the data.
+     * @stable ICU 3.2
+     */
+    public ByteArrayWrapper(ByteBuffer source) {
+        size = source.limit();
+        bytes = new byte[size];
+        source.get(bytes,0,size);
+    }
+
+    /**
+     * Create from ByteBuffer
+     * @param byteBuffer
+    public ByteArrayWrapper(ByteArrayWrapper source) {
+        size = source.size;
+        bytes = new byte[size];
+        copyBytes(source.bytes, 0, bytes, 0, size);
+    }
+     */
+
+    /**
+     * create from byte buffer
+     * @param src
+     * @param start
+     * @param limit
+    public ByteArrayWrapper(byte[] src, int start, int limit) {
+        size = limit - start;
+        bytes = new byte[size];
+        copyBytes(src, start, bytes, 0, size);
+    }
+     */
+
+    // public methods ----------------------------------------------------
+
+    /**
+     * Ensure that the internal byte array is at least of length capacity.     
+     * If the byte array is null or its length is less than capacity, a new 
+     * byte array of length capacity will be allocated.  
+     * The contents of the array (between 0 and size) remain unchanged. 
+     * @param capacity minimum length of internal byte array.
+     * @return this ByteArrayWrapper
+     * @stable ICU 3.2
+     */
+    public ByteArrayWrapper ensureCapacity(int capacity) 
+    {
+        if (bytes == null || bytes.length < capacity) {
+            byte[] newbytes = new byte[capacity];
+            copyBytes(bytes, 0, newbytes, 0, size);
+            bytes = newbytes;
+        }
+        return this;
+    }
+    
+    /**
+     * Set the internal byte array from offset 0 to (limit - start) with the 
+     * contents of src from offset start to limit. If the byte array is null or its length is less than capacity, a new 
+     * byte array of length (limit - start) will be allocated.  
+     * This resets the size of the internal byte array to (limit - start).
+     * @param src source byte array to copy from
+     * @param start start offset of src to copy from
+     * @param limit end + 1 offset of src to copy from
+     * @return this ByteArrayWrapper
+     * @stable ICU 3.2
+     */
+    public final ByteArrayWrapper set(byte[] src, int start, int limit) 
+    {
+        size = 0;
+        append(src, start, limit);
+        return this;
+    }
+    
+    /*
+    public final ByteArrayWrapper get(byte[] target, int start, int limit) 
+    {
+        int len = limit - start;
+        if (len > size) throw new IllegalArgumentException("limit too long");
+        copyBytes(bytes, 0, target, start, len);
+        return this;
+    }
+    */
+
+    /**
+     * Appends the internal byte array from offset size with the 
+     * contents of src from offset start to limit. This increases the size of
+     * the internal byte array to (size + limit - start).
+     * @param src source byte array to copy from
+     * @param start start offset of src to copy from
+     * @param limit end + 1 offset of src to copy from
+     * @return this ByteArrayWrapper
+     * @stable ICU 3.2
+     */
+    public final ByteArrayWrapper append(byte[] src, int start, int limit) 
+    {
+        int len = limit - start;
+        ensureCapacity(size + len);
+        copyBytes(src, start, bytes, size, len);
+        size += len;
+        return this;
+    }
+
+    /*
+    public final ByteArrayWrapper append(ByteArrayWrapper other) 
+    {
+        return append(other.bytes, 0, other.size);
+    }
+    */
+
+    /**
+     * Releases the internal byte array to the caller, resets the internal
+     * byte array to null and its size to 0.
+     * @return internal byte array.
+     * @stable ICU 2.8
+     */
+    public final byte[] releaseBytes()
+    {
+        byte result[] = bytes;
+        bytes = null;
+        size = 0;
+        return result;
+    }
+    
+    // Boilerplate ----------------------------------------------------
+    
+    /**
+     * Returns string value for debugging
+     * @stable ICU 3.2
+     */
+    public String toString() {
+        StringBuffer result = new StringBuffer();
+        for (int i = 0; i < size; ++i) {
+            if (i != 0) result.append(" ");
+            result.append(Utility.hex(bytes[i]&0xFF,2));
+        }
+        return result.toString();
+    }
+
+    /**
+     * Return true if the bytes in each wrapper are equal.
+     * @param other the object to compare to.
+     * @return true if the two objects are equal.
+     * @stable ICU 3.2
+     */
+    public boolean equals(Object other) {
+        if (this == other) return true;
+        if (other == null) return false;
+        try {
+            ByteArrayWrapper that = (ByteArrayWrapper)other;
+            if (size != that.size) return false;
+            for (int i = 0; i < size; ++i) {
+                if (bytes[i] != that.bytes[i]) return false;
+            }
+            return true;
+        }
+        catch (ClassCastException e) {
+        }
+        return false;
+    }
+
+    /**
+     * Return the hashcode.
+     * @return the hashcode.
+     * @stable ICU 3.2
+     */
+    public int hashCode() {
+        int result = bytes.length;
+        for (int i = 0; i < size; ++i) {
+            result = 37*result + bytes[i];
+        }
+        return result;
+    }
+
+    /**
+     * Compare this object to another ByteArrayWrapper, which must not be null.
+     * @param other the object to compare to.
+     * @return a value <0, 0, or >0 as this compares less than, equal to, or
+     * greater than other.
+     * @throws ClassCastException if the other object is not a ByteArrayWrapper
+     * @stable ICU 3.2
+     */
+    public int compareTo(Object other) {
+        if (this == other) return 0;
+        ByteArrayWrapper that = (ByteArrayWrapper) other;
+        int minSize = size < that.size ? size : that.size;
+        for (int i = 0; i < minSize; ++i) {
+            if (bytes[i] != that.bytes[i]) {
+                return (bytes[i] & 0xFF) - (that.bytes[i] & 0xFF);
+            }
+        }
+        return size - that.size;
+    }
+    
+    // private methods -----------------------------------------------------
+    
+    /**
+     * Copies the contents of src byte array from offset srcoff to the 
+     * target of tgt byte array at the offset tgtoff.
+     * @param src source byte array to copy from
+     * @param srcoff start offset of src to copy from
+     * @param tgt target byte array to copy to
+     * @param tgtoff start offset of tgt to copy to
+     * @param length size of contents to copy
+     */
+    private static final void copyBytes(byte[] src, int srcoff, byte[] tgt, 
+                                       int tgtoff, int length) {
+        if (length < 64) {
+            for (int i = srcoff, n = tgtoff; -- length >= 0; ++ i, ++ n) {
+                tgt[n] = src[i];
+            }
+        } 
+        else {
+            System.arraycopy(src, srcoff, tgt, tgtoff, length);
+        }
+    }      
+}