Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / iapi / services / io / ArrayUtil.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/iapi/services/io/ArrayUtil.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/iapi/services/io/ArrayUtil.java
new file mode 100644 (file)
index 0000000..d846be4
--- /dev/null
@@ -0,0 +1,368 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.iapi.services.io.ArrayUtil\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.services.io;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import java.io.ObjectOutput;\r
+import java.io.ObjectInput;\r
+import java.io.IOException;\r
+import java.lang.reflect.Array;\r
+\r
+/**\r
+  Utility class for constructing and reading and writing arrays from/to\r
+  formatId streams.\r
\r
+  @version 0.1\r
+ */\r
+public abstract class ArrayUtil\r
+{\r
+       ///////////////////////////////////////////////////////////////////\r
+       //\r
+       // Methods for Arrays of OBJECTS.  Cannot be used for an\r
+       // array of primitives, see below for something for primitives\r
+       //\r
+       ///////////////////////////////////////////////////////////////////\r
+       /**\r
+         Write the length of an array of objects to an output stream.\r
+\r
+         The length\r
+\r
+         @param        out             ObjectOutput stream\r
+         @param        a               array of objects whose length should be written.\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static void writeArrayLength(ObjectOutput out, Object[] a)\r
+                throws IOException\r
+       {\r
+               out.writeInt(a.length);\r
+       }\r
+\r
+       /**\r
+         Write an array of objects to an output stream.\r
+\r
+         @param        out             Object output stream to write to.\r
+         @param        a               array of objects to write.\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static void writeArrayItems(ObjectOutput out, Object[] a)\r
+                throws IOException\r
+       {\r
+               if (a == null)\r
+                       return;\r
+\r
+               for(int ix = 0; ix < a.length; ix++)\r
+               {       out.writeObject(a[ix]); }\r
+       }\r
+\r
+       /**\r
+         Write an array of objects and length to an output stream.\r
+         Does equivalent of writeArrayLength() followed by writeArrayItems()\r
+\r
+         @param        out             Object output stream to write to.\r
+         @param        a               array of objects to write.\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static void writeArray(ObjectOutput out, Object[] a)\r
+                throws IOException\r
+       {\r
+               if (a == null) \r
+               {\r
+                       out.writeInt(0);\r
+                       return;\r
+               }\r
+\r
+               out.writeInt(a.length);\r
+               for(int ix = 0; ix < a.length; ix++)\r
+               {       out.writeObject(a[ix]); }\r
+       }\r
+\r
+       /**\r
+         Read an array of objects out of a stream.\r
+\r
+         @param        in      Input stream\r
+         @param        a       array to read into\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         @exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist\r
+         */\r
+       public static void readArrayItems(ObjectInput in, Object[] a)\r
+                throws IOException, ClassNotFoundException\r
+       {\r
+               for (int ix=0; ix<a.length; ix++)\r
+               {\r
+                       a[ix]=in.readObject();\r
+               }\r
+       }\r
+\r
+       /**\r
+         Read the length of an array of objects in an object stream.\r
+\r
+         @param        in      Input stream.\r
+\r
+         @return       length of the array of objects\r
+         \r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static int readArrayLength(ObjectInput in)\r
+                throws IOException\r
+       {\r
+               return in.readInt();\r
+       }\r
+\r
+       /**\r
+         Reads an array of objects from the stream.\r
+\r
+         @param        in      Input stream\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         @exception java.lang.ClassNotFoundException The Class for an Object we are reading does not exist\r
+         */\r
+       public static Object[] readObjectArray(ObjectInput in)\r
+                throws IOException, ClassNotFoundException\r
+       {\r
+               int     size = in.readInt();\r
+               if ( size == 0 ) { return null; }\r
+\r
+               Object[]        result = new Object[ size ];\r
+\r
+               readArrayItems( in, result );\r
+\r
+               return result;\r
+       }\r
+\r
+       ///////////////////////////////////////////////////////////////////\r
+       //\r
+       // Methods for Arrays of INTs\r
+       //\r
+       ///////////////////////////////////////////////////////////////////\r
+\r
+       /**\r
+         Write an array of integers to an ObjectOutput. This writes the array\r
+         in a format readIntArray understands.\r
+\r
+         @param out the ObjectOutput.\r
+         @param a the array.\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static void writeIntArray(ObjectOutput out, int[] a) throws IOException {\r
+               if (a == null)\r
+                       out.writeInt(0);\r
+               else {\r
+                       out.writeInt(a.length);\r
+                       for (int i=0; i<a.length; i++)\r
+                               out.writeInt(a[i]);\r
+               }\r
+       }\r
+\r
+       /**\r
+         Read an array of integers from an ObjectInput. This allocates the\r
+         array.\r
+\r
+         @param        in      the ObjectInput.\r
+         @return   the array of integers.\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static int[] readIntArray(ObjectInput in) throws IOException {\r
+               int length = in.readInt();\r
+               if (length == 0)\r
+                       return null;\r
+               int[] a = new int[length];\r
+               for (int i=0; i<length; i++)\r
+                       a[i] = in.readInt();\r
+               return a;\r
+       }\r
+\r
+       public  static  void    writeInts( ObjectOutput out, int[][] val )\r
+               throws IOException\r
+       {\r
+               if (val == null)\r
+               {\r
+                       out.writeBoolean(false);\r
+               }\r
+               else\r
+               {\r
+                       out.writeBoolean(true);\r
+\r
+                       int     count = val.length;\r
+                       out.writeInt( count );\r
+\r
+                       for (int i = 0; i < count; i++)\r
+                       {\r
+                               ArrayUtil.writeIntArray( out, val[i] );\r
+                       }\r
+               }\r
+       }\r
+\r
+       public  static  int[][] readInts( ObjectInput in )\r
+                throws IOException, ClassNotFoundException\r
+       {\r
+               int[][] retVal = null;\r
+\r
+               if ( in.readBoolean() )\r
+               {\r
+                       int     count = in.readInt();\r
+\r
+                       retVal = new int[ count ][];\r
+\r
+                       for (int i = 0; i < count; i++)\r
+                       {\r
+                               retVal[ i ] = ArrayUtil.readIntArray( in );\r
+                       }\r
+               }\r
+\r
+               return retVal;\r
+       }\r
+\r
+    public static String toString(int[] value)\r
+    {\r
+        String ret_val;\r
+\r
+        if (value == null || value.length == 0)\r
+        {\r
+            ret_val = "null";\r
+        }\r
+        else\r
+        {\r
+            ret_val = "";\r
+            for (int i = 0; i < value.length; i++)\r
+            {\r
+                ret_val += "[" + value[i] + "],";\r
+            }\r
+        }\r
+        return(ret_val);\r
+    }\r
+\r
+\r
+       ///////////////////////////////////////////////////////////////////\r
+       //\r
+       // Methods for Arrays of LONGs\r
+       //\r
+       ///////////////////////////////////////////////////////////////////\r
+\r
+       /**\r
+         Write an array of longs to an ObjectOutput. This writes the array\r
+         in a format readLongArray understands.\r
+\r
+         @param out the ObjectOutput.\r
+         @param a the array.\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static void writeLongArray(ObjectOutput out, long[] a) throws IOException {\r
+               if (a == null)\r
+                       out.writeInt(0);\r
+               else {\r
+                       out.writeInt(a.length);\r
+                       for (int i=0; i<a.length; i++)\r
+                               out.writeLong(a[i]);\r
+               }\r
+       }\r
+\r
+       /**\r
+         Read an array of integers from an ObjectInput. This allocates the\r
+         array.\r
+\r
+         @param        in      the ObjectInput.\r
+         @return   the array of integers.\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static long[] readLongArray(ObjectInput in) throws IOException {\r
+               int length = in.readInt();\r
+               long[] a = new long[length];\r
+               for (int i=0; i<length; i++)\r
+                       a[i] = in.readLong();\r
+               return a;\r
+       }\r
+\r
+       /**\r
+         Read an array of strings from an ObjectInput. This allocates the\r
+         array.\r
+\r
+         @param        in      the ObjectInput.\r
+         @return   the array of integers.\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static String[] readStringArray(ObjectInput in) \r
+               throws IOException, ClassNotFoundException\r
+       {\r
+               Object[] objArray = readObjectArray(in);\r
+               int size = 0;\r
+\r
+               if (objArray == null)\r
+                       return null;\r
+\r
+               String[] stringArray = new String[size = objArray.length];\r
+\r
+               for (int i = 0; i < size; i++)\r
+               {\r
+                       stringArray[i] = (String)objArray[i];\r
+               } \r
+\r
+               return stringArray;\r
+       }\r
+       \r
+       ///////////////////////////////////////////////////////////////////\r
+       //\r
+       // Methods for Arrays of BOOLEANS\r
+       //\r
+       ///////////////////////////////////////////////////////////////////\r
+\r
+       /**\r
+         Write an array of booleans to an ObjectOutput. This writes the array\r
+         in a format readBooleanArray understands.\r
+\r
+         @param out the ObjectOutput.\r
+         @param a the array.\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static void writeBooleanArray(ObjectOutput out, boolean[] a) throws IOException {\r
+               if (a == null)\r
+                       out.writeInt(0);\r
+               else {\r
+                       out.writeInt(a.length);\r
+                       for (int i=0; i<a.length; i++)\r
+                               out.writeBoolean(a[i]);\r
+               }\r
+       }\r
+\r
+       /**\r
+         Read an array of integers from an ObjectInput. This allocates the\r
+         array.\r
+\r
+         @param        in      the ObjectInput.\r
+         @return   the array of integers.\r
+\r
+         @exception java.io.IOException The write caused an IOException. \r
+         */\r
+       public static boolean[] readBooleanArray(ObjectInput in) throws IOException {\r
+               int length = in.readInt();\r
+               boolean[] a = new boolean[length];\r
+               for (int i=0; i<length; i++)\r
+                       a[i] = in.readBoolean();\r
+               return a;\r
+       }\r
+}\r