KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

47
48     public ByteField(final int offset)
49         throws ArrayIndexOutOfBoundsException JavaDoc
50     {
51         this(offset, _default_value);
52     }
53
54     /**
55      * construct the ByteField with its offset into its containing
56      * byte array and initialize its value
57      *
58      * @param offset of the field within its byte array
59      * @param value the initial value
60      *
61      * @exception ArrayIndexOutOfBoundsException if offset is negative
62      */

63
64     public ByteField(final int offset, final byte value)
65         throws ArrayIndexOutOfBoundsException JavaDoc
66     {
67         if (offset < 0)
68         {
69             throw new ArrayIndexOutOfBoundsException JavaDoc(
70                 "offset cannot be negative");
71         }
72         _offset = offset;
73         set(value);
74     }
75
76     /**
77      * Construct the ByteField 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 ByteField(final int offset, final byte [] data)
88         throws ArrayIndexOutOfBoundsException JavaDoc
89     {
90         this(offset);
91         readFromBytes(data);
92     }
93
94     /**
95      * construct the ByteField with its offset into its containing
96      * byte array, initialize its value, and write its value to its
97      * byte 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 not
104      * within the range of 0..(data.length - 1)
105      */

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

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

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

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

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

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

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

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

221
Popular Tags