1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.FormatableIntHolder |
4 | |
5 | Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.io; |
22 | |
23 | import org.apache.derby.iapi.services.io.ArrayInputStream; |
24 | |
25 | import org.apache.derby.iapi.services.io.Formatable; |
26 | import org.apache.derby.iapi.services.io.FormatIdUtil; |
27 | import org.apache.derby.iapi.services.io.StoredFormatIds; |
28 | |
29 | import java.io.ObjectOutput; |
30 | import java.io.ObjectInput; |
31 | import java.io.IOException; |
32 | |
33 | /** |
34 | * A formatable holder for an int. |
35 | */ |
36 | public class FormatableIntHolder implements Formatable |
37 | { |
38 | /******************************************************** |
39 | ** |
40 | ** This class implements Formatable. That means that it |
41 | ** can write itself to and from a formatted stream. If |
42 | ** you add more fields to this class, make sure that you |
43 | ** also write/read them with the writeExternal()/readExternal() |
44 | ** methods. |
45 | ** |
46 | ** If, inbetween releases, you add more fields to this class, |
47 | ** then you should bump the version number emitted by the getTypeFormatId() |
48 | ** method. |
49 | ** |
50 | ********************************************************/ |
51 | |
52 | // the int |
53 | private int theInt; |
54 | |
55 | /** |
56 | * Niladic constructor for formatable |
57 | */ |
58 | public FormatableIntHolder() |
59 | { |
60 | } |
61 | |
62 | /** |
63 | * Construct a FormatableIntHolder using the input int. |
64 | * |
65 | * @param theInt the int to hold |
66 | */ |
67 | public FormatableIntHolder(int theInt) |
68 | { |
69 | this.theInt = theInt; |
70 | } |
71 | |
72 | /** |
73 | * Set the held int to the input int. |
74 | * |
75 | * @param theInt the int to hold |
76 | */ |
77 | public void setInt(int theInt) |
78 | { |
79 | this.theInt = theInt; |
80 | } |
81 | |
82 | /** |
83 | * Get the held int. |
84 | * |
85 | * @return The held int. |
86 | */ |
87 | public int getInt() |
88 | { |
89 | return theInt; |
90 | } |
91 | |
92 | /** |
93 | * Create and return an array of FormatableIntHolders |
94 | * given an array of ints. |
95 | * |
96 | * @param theInts The array of ints |
97 | * |
98 | * @return An array of FormatableIntHolders |
99 | */ |
100 | public static FormatableIntHolder[] getFormatableIntHolders(int[] theInts) |
101 | { |
102 | if (theInts == null) |
103 | { |
104 | return null; |
105 | } |
106 | |
107 | FormatableIntHolder[] fihArray = new FormatableIntHolder[theInts.length]; |
108 | |
109 | for (int index = 0; index < theInts.length; index++) |
110 | { |
111 | fihArray[index] = new FormatableIntHolder(theInts[index]); |
112 | } |
113 | return fihArray; |
114 | } |
115 | |
116 | ////////////////////////////////////////////// |
117 | // |
118 | // FORMATABLE |
119 | // |
120 | ////////////////////////////////////////////// |
121 | /** |
122 | * Write this formatable out |
123 | * |
124 | * @param out write bytes here |
125 | * |
126 | * @exception IOException thrown on error |
127 | */ |
128 | public void writeExternal(ObjectOutput out) throws IOException |
129 | { |
130 | out.writeInt(theInt); |
131 | } |
132 | |
133 | /** |
134 | * Read this formatable from a stream of stored objects. |
135 | * |
136 | * @param in read this. |
137 | * |
138 | * @exception IOException thrown on error |
139 | */ |
140 | public void readExternal(ObjectInput in) |
141 | throws IOException |
142 | { |
143 | theInt = in.readInt(); |
144 | } |
145 | public void readExternal(ArrayInputStream in) |
146 | throws IOException |
147 | { |
148 | theInt = in.readInt(); |
149 | } |
150 | |
151 | /** |
152 | * Get the formatID which corresponds to this class. |
153 | * |
154 | * @return the formatID of this class |
155 | */ |
156 | public int getTypeFormatId() { return StoredFormatIds.FORMATABLE_INT_HOLDER_V01_ID; } |
157 | } |