KickJava   Java API By Example, From Geeks To Geeks.

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


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 a short (16-bit) field at a fixed location within
27  * a byte array
28  *
29  * @author Marc Johnson (mjohnson at apache dot org
30  */

31
32 public class ShortField
33     implements FixedField
34 {
35     private short _value;
36     private final int _offset;
37
38     /**
39      * construct the ShortField 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 offset is negative
45      */

46
47     public ShortField(final int offset)
48         throws ArrayIndexOutOfBoundsException JavaDoc
49     {
50         if (offset < 0)
51         {
52             throw new ArrayIndexOutOfBoundsException JavaDoc("Illegal offset: "
53                                                      + offset);
54         }
55         _offset = offset;
56     }
57
58     /**
59      * construct the ShortField 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 offset is negative
66      */

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

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

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

117
118     public short get()
119     {
120         return _value;
121     }
122
123     /**
124      * set the ShortField's current value
125      *
126      * @param value to be set
127      */

128
129     public void set(final short value)
130     {
131         _value = value;
132     }
133
134     /**
135      * set the ShortField's current value and write it to a byte array
136      *
137      * @param value to be set
138      * @param data the byte array to write the value to
139      *
140      * @exception ArrayIndexOutOfBoundsException if the offset is out
141      * of range
142      */

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

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

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

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

208
209     public String JavaDoc toString()
210     {
211         return String.valueOf(_value);
212     }
213
214     /* ********** END implementation of FixedField ********** */
215 } // end public class ShortField
216

217
Popular Tags