KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > IntegerField


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import org.apache.poi.util.LittleEndian.BufferUnderrunException;
22
23 import java.io.*;
24
25 /**
26  * representation of an integer (32-bit) field at a fixed location
27  * within a byte array
28  *
29  * @author Marc Johnson (mjohnson at apache dot org
30  */

31
32 public class IntegerField
33     implements FixedField
34 {
35     private int _value;
36     private final int _offset;
37
38     /**
39      * construct the IntegerField with its offset into its containing
40      * byte array
41      *
42      * @param offset of the field within its byte array
43      *
44      * @exception ArrayIndexOutOfBoundsException if the offset is
45      * negative
46      */

47
48     public IntegerField(final int offset)
49         throws ArrayIndexOutOfBoundsException JavaDoc
50     {
51         if (offset < 0)
52         {
53             throw new ArrayIndexOutOfBoundsException JavaDoc("negative offset");
54         }
55         _offset = offset;
56     }
57
58     /**
59      * construct the IntegerField with its offset into its containing
60      * byte array and initialize its value
61      *
62      * @param offset of the field within its byte array
63      * @param value the initial value
64      *
65      * @exception ArrayIndexOutOfBoundsException if the offset is
66      * negative
67      */

68
69     public IntegerField(final int offset, final int value)
70         throws ArrayIndexOutOfBoundsException JavaDoc
71     {
72         this(offset);
73         set(value);
74     }
75
76     /**
77      * Construct the IntegerField with its offset into its containing
78      * byte array and initialize its value from its byte array
79      *
80      * @param offset of the field within its byte array
81      * @param data the byte array to read the value from
82      *
83      * @exception ArrayIndexOutOfBoundsException if the offset is not
84      * within the range of 0..(data.length - 1)
85      */

86
87     public IntegerField(final int offset, final byte [] data)
88         throws ArrayIndexOutOfBoundsException JavaDoc
89     {
90         this(offset);
91         readFromBytes(data);
92     }
93
94     /**
95      * construct the IntegerField with its offset into its containing
96      * byte array, initialize its value, and write the value to a byte
97      * array
98      *
99      * @param offset of the field within its byte array
100      * @param value the initial value
101      * @param data the byte array to write the value to
102      *
103      * @exception ArrayIndexOutOfBoundsException if the offset is
104      * negative or too large
105      */

106
107     public IntegerField(final int offset, final int value, final byte [] data)
108         throws ArrayIndexOutOfBoundsException JavaDoc
109     {
110         this(offset);
111         set(value, data);
112     }
113
114     /**
115      * get the IntegerField's current value
116      *
117      * @return current value
118      */

119
120     public int get()
121     {
122         return _value;
123     }
124
125     /**
126      * set the IntegerField's current value
127      *
128      * @param value to be set
129      */

130
131     public void set(final int value)
132     {
133         _value = value;
134     }
135
136     /**
137      * set the IntegerField's current value and write it to a byte
138      * array
139      *
140      * @param value to be set
141      * @param data the byte array to write the value to
142      *
143      * @exception ArrayIndexOutOfBoundsException if the offset is too
144      * large
145      */

146
147     public void set(final int value, final byte [] data)
148         throws ArrayIndexOutOfBoundsException JavaDoc
149     {
150         _value = value;
151         writeToBytes(data);
152     }
153
154     /* ********** START implementation of FixedField ********** */
155
156     /**
157      * set the value from its offset into an array of bytes
158      *
159      * @param data the byte array from which the value is to be read
160      *
161      * @exception ArrayIndexOutOfBoundsException if the offset is too
162      * large
163      */

164
165     public void readFromBytes(final byte [] data)
166         throws ArrayIndexOutOfBoundsException JavaDoc
167     {
168         _value = LittleEndian.getInt(data, _offset);
169     }
170
171     /**
172      * set the value from an InputStream
173      *
174      * @param stream the InputStream from which the value is to be
175      * read
176      *
177      * @exception BufferUnderrunException if there is not enough data
178      * available from the InputStream
179      * @exception IOException if an IOException is thrown from reading
180      * the InputStream
181      */

182
183     public void readFromStream(final InputStream stream)
184         throws IOException, BufferUnderrunException
185     {
186         _value = LittleEndian.readInt(stream);
187     }
188
189     /**
190      * write the value out to an array of bytes at the appropriate
191      * offset
192      *
193      * @param data the array of bytes to which the value is to be
194      * written
195      *
196      * @exception ArrayIndexOutOfBoundsException if the offset is too
197      * large
198      */

199
200     public void writeToBytes(final byte [] data)
201         throws ArrayIndexOutOfBoundsException JavaDoc
202     {
203         LittleEndian.putInt(data, _offset, _value);
204     }
205
206     /**
207      * return the value as a String
208      *
209      * @return the value as a String
210      */

211
212     public String JavaDoc toString()
213     {
214         return String.valueOf(_value);
215     }
216
217     /* ********** END implementation of FixedField ********** */
218 } // end public class IntegerField
219

220
Popular Tags