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 / FormatableArrayHolder.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/iapi/services/io/FormatableArrayHolder.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/iapi/services/io/FormatableArrayHolder.java
new file mode 100644 (file)
index 0000000..dd21868
--- /dev/null
@@ -0,0 +1,181 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.iapi.services.io.FormatableArrayHolder\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.io.ArrayInputStream;\r
+\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+import org.apache.derby.iapi.services.io.FormatIdUtil;\r
+import org.apache.derby.iapi.services.io.ArrayUtil;\r
+import org.apache.derby.iapi.services.io.Formatable;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import java.io.ObjectOutput;\r
+import java.io.ObjectInput;\r
+import java.io.IOException;\r
+\r
+import java.lang.reflect.Array;\r
+\r
+/**\r
+ * A formatable holder for an array of formatables.\r
+ * Used to avoid serializing arrays.\r
+ */\r
+public class FormatableArrayHolder implements Formatable\r
+{\r
+       /********************************************************\r
+       **\r
+       **      This class implements Formatable. That means that it\r
+       **      can write itself to and from a formatted stream. If\r
+       **      you add more fields to this class, make sure that you\r
+       **      also write/read them with the writeExternal()/readExternal()\r
+       **      methods.\r
+       **\r
+       **      If, inbetween releases, you add more fields to this class,\r
+       **      then you should bump the version number emitted by the getTypeFormatId()\r
+       **      method.\r
+       **\r
+       ********************************************************/\r
+\r
+       // the array\r
+       private Object[] array;\r
+       \r
+       /**\r
+        * Niladic constructor for formatable\r
+        */\r
+       public FormatableArrayHolder() \r
+       {\r
+       }\r
+\r
+       /**\r
+        * Construct a FormatableArrayHolder using the input\r
+        * array.\r
+        *\r
+        * @param array the array to hold\r
+        */\r
+       public FormatableArrayHolder(Object[] array)\r
+       {\r
+               if (SanityManager.DEBUG)\r
+               {\r
+                       SanityManager.ASSERT(array != null, \r
+                                       "array input to constructor is null, code can't handle this.");\r
+               }\r
+\r
+               this.array = array;\r
+       }\r
+\r
+       /**\r
+        * Set the held array to the input array.\r
+        *\r
+        * @param array the array to hold\r
+        */\r
+       public void setArray(Object[] array)\r
+       {\r
+               if (SanityManager.DEBUG)\r
+               {\r
+                       SanityManager.ASSERT(array != null, \r
+                                       "array input to setArray() is null, code can't handle this.");\r
+               }\r
+\r
+               this.array = array;\r
+       }\r
+\r
+       /**\r
+        * Get the held array of formatables, and return\r
+        * it in an array of type inputClass.\r
+        *\r
+        * @param inputClass    the class to use for the returned array\r
+        *\r
+        * @return an array of formatables\r
+        */\r
+       public Object[] getArray(Class inputClass)\r
+       {\r
+               Object[] outArray = (Object[])Array.newInstance(inputClass, array.length);\r
+               \r
+               /*\r
+               ** HACK: on as400 the following arraycopy() throws an\r
+               ** ArrayStoreException because the output array isn't\r
+               ** assignment compatible with the input array.  This\r
+               ** is a bug on as400, but to get around it we are\r
+               ** going to do an element by element copy.\r
+               */\r
+               //System.arraycopy(array, 0, outArray, 0, outArray.length);\r
+               for (int i = 0; i < outArray.length; i++)\r
+               {\r
+                       outArray[i] = array[i];\r
+               }\r
+\r
+               return outArray;\r
+       }\r
+\r
+       //////////////////////////////////////////////\r
+       //\r
+       // FORMATABLE\r
+       //\r
+       //////////////////////////////////////////////\r
+       /**\r
+        * Write this array out\r
+        *\r
+        * @param out write bytes here\r
+        *\r
+        * @exception IOException thrown on error\r
+        */\r
+       public void writeExternal(ObjectOutput out) throws IOException\r
+       {\r
+               if (SanityManager.DEBUG)\r
+               {\r
+                       SanityManager.ASSERT(array != null, "Array is null, which isn't expected");\r
+               }\r
+\r
+               ArrayUtil.writeArrayLength(out, array);\r
+               ArrayUtil.writeArrayItems(out, array);\r
+       }\r
+\r
+       /**\r
+        * Read this array from a stream of stored objects.\r
+        *\r
+        * @param in read this.\r
+        *\r
+        * @exception IOException                                       thrown on error\r
+        * @exception ClassNotFoundException            thrown on error\r
+        */\r
+       public void readExternal(ObjectInput in)\r
+               throws IOException, ClassNotFoundException\r
+       {\r
+               array = new Object[ArrayUtil.readArrayLength(in)];\r
+               ArrayUtil.readArrayItems(in, array);\r
+       }\r
+       public void readExternal(ArrayInputStream in)\r
+               throws IOException, ClassNotFoundException\r
+       {\r
+               array = new Formatable[ArrayUtil.readArrayLength(in)];\r
+               ArrayUtil.readArrayItems(in, array);\r
+       }\r
+       \r
+       \r
+       /**\r
+        * Get the formatID which corresponds to this class.\r
+        *\r
+        *      @return the formatID of this class\r
+        */\r
+       public  int     getTypeFormatId()       { return StoredFormatIds.FORMATABLE_ARRAY_HOLDER_V01_ID; }\r
+}\r