KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > io > FormatableArrayHolder


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.io.FormatableArrayHolder
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.io;
23
24 import org.apache.derby.iapi.services.io.ArrayInputStream;
25
26 import org.apache.derby.iapi.services.io.StoredFormatIds;
27 import org.apache.derby.iapi.services.io.FormatIdUtil;
28 import org.apache.derby.iapi.services.io.ArrayUtil;
29 import org.apache.derby.iapi.services.io.Formatable;
30
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32
33 import java.io.ObjectOutput JavaDoc;
34 import java.io.ObjectInput JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import java.lang.reflect.Array JavaDoc;
38
39 /**
40  * A formatable holder for an array of formatables.
41  * Used to avoid serializing arrays.
42  */

43 public class FormatableArrayHolder implements Formatable
44 {
45     /********************************************************
46     **
47     ** This class implements Formatable. That means that it
48     ** can write itself to and from a formatted stream. If
49     ** you add more fields to this class, make sure that you
50     ** also write/read them with the writeExternal()/readExternal()
51     ** methods.
52     **
53     ** If, inbetween releases, you add more fields to this class,
54     ** then you should bump the version number emitted by the getTypeFormatId()
55     ** method.
56     **
57     ********************************************************/

58
59     // the array
60
private Object JavaDoc[] array;
61     
62     /**
63      * Niladic constructor for formatable
64      */

65     public FormatableArrayHolder()
66     {
67     }
68
69     /**
70      * Construct a FormatableArrayHolder using the input
71      * array.
72      *
73      * @param array the array to hold
74      */

75     public FormatableArrayHolder(Object JavaDoc[] array)
76     {
77         if (SanityManager.DEBUG)
78         {
79             SanityManager.ASSERT(array != null,
80                     "array input to constructor is null, code can't handle this.");
81         }
82
83         this.array = array;
84     }
85
86     /**
87      * Set the held array to the input array.
88      *
89      * @param array the array to hold
90      */

91     public void setArray(Object JavaDoc[] array)
92     {
93         if (SanityManager.DEBUG)
94         {
95             SanityManager.ASSERT(array != null,
96                     "array input to setArray() is null, code can't handle this.");
97         }
98
99         this.array = array;
100     }
101
102     /**
103      * Get the held array of formatables, and return
104      * it in an array of type inputClass.
105      *
106      * @param inputClass the class to use for the returned array
107      *
108      * @return an array of formatables
109      */

110     public Object JavaDoc[] getArray(Class JavaDoc inputClass)
111     {
112         Object JavaDoc[] outArray = (Object JavaDoc[])Array.newInstance(inputClass, array.length);
113         
114         /*
115         ** HACK: on as400 the following arraycopy() throws an
116         ** ArrayStoreException because the output array isn't
117         ** assignment compatible with the input array. This
118         ** is a bug on as400, but to get around it we are
119         ** going to do an element by element copy.
120         */

121         //System.arraycopy(array, 0, outArray, 0, outArray.length);
122
for (int i = 0; i < outArray.length; i++)
123         {
124             outArray[i] = array[i];
125         }
126
127         return outArray;
128     }
129
130     //////////////////////////////////////////////
131
//
132
// FORMATABLE
133
//
134
//////////////////////////////////////////////
135
/**
136      * Write this array out
137      *
138      * @param out write bytes here
139      *
140      * @exception IOException thrown on error
141      */

142     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
143     {
144         if (SanityManager.DEBUG)
145         {
146             SanityManager.ASSERT(array != null, "Array is null, which isn't expected");
147         }
148
149         ArrayUtil.writeArrayLength(out, array);
150         ArrayUtil.writeArrayItems(out, array);
151     }
152
153     /**
154      * Read this array from a stream of stored objects.
155      *
156      * @param in read this.
157      *
158      * @exception IOException thrown on error
159      * @exception ClassNotFoundException thrown on error
160      */

161     public void readExternal(ObjectInput JavaDoc in)
162         throws IOException JavaDoc, ClassNotFoundException JavaDoc
163     {
164         array = new Object JavaDoc[ArrayUtil.readArrayLength(in)];
165         ArrayUtil.readArrayItems(in, array);
166     }
167     public void readExternal(ArrayInputStream in)
168         throws IOException JavaDoc, ClassNotFoundException JavaDoc
169     {
170         array = new Formatable[ArrayUtil.readArrayLength(in)];
171         ArrayUtil.readArrayItems(in, array);
172     }
173     
174     
175     /**
176      * Get the formatID which corresponds to this class.
177      *
178      * @return the formatID of this class
179      */

180     public int getTypeFormatId() { return StoredFormatIds.FORMATABLE_ARRAY_HOLDER_V01_ID; }
181 }
182
Popular Tags