poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1053791 [7/18] - in /poi/branches/NIO_32_BRANCH: ./ src/contrib/src/org/apache/poi/contrib/poibrowser/ src/java/org/apache/poi/ src/java/org/apache/poi/hpsf/ src/java/org/apache/poi/hpsf/extractor/ src/java/org/apache/poi/hssf/record/ src/...
Date Thu, 30 Dec 2010 02:35:11 GMT
Modified: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/BinaryTree.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/BinaryTree.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/BinaryTree.java (original)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/BinaryTree.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,7 +14,6 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.util;
 
@@ -89,34 +87,20 @@ import java.util.*;
  *
  * @author Marc Johnson (mjohnson at apache dot org)
  */
-public final class BinaryTree   // final for performance
-
-    extends AbstractMap
-{
-    private Node[]                _root             = new Node[]
-    {
-        null, null
-    };
-    private int                   _size             = 0;
-    private int                   _modifications    = 0;
-    private Set[]                 _key_set          = new Set[]
-    {
-        null, null
-    };
-    private Set[]                 _entry_set        = new Set[]
-    {
-        null, null
-    };
-    private Collection[]          _value_collection = new Collection[]
-    {
-        null, null
-    };
-    private static final int      _KEY              = 0;
-    private static final int      _VALUE            = 1;
-    private static final int      _INDEX_SUM        = _KEY + _VALUE;
-    private static final int      _MINIMUM_INDEX    = 0;
-    private static final int      _INDEX_COUNT      = 2;
-    private static final String[] _data_name        = new String[]
+//for performance
+public class BinaryTree extends AbstractMap {
+    final Node[] _root;
+    int _size = 0;
+    int _modifications = 0;
+    private final Set[] _key_set = new Set[] { null, null };
+    private final Set[] _entry_set = new Set[] { null, null };
+    private final Collection[] _value_collection = new Collection[] { null, null };
+    static int      _KEY              = 0;
+    static int      _VALUE            = 1;
+    private static int      _INDEX_SUM        = _KEY + _VALUE;
+    private static int      _MINIMUM_INDEX    = 0;
+    private static int      _INDEX_COUNT      = 2;
+    private static String[] _data_name        = new String[]
     {
         "key", "value"
     };
@@ -124,9 +108,8 @@ public final class BinaryTree   // final
     /**
      * Construct a new BinaryTree
      */
-
-    public BinaryTree()
-    {
+    public BinaryTree() {
+        _root = new Node[]{ null, null, };
     }
 
     /**
@@ -146,11 +129,11 @@ public final class BinaryTree   // final
      *                                     or duplicate values in the
      *                                     map
      */
-
-    public BinaryTree(final Map map)
+    public BinaryTree(Map map)
         throws ClassCastException, NullPointerException,
                 IllegalArgumentException
     {
+    	this();
         putAll(map);
     }
 
@@ -167,8 +150,7 @@ public final class BinaryTree   // final
      *                               inappropriate type for this map.
      * @exception NullPointerException if the value is null
      */
-
-    public Object getKeyForValue(final Object value)
+    public Object getKeyForValue(Object value)
         throws ClassCastException, NullPointerException
     {
         return doGet(( Comparable ) value, _VALUE);
@@ -182,8 +164,7 @@ public final class BinaryTree   // final
      * @return previous key associated with specified value, or null
      *         if there was no mapping for value.
      */
-
-    public Object removeValue(final Object value)
+    public Object removeValue(Object value)
     {
         return doRemove(( Comparable ) value, _VALUE);
     }
@@ -207,7 +188,6 @@ public final class BinaryTree   // final
      *
      * @return a set view of the mappings contained in this map.
      */
-
     public Set entrySetByValue()
     {
         if (_entry_set[ _VALUE ] == null)
@@ -423,8 +403,7 @@ public final class BinaryTree   // final
      *         key. null if the specified key or value could not be
      *         found
      */
-
-    private Object doRemove(final Comparable o, final int index)
+    private Object doRemove(Comparable o, int index)
     {
         Node   node = lookup(o, index);
         Object rval = null;
@@ -447,8 +426,7 @@ public final class BinaryTree   // final
      *         key was mapped); null if we couldn't find the specified
      *         object
      */
-
-    private Object doGet(final Comparable o, final int index)
+    private Object doGet(Comparable o, int index)
     {
         checkNonNullComparable(o, index);
         Node node = lookup(o, index);
@@ -464,8 +442,7 @@ public final class BinaryTree   // final
      *
      * @return _VALUE (if _KEY was specified), else _KEY
      */
-
-    private int oppositeIndex(final int index)
+    private int oppositeIndex(int index)
     {
 
         // old trick ... to find the opposite of a value, m or n,
@@ -483,8 +460,7 @@ public final class BinaryTree   // final
      * @return the desired Node, or null if there is no mapping of the
      *         specified data
      */
-
-    private Node lookup(final Comparable data, final int index)
+    public Node lookup(Comparable data, int index)
     {
         Node rval = null;
         Node node = _root[ index ];
@@ -498,11 +474,8 @@ public final class BinaryTree   // final
                 rval = node;
                 break;
             }
-            else
-            {
-                node = (cmp < 0) ? node.getLeft(index)
-                                 : node.getRight(index);
-            }
+            node = (cmp < 0) ? node.getLeft(index)
+                             : node.getRight(index);
         }
         return rval;
     }
@@ -516,10 +489,9 @@ public final class BinaryTree   // final
      * @return negative value if o1 < o2; 0 if o1 == o2; positive
      *         value if o1 > o2
      */
-
-    private static int compare(final Comparable o1, final Comparable o2)
+    private static int compare(Comparable o1, Comparable o2)
     {
-        return (( Comparable ) o1).compareTo(o2);
+        return o1.compareTo(o2);
     }
 
     /**
@@ -532,8 +504,7 @@ public final class BinaryTree   // final
      * @return the smallest node, from the specified node, in the
      *         specified mapping
      */
-
-    private static Node leastNode(final Node node, final int index)
+    static Node leastNode(Node node, int index)
     {
         Node rval = node;
 
@@ -555,8 +526,7 @@ public final class BinaryTree   // final
      *
      * @return the specified node
      */
-
-    private Node nextGreater(final Node node, final int index)
+    static Node nextGreater(Node node, int index)
     {
         Node rval = null;
 
@@ -601,9 +571,7 @@ public final class BinaryTree   // final
      * @param to the node whose color we're changing; may be null
      * @param index _KEY or _VALUE
      */
-
-    private static void copyColor(final Node from, final Node to,
-                                  final int index)
+    private static void copyColor(Node from, Node to, int index)
     {
         if (to != null)
         {
@@ -627,11 +595,9 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static boolean isRed(final Node node, final int index)
+    private static boolean isRed(Node node, int index)
     {
-        return ((node == null) ? false
-                               : node.isRed(index));
+        return node == null ? false : node.isRed(index);
     }
 
     /**
@@ -641,11 +607,9 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static boolean isBlack(final Node node, final int index)
+    private static boolean isBlack(Node node, int index)
     {
-        return ((node == null) ? true
-                               : node.isBlack(index));
+        return node == null ? true : node.isBlack(index);
     }
 
     /**
@@ -654,8 +618,7 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static void makeRed(final Node node, final int index)
+    private static void makeRed(Node node, int index)
     {
         if (node != null)
         {
@@ -669,8 +632,7 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static void makeBlack(final Node node, final int index)
+    private static void makeBlack(Node node, int index)
     {
         if (node != null)
         {
@@ -685,8 +647,7 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static Node getGrandParent(final Node node, final int index)
+    private static Node getGrandParent(Node node, int index)
     {
         return getParent(getParent(node, index), index);
     }
@@ -698,8 +659,7 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static Node getParent(final Node node, final int index)
+    private static Node getParent(Node node, int index)
     {
         return ((node == null) ? null
                                : node.getParent(index));
@@ -712,8 +672,7 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static Node getRightChild(final Node node, final int index)
+    private static Node getRightChild(Node node, int index)
     {
         return (node == null) ? null
                               : node.getRight(index);
@@ -726,8 +685,7 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static Node getLeftChild(final Node node, final int index)
+    private static Node getLeftChild(Node node, int index)
     {
         return (node == null) ? null
                               : node.getLeft(index);
@@ -744,15 +702,14 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static boolean isLeftChild(final Node node, final int index)
-    {
-        return (node == null) ? true
-                              : ((node.getParent(index) == null) ? false
-                                                                 : (node
-                                                                    == node.getParent(
-                                                                        index).getLeft(
-                                                                        index)));
+    private static boolean isLeftChild(Node node, int index) {
+        if (node == null) {
+            return true;
+        }
+        if (node.getParent(index) == null) {
+            return false;
+        }
+        return node == node.getParent(index).getLeft(index);
     }
 
     /**
@@ -766,15 +723,15 @@ public final class BinaryTree   // final
      * @param node the node (may be null) in question
      * @param index _KEY or _VALUE
      */
-
-    private static boolean isRightChild(final Node node, final int index)
+    private static boolean isRightChild(Node node, int index)
     {
-        return (node == null) ? true
-                              : ((node.getParent(index) == null) ? false
-                                                                 : (node
-                                                                    == node.getParent(
-                                                                        index).getRight(
-                                                                        index)));
+        if (node == null) {
+            return true;
+        }
+        if (node.getParent(index) == null) {
+            return false;
+        }
+        return node == node.getParent(index).getRight(index);
     }
 
     /**
@@ -783,8 +740,7 @@ public final class BinaryTree   // final
      * @param node the node to be rotated
      * @param index _KEY or _VALUE
      */
-
-    private void rotateLeft(final Node node, final int index)
+    private void rotateLeft(Node node, int index)
     {
         Node right_child = node.getRight(index);
 
@@ -818,8 +774,7 @@ public final class BinaryTree   // final
      * @param node the node to be rotated
      * @param index _KEY or _VALUE
      */
-
-    private void rotateRight(final Node node, final int index)
+    private void rotateRight(Node node, int index)
     {
         Node left_child = node.getLeft(index);
 
@@ -854,8 +809,7 @@ public final class BinaryTree   // final
      * @param inserted_node the node to be inserted
      * @param index _KEY or _VALUE
      */
-
-    private void doRedBlackInsert(final Node inserted_node, final int index)
+    private void doRedBlackInsert(Node inserted_node, int index)
     {
         Node current_node = inserted_node;
 
@@ -931,8 +885,7 @@ public final class BinaryTree   // final
      *
      * @param deleted_node the node to be deleted
      */
-
-    private void doRedBlackDelete(final Node deleted_node)
+    void doRedBlackDelete(Node deleted_node)
     {
         for (int index = _MINIMUM_INDEX; index < _INDEX_COUNT; index++)
         {
@@ -1023,9 +976,8 @@ public final class BinaryTree   // final
      * @param replacement_node  the node being replaced
      * @param index _KEY or _VALUE
      */
-
-    private void doRedBlackDeleteFixup(final Node replacement_node,
-                                       final int index)
+    private void doRedBlackDeleteFixup(Node replacement_node,
+                                       int index)
     {
         Node current_node = replacement_node;
 
@@ -1121,8 +1073,7 @@ public final class BinaryTree   // final
      * @param y another node
      * @param index _KEY or _VALUE
      */
-
-    private void swapPosition(final Node x, final Node y, final int index)
+    private void swapPosition(Node x, Node y, int index)
     {
 
         // Save initial values.
@@ -1244,9 +1195,8 @@ public final class BinaryTree   // final
      * @exception NullPointerException if o is null
      * @exception ClassCastException if o is not Comparable
      */
-
-    private static void checkNonNullComparable(final Object o,
-                                               final int index)
+    private static void checkNonNullComparable(Object o,
+                                               int index)
     {
         if (o == null)
         {
@@ -1268,8 +1218,7 @@ public final class BinaryTree   // final
      * @exception NullPointerException if key is null
      * @exception ClassCastException if key is not Comparable
      */
-
-    private static void checkKey(final Object key)
+    private static void checkKey(Object key)
     {
         checkNonNullComparable(key, _KEY);
     }
@@ -1282,8 +1231,7 @@ public final class BinaryTree   // final
      * @exception NullPointerException if value is null
      * @exception ClassCastException if value is not Comparable
      */
-
-    private static void checkValue(final Object value)
+    private static void checkValue(Object value)
     {
         checkNonNullComparable(value, _VALUE);
     }
@@ -1298,8 +1246,7 @@ public final class BinaryTree   // final
      * @exception NullPointerException if key or value is null
      * @exception ClassCastException if key or value is not Comparable
      */
-
-    private static void checkKeyAndValue(final Object key, final Object value)
+    private static void checkKeyAndValue(Object key, Object value)
     {
         checkKey(key);
         checkValue(value);
@@ -1310,7 +1257,6 @@ public final class BinaryTree   // final
      * concurrent modification of the map through the map and through
      * an Iterator from one of its Set or Collection views
      */
-
     private void modify()
     {
         _modifications++;
@@ -1319,7 +1265,6 @@ public final class BinaryTree   // final
     /**
      * bump up the size and note that the map has changed
      */
-
     private void grow()
     {
         modify();
@@ -1329,7 +1274,6 @@ public final class BinaryTree   // final
     /**
      * decrement the size and note that the map has changed
      */
-
     private void shrink()
     {
         modify();
@@ -1344,8 +1288,7 @@ public final class BinaryTree   // final
      * @exception IllegalArgumentException if the node already exists
      *                                     in the value mapping
      */
-
-    private void insertValue(final Node newNode)
+    private void insertValue(Node newNode)
         throws IllegalArgumentException
     {
         Node node = _root[ _VALUE ];
@@ -1400,7 +1343,6 @@ public final class BinaryTree   // final
      *
      * @return the number of key-value mappings in this map.
      */
-
     public int size()
     {
         return _size;
@@ -1419,8 +1361,7 @@ public final class BinaryTree   // final
      *                               type for this map.
      * @exception NullPointerException if the key is null
      */
-
-    public boolean containsKey(final Object key)
+    public boolean containsKey(Object key)
         throws ClassCastException, NullPointerException
     {
         checkKey(key);
@@ -1436,8 +1377,7 @@ public final class BinaryTree   // final
      * @return true if this map maps one or more keys to the specified
      *         value.
      */
-
-    public boolean containsValue(final Object value)
+    public boolean containsValue(Object value)
     {
         checkValue(value);
         return lookup(( Comparable ) value, _VALUE) != null;
@@ -1456,8 +1396,7 @@ public final class BinaryTree   // final
      *                               type for this map.
      * @exception NullPointerException if the key is null
      */
-
-    public Object get(final Object key)
+    public Object get(Object key)
         throws ClassCastException, NullPointerException
     {
         return doGet(( Comparable ) key, _KEY);
@@ -1483,8 +1422,7 @@ public final class BinaryTree   // final
      *                                     value duplicates an
      *                                     existing value
      */
-
-    public Object put(final Object key, final Object value)
+    public Object put(Object key, Object value)
         throws ClassCastException, NullPointerException,
                 IllegalArgumentException
     {
@@ -1562,8 +1500,7 @@ public final class BinaryTree   // final
      * @return previous value associated with specified key, or null
      *         if there was no mapping for key.
      */
-
-    public Object remove(final Object key)
+    public Object remove(Object key)
     {
         return doRemove(( Comparable ) key, _KEY);
     }
@@ -1571,7 +1508,6 @@ public final class BinaryTree   // final
     /**
      * Removes all mappings from this map
      */
-
     public void clear()
     {
         modify();
@@ -1592,7 +1528,6 @@ public final class BinaryTree   // final
      *
      * @return a set view of the keys contained in this map.
      */
-
     public Set keySet()
     {
         if (_key_set[ _KEY ] == null)
@@ -1650,7 +1585,6 @@ public final class BinaryTree   // final
      *
      * @return a collection view of the values contained in this map.
      */
-
     public Collection values()
     {
         if (_value_collection[ _KEY ] == null)
@@ -1723,7 +1657,6 @@ public final class BinaryTree   // final
      *
      * @return a set view of the mappings contained in this map.
      */
-
     public Set entrySet()
     {
         if (_entry_set[ _KEY ] == null)
@@ -1803,8 +1736,7 @@ public final class BinaryTree   // final
          *
          * @param type
          */
-
-        BinaryTreeIterator(final int type)
+        BinaryTreeIterator(int type)
         {
             _type                   = type;
             _expected_modifications = BinaryTree.this._modifications;
@@ -1825,7 +1757,7 @@ public final class BinaryTree   // final
          * @return true if the iterator has more elements.
          */
 
-        public final boolean hasNext()
+        public boolean hasNext()
         {
             return _next_node != null;
         }
@@ -1842,7 +1774,7 @@ public final class BinaryTree   // final
          *                                            back
          */
 
-        public final Object next()
+        public Object next()
             throws NoSuchElementException, ConcurrentModificationException
         {
             if (_next_node == null)
@@ -1878,7 +1810,7 @@ public final class BinaryTree   // final
          *                                            back
          */
 
-        public final void remove()
+        public void remove()
             throws IllegalStateException, ConcurrentModificationException
         {
             if (_last_returned_node == null)
@@ -1897,7 +1829,7 @@ public final class BinaryTree   // final
         /* **********  END  implementation of Iterator ********** */
     }   // end private abstract class BinaryTreeIterator
 
-    // final for performance
+    // for performance
     private static final class Node
         implements Map.Entry
     {
@@ -1917,7 +1849,7 @@ public final class BinaryTree   // final
          * @param value
          */
 
-        Node(final Comparable key, final Comparable value)
+        Node(Comparable key, Comparable value)
         {
             _data                = new Comparable[]
             {
@@ -1949,8 +1881,7 @@ public final class BinaryTree   // final
          *
          * @return the key or value
          */
-
-        private Comparable getData(final int index)
+        public Comparable getData(int index)
         {
             return _data[ index ];
         }
@@ -1961,8 +1892,7 @@ public final class BinaryTree   // final
          * @param node the new left node
          * @param index _KEY or _VALUE
          */
-
-        private void setLeft(final Node node, final int index)
+        public void setLeft(Node node, int index)
         {
             _left[ index ] = node;
         }
@@ -1975,7 +1905,7 @@ public final class BinaryTree   // final
          * @return the left node -- may be null
          */
 
-        private Node getLeft(final int index)
+        public Node getLeft(int index)
         {
             return _left[ index ];
         }
@@ -1986,8 +1916,7 @@ public final class BinaryTree   // final
          * @param node the new right node
          * @param index _KEY or _VALUE
          */
-
-        private void setRight(final Node node, final int index)
+        public void setRight(Node node, int index)
         {
             _right[ index ] = node;
         }
@@ -2000,7 +1929,7 @@ public final class BinaryTree   // final
          * @return the right node -- may be null
          */
 
-        private Node getRight(final int index)
+        public Node getRight(int index)
         {
             return _right[ index ];
         }
@@ -2011,8 +1940,7 @@ public final class BinaryTree   // final
          * @param node the new parent node
          * @param index _KEY or _VALUE
          */
-
-        private void setParent(final Node node, final int index)
+        public void setParent(Node node, int index)
         {
             _parent[ index ] = node;
         }
@@ -2024,8 +1952,7 @@ public final class BinaryTree   // final
          *
          * @return the parent node -- may be null
          */
-
-        private Node getParent(final int index)
+        public Node getParent(int index)
         {
             return _parent[ index ];
         }
@@ -2036,8 +1963,7 @@ public final class BinaryTree   // final
          * @param node the node to swap with
          * @param index _KEY or _VALUE
          */
-
-        private void swapColors(final Node node, final int index)
+        public void swapColors(Node node, int index)
         {
 
             // Swap colors -- old hacker's trick
@@ -2053,8 +1979,7 @@ public final class BinaryTree   // final
          *
          * @return true if black (which is represented as a true boolean)
          */
-
-        private boolean isBlack(final int index)
+        public boolean isBlack(int index)
         {
             return _black[ index ];
         }
@@ -2066,8 +1991,7 @@ public final class BinaryTree   // final
          *
          * @return true if non-black
          */
-
-        private boolean isRed(final int index)
+        public boolean isRed(int index)
         {
             return !_black[ index ];
         }
@@ -2077,8 +2001,7 @@ public final class BinaryTree   // final
          *
          * @param index _KEY or _VALUE
          */
-
-        private void setBlack(final int index)
+        public void setBlack(int index)
         {
             _black[ index ] = true;
         }
@@ -2088,8 +2011,7 @@ public final class BinaryTree   // final
          *
          * @param index _KEY or _VALUE
          */
-
-        private void setRed(final int index)
+        public void setRed(int index)
         {
             _black[ index ] = false;
         }
@@ -2100,8 +2022,7 @@ public final class BinaryTree   // final
          * @param node the node whose color we're adopting
          * @param index _KEY or _VALUE
          */
-
-        private void copyColor(final Node node, final int index)
+        public void copyColor(Node node, int index)
         {
             _black[ index ] = node._black[ index ];
         }
@@ -2111,7 +2032,6 @@ public final class BinaryTree   // final
         /**
          * @return the key corresponding to this entry.
          */
-
         public Object getKey()
         {
             return _data[ _KEY ];
@@ -2120,7 +2040,6 @@ public final class BinaryTree   // final
         /**
          * @return the value corresponding to this entry.
          */
-
         public Object getValue()
         {
             return _data[ _VALUE ];
@@ -2133,10 +2052,7 @@ public final class BinaryTree   // final
          * @param ignored
          *
          * @return does not return
-         *
-         * @exception UnsupportedOperationException
          */
-
         public Object setValue(Object ignored)
             throws UnsupportedOperationException
         {
@@ -2154,7 +2070,6 @@ public final class BinaryTree   // final
          * @return true if the specified object is equal to this map
          *         entry.
          */
-
         public boolean equals(Object o)
         {
             if (this == o)
@@ -2188,5 +2103,4 @@ public final class BinaryTree   // final
 
         /* **********  END  implementation of Map.Entry ********** */
     }
-}   // end public class BinaryTree
-
+}

Modified: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/BitFieldFactory.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/BitFieldFactory.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/BitFieldFactory.java (original)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/BitFieldFactory.java Thu Dec 30 02:35:06 2010
@@ -15,7 +15,6 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.util;
 
@@ -27,18 +26,15 @@ import java.util.*;
  * @author Jason Height (jheight at apache dot org)
  */
 
-public class BitFieldFactory
-{
+public class BitFieldFactory {
     private static Map instances = new HashMap();
-    
-    public static BitField getInstance(final int mask) {
-      BitField f = (BitField)instances.get(new Integer(mask));
+
+    public static BitField getInstance(int mask) {
+      BitField f = (BitField)instances.get(Integer.valueOf(mask));
       if (f == null) {
         f = new BitField(mask);
-        instances.put(new Integer(mask), f);        
+        instances.put(Integer.valueOf(mask), f);
       }
       return f;
     }
-
-}   // end public class BitFieldFactory
-
+}

Added: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/CloseIgnoringInputStream.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/CloseIgnoringInputStream.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/CloseIgnoringInputStream.java (added)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/CloseIgnoringInputStream.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,41 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.util;
+
+import java.io.FilterInputStream;
+import java.io.InputStream;
+
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+
+/**
+ * A wrapper around an {@link InputStream}, which 
+ *  ignores close requests made to it.
+ *
+ * Useful with {@link POIFSFileSystem}, where you want
+ *  to control the close yourself.
+ */
+public class CloseIgnoringInputStream extends FilterInputStream {
+   public CloseIgnoringInputStream(InputStream in) {
+      super(in);
+   }
+
+   public void close() {
+      // Does nothing and ignores you
+      return;
+   }
+}

Added: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/DelayableLittleEndianOutput.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/DelayableLittleEndianOutput.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/DelayableLittleEndianOutput.java (added)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/DelayableLittleEndianOutput.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,34 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.util;
+/**
+ * Implementors of this interface allow client code to 'delay' writing to a certain section of a
+ * data output stream.<br/>
+ * A typical application is for writing BIFF records when the size is not known until well after
+ * the header has been written.  The client code can call {@link #createDelayedOutput(int)}
+ * to reserve two bytes of the output for the 'ushort size' header field.  The delayed output can
+ * be written at any stage.
+ *
+ * @author Josh Micich
+ */
+public interface DelayableLittleEndianOutput extends LittleEndianOutput {
+	/**
+	 * Creates an output stream intended for outputting a sequence of <tt>size</tt> bytes.
+	 */
+	LittleEndianOutput createDelayedOutput(int size);
+}

Added: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/FontMetricsDumper.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/FontMetricsDumper.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/FontMetricsDumper.java (added)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/FontMetricsDumper.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,84 @@
+
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+        
+package org.apache.poi.util;
+
+import java.awt.*;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+@SuppressWarnings("deprecation")
+public class FontMetricsDumper
+{
+    public static void main( String[] args ) throws IOException
+    {
+
+        Properties props = new Properties();
+
+        Font[] allFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
+        for ( int i = 0; i < allFonts.length; i++ )
+        {
+            String fontName = allFonts[i].getFontName();
+
+            Font font = new Font(fontName, Font.BOLD, 10);
+            FontMetrics fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
+            int fontHeight = fontMetrics.getHeight();
+
+            props.setProperty("font." + fontName + ".height", fontHeight+"");
+            StringBuffer characters = new StringBuffer();
+            for (char c = 'a'; c <= 'z'; c++)
+            {
+                characters.append( c + ", " );
+            }
+            for (char c = 'A'; c <= 'Z'; c++)
+            {
+                characters.append( c + ", " );
+            }
+            for (char c = '0'; c <= '9'; c++)
+            {
+                characters.append( c + ", " );
+            }
+            StringBuffer widths = new StringBuffer();
+            for (char c = 'a'; c <= 'z'; c++)
+            {
+                widths.append( fontMetrics.getWidths()[c] + ", " );
+            }
+            for (char c = 'A'; c <= 'Z'; c++)
+            {
+                widths.append( fontMetrics.getWidths()[c] + ", " );
+            }
+            for (char c = '0'; c <= '9'; c++)
+            {
+                widths.append( fontMetrics.getWidths()[c] + ", " );
+            }
+            props.setProperty("font." + fontName + ".characters", characters.toString());
+            props.setProperty("font." + fontName + ".widths", widths.toString());
+        }
+
+        FileOutputStream fileOut = new FileOutputStream("font_metrics.properties");
+        try
+        {
+            props.store(fileOut, "Font Metrics");
+        }
+        finally
+        {
+            fileOut.close();
+        }
+    }
+}

Modified: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/HexDump.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/HexDump.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/HexDump.java (original)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/HexDump.java Thu Dec 30 02:35:06 2010
@@ -169,7 +169,7 @@ public class HexDump {
      *            outside the data array's bounds
      * @return output string
      */
-    
+
     public static String dump(final byte [] data, final long offset,
                             final int index) {
         StringBuffer buffer;
@@ -216,10 +216,10 @@ public class HexDump {
             }
             buffer.append(EOL);
             display_offset += chars_read;
-        }                 
+        }
         return buffer.toString();
     }
-    
+
 
     private static String dump(final long value)
     {
@@ -399,10 +399,10 @@ public class HexDump {
             while (bytesRemaining-- > 0)
             {
                 int c = in.read();
-                if (c == -1)
+                if (c == -1) {
                     break;
-                else
-                    buf.write(c);
+                }
+                buf.write(c);
             }
         }
 
@@ -417,13 +417,13 @@ public class HexDump {
         // The return type is char array because most callers will probably append the value to a
         // StringBuffer, or write it to a Stream / Writer so there is no need to create a String;
         char[] result = new char[charPos];
-        
+
         long value = pValue;
         do {
             result[--charPos] = _hexcodes[(int) (value & 0x0F)];
             value >>>= 4;
         } while (charPos > 1);
-    
+
         // Prefix added to avoid ambiguity
         result[0] = '0';
         result[1] = 'x';
@@ -456,7 +456,7 @@ public class HexDump {
 
     public static void main(String[] args) throws Exception {
         File file = new File(args[0]);
-        InputStream in = new BufferedInputStream(new FileInputStream(file)); 
+        InputStream in = new BufferedInputStream(new FileInputStream(file));
         byte[] b = new byte[(int)file.length()];
         in.read(b);
         System.out.println(HexDump.dump(b, 0, 0));

Modified: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/HexRead.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/HexRead.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/HexRead.java (original)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/HexRead.java Thu Dec 30 02:35:06 2010
@@ -62,7 +62,7 @@ public class HexRead
      * @see #readData(String)
      */
     public static byte[] readData(InputStream stream, String section ) throws IOException {
-    	
+
         try
         {
             StringBuffer sectionText = new StringBuffer();
@@ -128,7 +128,7 @@ public class HexRead
                     characterCount++;
                     if ( characterCount == 2 )
                     {
-                        bytes.add( new Byte( b ) );
+                        bytes.add( Byte.valueOf( b ) );
                         characterCount = 0;
                         b = (byte) 0;
                     }
@@ -151,7 +151,7 @@ public class HexRead
                     characterCount++;
                     if ( characterCount == 2 )
                     {
-                        bytes.add( new Byte( b ) );
+                        bytes.add( Byte.valueOf( b ) );
                         characterCount = 0;
                         b = (byte) 0;
                     }

Modified: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IOUtils.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IOUtils.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IOUtils.java (original)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IOUtils.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,73 +14,119 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.util;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-
-public class IOUtils
-{
-    private IOUtils()
-    {
-    }
-
-    /**
-     * Reads all the data from the input stream, and returns
-     *  the bytes read.
-     */
-    public static byte[] toByteArray(InputStream stream) throws IOException {
-    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
-    	
-    	byte[] buffer = new byte[4096];
-    	int read = 0;
-    	while(read != -1) {
-    		read = stream.read(buffer);
-    		if(read > 0) {
-    			baos.write(buffer, 0, read);
-    		}
-    	}
-    	
-    	return baos.toByteArray();
-    }
-
-    /**
-     * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
-     */
-    public static int readFully(InputStream in, byte[] b)
-    throws IOException
-    {
-        return readFully(in, b, 0, b.length);
-    }
-
-    /**
-     * Same as the normal <tt>in.read(b, off, len)</tt>, but
-     *  tries to ensure that the entire len number of bytes
-     *  is read.
-     * <p>
-     * If the end of file is reached before any bytes
-     *  are read, returns -1.
-     * If the end of the file is reached after some bytes are
-     *  read, returns the number of bytes read.
-     * If the end of the file isn't reached before len
-     *  bytes have been read, will return len bytes.
-     */
-    public static int readFully(InputStream in, byte[] b, int off, int len)
-    throws IOException
-    {
-        int total = 0;
-        for (;;) {
-            int got = in.read(b, off + total, len - total);
-            if (got < 0) {
-                return (total == 0) ? -1 : total;
-            } else {
-                total += got;
-                if (total == len)
-                    return total;
-            }
-        }
-    }
-}
\ No newline at end of file
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.ReadableByteChannel;
+
+public final class IOUtils {
+	private IOUtils() {
+		// no instances of this class
+	}
+
+	/**
+	 * Reads all the data from the input stream, and returns the bytes read.
+	 */
+	public static byte[] toByteArray(InputStream stream) throws IOException {
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+		byte[] buffer = new byte[4096];
+		int read = 0;
+		while (read != -1) {
+			read = stream.read(buffer);
+			if (read > 0) {
+				baos.write(buffer, 0, read);
+			}
+		}
+
+		return baos.toByteArray();
+	}
+
+   /**
+    * Returns an array (that shouldn't be written to!) of the
+    *  ByteBuffer. Will be of the requested length, or possibly
+    *  longer if that's easier.
+    */
+   public static byte[] toByteArray(ByteBuffer buffer, int length) {
+      if(buffer.hasArray() && buffer.arrayOffset() == 0) {
+         // The backing array should work out fine for us
+         return buffer.array();
+      }
+      
+      byte[] data = new byte[length];
+      buffer.get(data);
+      return data;
+   }
+
+	/**
+	 * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
+	 */
+	public static int readFully(InputStream in, byte[] b) throws IOException {
+		return readFully(in, b, 0, b.length);
+	}
+
+	/**
+	 * Same as the normal <tt>in.read(b, off, len)</tt>, but tries to ensure
+	 * that the entire len number of bytes is read.
+	 * <p>
+	 * If the end of file is reached before any bytes are read, returns -1. If
+	 * the end of the file is reached after some bytes are read, returns the
+	 * number of bytes read. If the end of the file isn't reached before len
+	 * bytes have been read, will return len bytes.
+	 */
+	public static int readFully(InputStream in, byte[] b, int off, int len) throws IOException {
+		int total = 0;
+		while (true) {
+			int got = in.read(b, off + total, len - total);
+			if (got < 0) {
+				return (total == 0) ? -1 : total;
+			}
+			total += got;
+			if (total == len) {
+				return total;
+			}
+		}
+	}
+	
+   /**
+    * Same as the normal <tt>channel.read(b)</tt>, but tries to ensure
+    * that the entire len number of bytes is read.
+    * <p>
+    * If the end of file is reached before any bytes are read, returns -1. If
+    * the end of the file is reached after some bytes are read, returns the
+    * number of bytes read. If the end of the file isn't reached before len
+    * bytes have been read, will return len bytes.
+    */
+	public static int readFully(ReadableByteChannel channel, ByteBuffer b) throws IOException {
+      int total = 0;
+      while (true) {
+         int got = channel.read(b);
+         if (got < 0) {
+            return (total == 0) ? -1 : total;
+         }
+         total += got;
+         if (total == b.capacity()) {
+            return total;
+         }
+      }
+	}
+	
+	/**
+	 * Copies all the data from the given InputStream to the OutputStream. It
+	 * leaves both streams open, so you will still need to close them once done.
+	 */
+	public static void copy(InputStream inp, OutputStream out) throws IOException {
+		byte[] buff = new byte[4096];
+		int count;
+		while ((count = inp.read(buff)) != -1) {
+			if (count > 0) {
+				out.write(buff, 0, count);
+			}
+		}
+	}
+}

Modified: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IntList.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IntList.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IntList.java (original)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IntList.java Thu Dec 30 02:35:06 2010
@@ -1,4 +1,3 @@
-
 /* ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
@@ -15,12 +14,9 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
-        
 
 package org.apache.poi.util;
 
-import java.util.*;
-
 /**
  * A List of int's; as full an implementation of the java.util.List
  * interface as possible, with an eye toward minimal creation of
@@ -47,7 +43,6 @@ import java.util.*;
  *
  * @author Marc Johnson
  */
-
 public class IntList
 {
     private int[]            _array;
@@ -62,14 +57,14 @@ public class IntList
     public IntList()
     {
         this(_default_size);
-    }    
+    }
 
     public IntList(final int initialCapacity)
     {
         this(initialCapacity,0);
     }
-    
-    
+
+
     /**
      * create a copy of an existing IntList
      *
@@ -94,17 +89,17 @@ public class IntList
         _array = new int[ initialCapacity ];
         if (fillval != 0) {
             fillval = fillvalue;
-            fillArray(fillval, _array, 0);        
+            fillArray(fillval, _array, 0);
         }
         _limit = 0;
     }
 
     private void fillArray(int val, int[] array, int index) {
       for (int k = index; k < array.length; k++) {
-        array[k] = val;   
+        array[k] = val;
       }
     }
-    
+
     /**
      * add the specfied value at the specified index
      *
@@ -348,7 +343,9 @@ public class IntList
     {
         if (index >= _limit)
         {
-            throw new IndexOutOfBoundsException();
+            throw new IndexOutOfBoundsException(
+                  index + " not accessible in a list of length " + _limit
+            );
         }
         return _array[ index ];
     }
@@ -653,11 +650,11 @@ public class IntList
         int   size      = (new_size == _array.length) ? new_size + 1
                                                       : new_size;
         int[] new_array = new int[ size ];
-        
+
         if (fillval != 0) {
-          fillArray(fillval, new_array, _array.length);                
+          fillArray(fillval, new_array, _array.length);
         }
-        
+
         System.arraycopy(_array, 0, new_array, 0, _limit);
         _array = new_array;
     }

Modified: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IntMapper.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IntMapper.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IntMapper.java (original)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/IntMapper.java Thu Dec 30 02:35:06 2010
@@ -34,10 +34,10 @@ import java.util.*;
  * @author Jason Height
  */
 
-public class IntMapper
+public class IntMapper<T>
 {
-  private List elements;
-  private Map valueKeyMap;
+  private List<T> elements;
+  private Map<T,Integer> valueKeyMap;
 
   private static final int _default_size = 10;
 
@@ -52,8 +52,8 @@ public class IntMapper
 
     public IntMapper(final int initialCapacity)
     {
-        elements = new ArrayList(initialCapacity);
-        valueKeyMap = new HashMap(initialCapacity);
+        elements = new ArrayList<T>(initialCapacity);
+        valueKeyMap = new HashMap<T,Integer>(initialCapacity);
     }
 
     /**
@@ -64,12 +64,11 @@ public class IntMapper
      * @return true (as per the general contract of the Collection.add
      *         method).
      */
-
-    public boolean add(final Object value)
+    public boolean add(final T value)
     {
       int index = elements.size();
       elements.add(value);
-      valueKeyMap.put(value, new Integer(index));
+      valueKeyMap.put(value, index);
       return true;
     }
 
@@ -77,18 +76,18 @@ public class IntMapper
       return elements.size();
     }
 
-    public Object get(int index) {
+    public T get(int index) {
       return elements.get(index);
     }
 
-    public int getIndex(Object o) {
-      Integer i = ((Integer)valueKeyMap.get(o));
+    public int getIndex(T o) {
+      Integer i = valueKeyMap.get(o);
       if (i == null)
         return -1;
       return i.intValue();
     }
 
-    public Iterator iterator() {
+    public Iterator<T> iterator() {
       return elements.iterator();
     }
 }   // end public class IntMapper

Added: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/Internal.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/Internal.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/Internal.java (added)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/Internal.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,38 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.util;
+
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Documented;
+
+
+/**
+ * Program elements annotated &#64;Internal are intended for
+ * POI internal use only. Such elements are not public by design
+ * and likely to be removed in future versions of POI  or access
+ * to such elements will be changed from 'public' to 'default' or less.
+ *
+ * @author Yegor Kozlov
+ * @since POI-3.6
+ */
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Internal {
+    String value() default "";    
+}

Added: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LZWDecompresser.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LZWDecompresser.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LZWDecompresser.java (added)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LZWDecompresser.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,178 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * This class provides common functionality for the
+ *  various LZW implementations in the different file
+ *  formats.
+ * It's currently used by HDGF and HMEF.
+ *
+ * Two good resources on LZW are:
+ *  http://en.wikipedia.org/wiki/LZW
+ *  http://marknelson.us/1989/10/01/lzw-data-compression/
+ */
+public abstract class LZWDecompresser {
+   /**
+    * Does the mask bit mean it's compressed or uncompressed?
+    */
+   private boolean maskMeansCompressed;
+   
+   protected LZWDecompresser(boolean maskMeansCompressed) {
+      this.maskMeansCompressed = maskMeansCompressed;
+   }
+   
+   /**
+    * Populates the dictionary. May not need
+    *  to do anything if all zeros is fine.
+    */
+   protected abstract void populateDictionary(byte[] dict);
+   
+   /**
+    * Adjusts the position offset if needed when looking
+    *  something up in the dictionary.
+    */
+   protected abstract int adjustDictionaryOffset(int offset); 
+   
+   /**
+    * Decompresses the given input stream, returning the array of bytes
+    *  of the decompressed input.
+    */
+   public byte[] decompress(InputStream src) throws IOException {
+      ByteArrayOutputStream res = new ByteArrayOutputStream();
+      decompress(src,res);
+      return res.toByteArray();
+   }
+   
+   /**
+    * Perform a streaming decompression of the input.
+    * Works by:
+    * 1) Reading a flag byte, the 8 bits of which tell you if the
+    *     following 8 codes are compressed our un-compressed
+    * 2) Consider the 8 bits in turn
+    * 3) If the bit is set, the next code is un-compressed, so
+    *     add it to the dictionary and output it
+    * 4) If the bit isn't set, then read in the length and start
+    *     position in the dictionary, and output the bytes there
+    * 5) Loop until we've done all 8 bits, then read in the next
+    *     flag byte
+    */
+   public void decompress(InputStream src, OutputStream res) throws IOException {
+      // We use 12 bit codes:
+      // * 0-255 are real bytes
+      // * 256-4095 are the substring codes
+      // Java handily initialises our buffer / dictionary
+      //  to all zeros
+      byte[] buffer = new byte[4096];
+      populateDictionary(buffer);
+
+      // How far through the output we've got
+      // (This is normally used &4095, so it nicely wraps)
+      int pos = 0;
+      // The flag byte is treated as its 8 individual
+      //  bits, which tell us if the following 8 codes
+      //  are compressed or un-compressed
+      int flag;
+      // The mask, between 1 and 255, which is used when
+      //  processing each bit of the flag byte in turn
+      int mask;
+
+      // These are bytes as looked up in the dictionary
+      // It needs to be signed, as it'll get passed on to
+      //  the output stream
+      byte[] dataB = new byte[19];
+      // This is an unsigned byte read from the stream
+      // It needs to be unsigned, so that bit stuff works
+      int dataI;
+      // The compressed code sequence is held over 2 bytes
+      int dataIPt1, dataIPt2;
+      // How long a code sequence is, and where in the
+      //  dictionary to start at
+      int len, pntr;
+
+      while( (flag = src.read()) != -1 ) {
+         // Compare each bit in our flag byte in turn:
+         for(mask = 1; mask < 256 ; mask <<= 1) {
+            // Is this a new code (un-compressed), or
+            //  the use of existing codes (compressed)?
+            boolean isMaskSet = (flag & mask) > 0;
+            if( isMaskSet && !maskMeansCompressed ) {
+               // Retrieve the un-compressed code
+               if( (dataI = src.read()) != -1) {
+                  // Save the byte into the dictionary
+                  buffer[(pos&4095)] = fromInt(dataI);
+                  pos++;
+                  // And output the byte
+                  res.write( new byte[] {fromInt(dataI)} );
+               }
+            } else {
+               // We have a compressed sequence
+               // Grab the next 16 bits of data
+               dataIPt1 = src.read();
+               dataIPt2 = src.read();
+               if(dataIPt1 == -1 || dataIPt2 == -1) break;
+
+               // Build up how long the code sequence is, and
+               //  what position of the code to start at
+               // (The position is the first 12 bits, the
+               //  length is the last 4 bits)
+               len = (dataIPt2 & 15) + 3;
+               pntr = (dataIPt2 & 240)*16 + dataIPt1;
+
+               // Adjust the pointer as needed
+               pntr = adjustDictionaryOffset(pntr);
+
+               // Loop over the codes, outputting what they correspond to
+               for(int i=0; i<len; i++) {
+                  dataB[i] = buffer[(pntr + i) & 4095];
+                  buffer[ (pos + i) & 4095 ] = dataB[i];
+               }
+               res.write(dataB, 0, len);
+
+               // Record how far along the stream we have moved
+               pos = pos + len;
+            }
+         }
+      }
+   }
+
+   /**
+    * Given an integer, turn it into a java byte, handling
+    *  the wrapping.
+    * This is a convenience method
+    */
+   public static byte fromInt(int b) {
+      if(b < 128) return (byte)b;
+      return (byte)(b - 256);
+   }
+   /**
+    * Given a java byte, turn it into an integer between 0
+    *  and 255 (i.e. handle the unwrapping).
+    * This is a convenience method
+    */
+   public static int fromByte(byte b) {
+      if(b >= 0) {
+         return b;
+      }
+      return b + 256;
+   }
+}

Modified: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LittleEndian.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LittleEndian.java?rev=1053791&r1=1053790&r2=1053791&view=diff
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LittleEndian.java (original)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LittleEndian.java Thu Dec 30 02:35:06 2010
@@ -28,13 +28,12 @@ import java.io.InputStream;
  *@author     Marc Johnson (mjohnson at apache dot org)
  *@author     Andrew Oliver (acoliver at apache dot org)
  */
-public final class LittleEndian implements LittleEndianConsts {
+public class LittleEndian implements LittleEndianConsts {
 
     private LittleEndian() {
-    	// no instances of this class
+        // no instances of this class
     }
 
-
     /**
      *  get a short value from a byte array
      *
@@ -42,9 +41,10 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@return         the short (16-bit) value
      */
-
-    public static short getShort(final byte[] data, final int offset) {
-        return (short) getNumber(data, offset, SHORT_SIZE);
+    public static short getShort(byte[] data, int offset) {
+        int b0 = data[offset] & 0xFF;
+        int b1 = data[offset+1] & 0xFF;
+        return (short) ((b1 << 8) + (b0 << 0));
     }
 
 
@@ -55,73 +55,32 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@return         the unsigned short (16-bit) value in an integer
      */
-    public static int getUShort(final byte[] data, final int offset) {
-        short num = (short) getNumber(data, offset, SHORT_SIZE);
-        int retNum;
-        if (num < 0) {
-            retNum = (Short.MAX_VALUE + 1) * 2 + num;
-        } else {
-            retNum = num;
-        }
-        return retNum;
-    }
-
-
-    /**
-     *  get a short array from a byte array.
-     *
-     *@param  data    Description of the Parameter
-     *@param  offset  Description of the Parameter
-     *@param  size    Description of the Parameter
-     *@return         The simpleShortArray value
-     */
-    public static short[] getSimpleShortArray(final byte[] data, final int offset, final int size) {
-        short[] results = new short[size];
-        for (int i = 0; i < size; i++) {
-            results[i] = getShort(data, offset + 2 + (i * 2));
-        }
-        return results;
+    public static int getUShort(byte[] data, int offset) {
+        int b0 = data[offset] & 0xFF;
+        int b1 = data[offset+1] & 0xFF;
+        return (b1 << 8) + (b0 << 0);
     }
 
-
-    /**
-     *  get a short array from a byte array. The short array is assumed to start
-     *  with a word describing the length of the array.
-     *
-     *@param  data    Description of the Parameter
-     *@param  offset  Description of the Parameter
-     *@return         The shortArray value
-     */
-    public static short[] getShortArray(final byte[] data, final int offset) {
-        int size = (int) getNumber(data, offset, SHORT_SIZE);
-        short[] results = getSimpleShortArray(data, offset, size);
-        return results;
-    }
-
-
     /**
      *  get a short value from the beginning of a byte array
      *
      *@param  data  the byte array
      *@return       the short (16-bit) value
      */
-
-    public static short getShort(final byte[] data) {
+    public static short getShort(byte[] data) {
         return getShort(data, 0);
     }
 
-
     /**
      *  get an unsigned short value from the beginning of a byte array
      *
      *@param  data  the byte array
      *@return       the unsigned short (16-bit) value in an int
      */
-    public static int getUShort(final byte[] data) {
+    public static int getUShort(byte[] data) {
         return getUShort(data, 0);
     }
 
-
     /**
      *  get an int value from a byte array
      *
@@ -129,9 +88,13 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@return         the int (32-bit) value
      */
-
-    public static int getInt(final byte[] data, final int offset) {
-        return (int) getNumber(data, offset, INT_SIZE);
+    public static int getInt(byte[] data, int offset) {
+        int i=offset;
+        int b0 = data[i++] & 0xFF;
+        int b1 = data[i++] & 0xFF;
+        int b2 = data[i++] & 0xFF;
+        int b3 = data[i++] & 0xFF;
+        return (b3 << 24) + (b2 << 16) + (b1 << 8) + (b0 << 0);
     }
 
 
@@ -139,10 +102,9 @@ public final class LittleEndian implemen
      *  get an int value from the beginning of a byte array
      *
      *@param  data  the byte array
-     *@return       the int (32-bit) value
+     *@return the int (32-bit) value
      */
-
-    public static int getInt(final byte[] data) {
+    public static int getInt(byte[] data) {
         return getInt(data, 0);
     }
 
@@ -154,15 +116,9 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@return         the unsigned int (32-bit) value in a long
      */
-    public static long getUInt(final byte[] data, final int offset) {
-        int num = (int) getNumber(data, offset, INT_SIZE);
-        long retNum;
-        if (num < 0) {
-            retNum = ((long) Integer.MAX_VALUE + 1) * 2 + num;
-        } else {
-            retNum = num;
-        }
-        return retNum;
+    public static long getUInt(byte[] data, int offset) {
+        long retNum = getInt(data, offset);
+        return retNum & 0x00FFFFFFFF;
     }
 
     /**
@@ -171,8 +127,8 @@ public final class LittleEndian implemen
      *@param  data    the byte array
      *@return         the unsigned int (32-bit) value in a long
      */
-    public static long getUInt(final byte[] data) {
-	return getUInt(data,0);
+    public static long getUInt(byte[] data) {
+        return getUInt(data,0);
     }
 
     /**
@@ -182,24 +138,16 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@return         the long (64-bit) value
      */
-
-    public static long getLong(final byte[] data, final int offset) {
-        return getNumber(data, offset, LONG_SIZE);
-    }
-
-
-    /**
-     *  get a long value from the beginning of a byte array
-     *
-     *@param  data  the byte array
-     *@return       the long (64-bit) value
-     */
-
-    public static long getLong(final byte[] data) {
-        return getLong(data, 0);
+    public static long getLong(byte[] data, int offset) {
+        long result = 0;
+		
+		for (int j = offset + LONG_SIZE - 1; j >= offset; j--) {
+		    result <<= 8;
+		    result |= 0xff & data[j];
+		}
+		return result;
     }
 
-
     /**
      *  get a double value from a byte array, reads it in little endian format
      *  then converts the resulting revolting IEEE 754 (curse them) floating
@@ -209,24 +157,10 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@return         the double (64-bit) value
      */
-
-    public static double getDouble(final byte[] data, final int offset) {
-        return Double.longBitsToDouble(getNumber(data, offset, DOUBLE_SIZE));
+    public static double getDouble(byte[] data, int offset) {
+        return Double.longBitsToDouble(getLong(data, offset));
     }
 
-
-    /**
-     *  get a double value from the beginning of a byte array
-     *
-     *@param  data  the byte array
-     *@return       the double (64-bit) value
-     */
-
-    public static double getDouble(final byte[] data) {
-        return getDouble(data, 0);
-    }
-
-
     /**
      *  put a short value into a byte array
      *
@@ -234,9 +168,10 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@param  value   the short (16-bit) value
      */
-    public static void putShort(final byte[] data, final int offset,
-            final short value) {
-        putNumber(data, offset, value, SHORT_SIZE);
+    public static void putShort(byte[] data, int offset, short value) {
+        int i = offset;
+        data[i++] = (byte)((value >>>  0) & 0xFF);
+        data[i++] = (byte)((value >>>  8) & 0xFF);
     }
 
     /**
@@ -247,7 +182,7 @@ public final class LittleEndian implemen
      * Added for consistency with other put~() methods
      */
     public static void putByte(byte[] data, int offset, int value) {
-        putNumber(data, offset, value, LittleEndianConsts.BYTE_SIZE);
+        data[offset] = (byte) value;
     }
 
     /**
@@ -259,10 +194,10 @@ public final class LittleEndian implemen
      *
      * @exception ArrayIndexOutOfBoundsException may be thrown
      */
-    public static void putUShort(final byte[] data, final int offset,
-                                final int value)
-    {
-        putNumber(data, offset, value, SHORT_SIZE);
+    public static void putUShort(byte[] data, int offset, int value) {
+        int i = offset;
+        data[i++] = (byte)((value >>>  0) & 0xFF);
+        data[i++] = (byte)((value >>>  8) & 0xFF);
     }
 
     /**
@@ -271,8 +206,7 @@ public final class LittleEndian implemen
      *@param  data   the byte array
      *@param  value  the short (16-bit) value
      */
-
-    public static void putShort(final byte[] data, final short value) {
+    public static void putShort(byte[] data, short value) {
         putShort(data, 0, value);
     }
 
@@ -284,10 +218,12 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@param  value   the int (32-bit) value
      */
-
-    public static void putInt(final byte[] data, final int offset,
-            final int value) {
-        putNumber(data, offset, value, INT_SIZE);
+    public static void putInt(byte[] data, int offset, int value) {
+        int i = offset;
+        data[i++] = (byte)((value >>>  0) & 0xFF);
+        data[i++] = (byte)((value >>>  8) & 0xFF);
+        data[i++] = (byte)((value >>> 16) & 0xFF);
+        data[i++] = (byte)((value >>> 24) & 0xFF);
     }
 
 
@@ -297,8 +233,7 @@ public final class LittleEndian implemen
      *@param  data   the byte array
      *@param  value  the int (32-bit) value
      */
-
-    public static void putInt(final byte[] data, final int value) {
+    public static void putInt(byte[] data, int value) {
         putInt(data, 0, value);
     }
 
@@ -310,22 +245,14 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@param  value   the long (64-bit) value
      */
-
-    public static void putLong(final byte[] data, final int offset,
-            final long value) {
-        putNumber(data, offset, value, LONG_SIZE);
-    }
-
-
-    /**
-     *  put a long value into beginning of a byte array
-     *
-     *@param  data   the byte array
-     *@param  value  the long (64-bit) value
-     */
-
-    public static void putLong(final byte[] data, final long value) {
-        putLong(data, 0, value);
+    public static void putLong(byte[] data, int offset, long value) {
+        int limit = LONG_SIZE + offset;
+        long v = value;
+        
+        for (int j = offset; j < limit; j++) {
+            data[j] = (byte) (v & 0xFF);
+            v >>= 8;
+        }
     }
 
 
@@ -336,26 +263,8 @@ public final class LittleEndian implemen
      *@param  offset  a starting offset into the byte array
      *@param  value   the double (64-bit) value
      */
-
-    public static void putDouble(final byte[] data, final int offset,
-            final double value) {
-        // Excel likes NaN to be a specific value.
-        if (Double.isNaN(value))
-            putNumber(data, offset, -276939487313920L, DOUBLE_SIZE);
-        else
-            putNumber(data, offset, Double.doubleToLongBits(value), DOUBLE_SIZE);
-    }
-
-
-    /**
-     *  put a double value into beginning of a byte array
-     *
-     *@param  data   the byte array
-     *@param  value  the double (64-bit) value
-     */
-
-    public static void putDouble(final byte[] data, final double value) {
-        putDouble(data, 0, value);
+    public static void putDouble(byte[] data, int offset, double value) {
+        putLong(data, offset, Double.doubleToLongBits(value));
     }
 
 
@@ -364,7 +273,6 @@ public final class LittleEndian implemen
      *
      *@author     Marc Johnson (mjohnson at apache dot org)
      */
-
     public static final class BufferUnderrunException extends IOException {
 
         BufferUnderrunException() {
@@ -376,79 +284,72 @@ public final class LittleEndian implemen
     /**
      *  get a short value from an InputStream
      *
-     *@param  stream                       the InputStream from which the short
-     *      is to be read
+     *@param  stream the InputStream from which the short is to be read
      *@return                              the short (16-bit) value
      *@exception  IOException              will be propagated back to the caller
-     *@exception  BufferUnderrunException  if the stream cannot provide enough
-     *      bytes
+     *@exception  BufferUnderrunException  if the stream cannot provide enough bytes
      */
     public static short readShort(InputStream stream) throws IOException, BufferUnderrunException {
 
-		return (short) readUShort(stream);
-	}
+        return (short) readUShort(stream);
+    }
 
-	public static int readUShort(InputStream stream) throws IOException, BufferUnderrunException {
+    public static int readUShort(InputStream stream) throws IOException, BufferUnderrunException {
 
-		int ch1 = stream.read();
-		int ch2 = stream.read();
-		if ((ch1 | ch2) < 0) {
-			throw new BufferUnderrunException();
-		}
-		return ((ch2 << 8) + (ch1 << 0));
-	}
+        int ch1 = stream.read();
+        int ch2 = stream.read();
+        if ((ch1 | ch2) < 0) {
+            throw new BufferUnderrunException();
+        }
+        return (ch2 << 8) + (ch1 << 0);
+    }
     
 
     /**
      *  get an int value from an InputStream
      *
-     *@param  stream                       the InputStream from which the int is
-     *      to be read
-     *@return                              the int (32-bit) value
-     *@exception  IOException              will be propagated back to the caller
-     *@exception  BufferUnderrunException  if the stream cannot provide enough
-     *      bytes
+     *@param  stream the InputStream from which the int is to be read
+     * @return                              the int (32-bit) value
+     * @exception  IOException              will be propagated back to the caller
+     * @exception  BufferUnderrunException  if the stream cannot provide enough bytes
      */
-    public static int readInt(final InputStream stream)
+    public static int readInt(InputStream stream)
              throws IOException, BufferUnderrunException {
-		int ch1 = stream.read();
-		int ch2 = stream.read();
-		int ch3 = stream.read();
-		int ch4 = stream.read();
-		if ((ch1 | ch2 | ch3 | ch4) < 0) {
-			throw new BufferUnderrunException();
-		}
-		return ((ch4 << 24) + (ch3<<16) + (ch2 << 8) + (ch1 << 0));
+        int ch1 = stream.read();
+        int ch2 = stream.read();
+        int ch3 = stream.read();
+        int ch4 = stream.read();
+        if ((ch1 | ch2 | ch3 | ch4) < 0) {
+            throw new BufferUnderrunException();
+        }
+        return (ch4 << 24) + (ch3<<16) + (ch2 << 8) + (ch1 << 0);
     }
 
 
     /**
      *  get a long value from an InputStream
      *
-     *@param  stream                       the InputStream from which the long
-     *      is to be read
-     *@return                              the long (64-bit) value
-     *@exception  IOException              will be propagated back to the caller
-     *@exception  BufferUnderrunException  if the stream cannot provide enough
-     *      bytes
+     * @param  stream the InputStream from which the long is to be read
+     * @return                              the long (64-bit) value
+     * @exception  IOException              will be propagated back to the caller
+     * @exception  BufferUnderrunException  if the stream cannot provide enough bytes
      */
-
-    public static long readLong(final InputStream stream)
+    public static long readLong(InputStream stream)
              throws IOException, BufferUnderrunException {
-		int ch1 = stream.read();
-		int ch2 = stream.read();
-		int ch3 = stream.read();
-		int ch4 = stream.read();
-		int ch5 = stream.read();
-		int ch6 = stream.read();
-		int ch7 = stream.read();
-		int ch8 = stream.read();
-		if ((ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8) < 0) {
-			throw new BufferUnderrunException();
-		}
-		
-		return 
-			((long)ch8 << 56) +
+        int ch1 = stream.read();
+        int ch2 = stream.read();
+        int ch3 = stream.read();
+        int ch4 = stream.read();
+        int ch5 = stream.read();
+        int ch6 = stream.read();
+        int ch7 = stream.read();
+        int ch8 = stream.read();
+        if ((ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8) < 0) {
+            throw new BufferUnderrunException();
+        }
+        
+        return 
+            ((long)ch8 << 56) +
             ((long)ch7 << 48) +
             ((long)ch6 << 40) +
             ((long)ch5 << 32) +
@@ -459,113 +360,43 @@ public final class LittleEndian implemen
     }
 
     /**
-     *  Gets the number attribute of the LittleEndian class
-     *
-     *@param  data    Description of the Parameter
-     *@param  offset  Description of the Parameter
-     *@param  size    Description of the Parameter
-     *@return         The number value
-     */
-    private static long getNumber(final byte[] data, final int offset,
-            final int size) {
-        long result = 0;
-
-        for (int j = offset + size - 1; j >= offset; j--) {
-            result <<= 8;
-            result |= 0xff & data[j];
-        }
-        return result;
-    }
-
-
-    /**
-     *  Description of the Method
-     *
-     *@param  data    Description of the Parameter
-     *@param  offset  Description of the Parameter
-     *@param  value   Description of the Parameter
-     *@param  size    Description of the Parameter
-     */
-    private static void putNumber(final byte[] data, final int offset,
-            final long value, final int size) {
-        int limit = size + offset;
-        long v = value;
-
-        for (int j = offset; j < limit; j++) {
-            data[j] = (byte) (v & 0xFF);
-            v >>= 8;
-        }
-    }
-
-
-    /**
      *  Convert an 'unsigned' byte to an integer. ie, don't carry across the
      *  sign.
      *
-     *@param  b  Description of the Parameter
-     *@return    Description of the Return Value
+     * @param  b  Description of the Parameter
+     * @return    Description of the Return Value
      */
     public static int ubyteToInt(byte b) {
-        return ((b & 0x80) == 0 ? (int) b : (b & (byte) 0x7f) + 0x80);
+        return b & 0xFF;
     }
 
 
     /**
      *  get the unsigned value of a byte.
      *
-     *@param  data    the byte array.
-     *@param  offset  a starting offset into the byte array.
-     *@return         the unsigned value of the byte as a 32 bit integer
+     * @param  data    the byte array.
+     * @param  offset  a starting offset into the byte array.
+     * @return         the unsigned value of the byte as a 32 bit integer
      */
-    public static int getUnsignedByte(final byte[] data, final int offset) {
-        return (int) getNumber(data, offset, BYTE_SIZE);
-    }
-
-
-    /**
-     *  get the unsigned value of a byte.
-     *
-     *@param  data  the byte array
-     *@return       the unsigned value of the byte as a 32 bit integer
-     */
-    public static int getUnsignedByte(final byte[] data) {
-        return getUnsignedByte(data, 0);
+    public static int getUnsignedByte(byte[] data, int offset) {
+        return data[offset] & 0xFF;
     }
 
 
     /**
      *  Copy a portion of a byte array
      *
-     *@param  data                        the original byte array
-     *@param  offset                      Where to start copying from.
-     *@param  size                        Number of bytes to copy.
-     *@return                             The byteArray value
-     *@throws  IndexOutOfBoundsException  - if copying would cause access of
+     * @param  data                        the original byte array
+     * @param  offset                      Where to start copying from.
+     * @param  size                        Number of bytes to copy.
+     * @return                             The byteArray value
+     * @throws  IndexOutOfBoundsException  - if copying would cause access of
      *      data outside array bounds.
      */
-    public static byte[] getByteArray(final byte[] data, int offset, int size) {
+    public static byte[] getByteArray(byte[] data, int offset, int size) {
         byte[] copy = new byte[size];
         System.arraycopy(data, offset, copy, 0, size);
 
         return copy;
     }
-
-    /**
-     * <p>Gets an unsigned int value (8 bytes) from a byte array.</p>
-     * 
-     * @param data the byte array
-     * @param offset a starting offset into the byte array
-     * @return the unsigned int (32-bit) value in a long
-     */
-    public static long getULong(final byte[] data, final int offset)
-    {
-        int num = (int) getNumber(data, offset, LONG_SIZE);
-        long retNum;
-        if (num < 0)
-            retNum = ((long) Integer.MAX_VALUE + 1) * 2 + num;
-        else
-            retNum = num;
-        return retNum;
-    }
-
 }

Added: poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
URL: http://svn.apache.org/viewvc/poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java?rev=1053791&view=auto
==============================================================================
--- poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java (added)
+++ poi/branches/NIO_32_BRANCH/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java Thu Dec 30 02:35:06 2010
@@ -0,0 +1,119 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.util;
+
+/**
+ * Adapts a plain byte array to {@link LittleEndianInput}
+ *
+ * @author Josh Micich
+ */
+public final class LittleEndianByteArrayInputStream implements LittleEndianInput {
+	private final byte[] _buf;
+	private final int _endIndex;
+	private int _readIndex;
+
+	public LittleEndianByteArrayInputStream(byte[] buf, int startOffset, int maxReadLen) {
+		_buf = buf;
+		_readIndex = startOffset;
+		_endIndex = startOffset + maxReadLen;
+	}
+	public LittleEndianByteArrayInputStream(byte[] buf, int startOffset) {
+		this(buf, startOffset, buf.length - startOffset);
+	}
+	public LittleEndianByteArrayInputStream(byte[] buf) {
+		this(buf, 0, buf.length);
+	}
+
+	public int available() {
+		return _endIndex - _readIndex;
+	}
+	private void checkPosition(int i) {
+		if (i > _endIndex - _readIndex) {
+			throw new RuntimeException("Buffer overrun");
+		}
+	}
+
+	public int getReadIndex() {
+		return _readIndex;
+	}
+	public byte readByte() {
+		checkPosition(1);
+		return _buf[_readIndex++];
+	}
+
+	public int readInt() {
+		checkPosition(4);
+		int i = _readIndex;
+
+		int b0 = _buf[i++] & 0xFF;
+		int b1 = _buf[i++] & 0xFF;
+		int b2 = _buf[i++] & 0xFF;
+		int b3 = _buf[i++] & 0xFF;
+		_readIndex = i;
+		return (b3 << 24) + (b2 << 16) + (b1 << 8) + (b0 << 0);
+	}
+	public long readLong() {
+		checkPosition(8);
+		int i = _readIndex;
+
+		int b0 = _buf[i++] & 0xFF;
+		int b1 = _buf[i++] & 0xFF;
+		int b2 = _buf[i++] & 0xFF;
+		int b3 = _buf[i++] & 0xFF;
+		int b4 = _buf[i++] & 0xFF;
+		int b5 = _buf[i++] & 0xFF;
+		int b6 = _buf[i++] & 0xFF;
+		int b7 = _buf[i++] & 0xFF;
+		_readIndex = i;
+		return (((long)b7 << 56) +
+				((long)b6 << 48) +
+				((long)b5 << 40) +
+				((long)b4 << 32) +
+				((long)b3 << 24) +
+				(b2 << 16) +
+				(b1 <<  8) +
+				(b0 <<  0));
+	}
+	public short readShort() {
+		return (short)readUShort();
+	}
+	public int readUByte() {
+		checkPosition(1);
+		return _buf[_readIndex++] & 0xFF;
+	}
+	public int readUShort() {
+		checkPosition(2);
+		int i = _readIndex;
+
+		int b0 = _buf[i++] & 0xFF;
+		int b1 = _buf[i++] & 0xFF;
+		_readIndex = i;
+		return (b1 << 8) + (b0 << 0);
+	}
+	public void readFully(byte[] buf, int off, int len) {
+		checkPosition(len);
+		System.arraycopy(_buf, _readIndex, buf, off, len);
+		_readIndex+=len;
+	}
+	public void readFully(byte[] buf) {
+		readFully(buf, 0, buf.length);
+	}
+	public double readDouble() {
+		return Double.longBitsToDouble(readLong());
+	}
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message