1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.util;
57   
58   import org.apache.poi.util.LittleEndian.BufferUnderrunException;
59   
60   import java.io.*;
61   
62   /**
63    * representation of a byte (8-bit) field at a fixed location within a
64    * byte array
65    *
66    * @author Marc Johnson (mjohnson at apache dot org
67    */
68   
69   public class ByteField
70       implements FixedField
71   {
72       private static final byte _default_value = 0;
73       private byte              _value;
74       private final int         _offset;
75   
76       /**
77        * construct the ByteField with its offset into its containing
78        * byte array and a default value of 0
79        *
80        * @param offset of the field within its byte array
81        *
82        * @exception ArrayIndexOutOfBoundsException if offset is negative
83        */
84   
85       public ByteField(final int offset)
86           throws ArrayIndexOutOfBoundsException
87       {
88           this(offset, _default_value);
89       }
90   
91       /**
92        * construct the ByteField with its offset into its containing
93        * byte array and initialize its value
94        *
95        * @param offset of the field within its byte array
96        * @param value the initial value
97        *
98        * @exception ArrayIndexOutOfBoundsException if offset is negative
99        */
100  
101      public ByteField(final int offset, final byte value)
102          throws ArrayIndexOutOfBoundsException
103      {
104          if (offset < 0)
105          {
106              throw new ArrayIndexOutOfBoundsException(
107                  "offset cannot be negative");
108          }
109          _offset = offset;
110          set(value);
111      }
112  
113      /**
114       * Construct the ByteField with its offset into its containing
115       * byte array and initialize its value from its byte array
116       *
117       * @param offset of the field within its byte array
118       * @param data the byte array to read the value from
119       *
120       * @exception ArrayIndexOutOfBoundsException if the offset is not
121       *            within the range of 0..(data.length - 1)
122       */
123  
124      public ByteField(final int offset, final byte [] data)
125          throws ArrayIndexOutOfBoundsException
126      {
127          this(offset);
128          readFromBytes(data);
129      }
130  
131      /**
132       * construct the ByteField with its offset into its containing
133       * byte array, initialize its value, and write its value to its
134       * byte array
135       *
136       * @param offset of the field within its byte array
137       * @param value the initial value
138       * @param data the byte array to write the value to
139       *
140       * @exception ArrayIndexOutOfBoundsException if the offset is not
141       *            within the range of 0..(data.length - 1)
142       */
143  
144      public ByteField(final int offset, final byte value, final byte [] data)
145          throws ArrayIndexOutOfBoundsException
146      {
147          this(offset, value);
148          writeToBytes(data);
149      }
150  
151      /**
152       * get the ByteField's current value
153       *
154       * @return current value
155       */
156  
157      public byte get()
158      {
159          return _value;
160      }
161  
162      /**
163       * set the ByteField's current value
164       *
165       * @param value to be set
166       */
167  
168      public void set(final byte value)
169      {
170          _value = value;
171      }
172  
173      /**
174       * set the ByteField's current value and write it to a byte array
175       *
176       * @param value to be set
177       * @param data the byte array to write the value to
178       *
179       * @exception ArrayIndexOutOfBoundsException if the offset is out
180       *            of the byte array's range
181       */
182  
183      public void set(final byte value, final byte [] data)
184          throws ArrayIndexOutOfBoundsException
185      {
186          set(value);
187          writeToBytes(data);
188      }
189  
190      /* ********** START implementation of FixedField ********** */
191  
192      /**
193       * set the value from its offset into an array of bytes
194       *
195       * @param data the byte array from which the value is to be read
196       *
197       * @exception ArrayIndexOutOfBoundsException if the offset is out
198       *            of range of the bte array
199       */
200  
201      public void readFromBytes(final byte [] data)
202          throws ArrayIndexOutOfBoundsException
203      {
204          _value = data[ _offset ];
205      }
206  
207      /**
208       * set the value from an InputStream
209       *
210       * @param stream the InputStream from which the value is to be
211       *               read
212       *
213       * @exception BufferUnderrunException if there is not enough data
214       *            available from the InputStream
215       * @exception IOException if an IOException is thrown from reading
216       *            the InputStream
217       */
218  
219      public void readFromStream(final InputStream stream)
220          throws IOException, BufferUnderrunException
221      {
222          _value =
223              (LittleEndian.readFromStream(stream,
224                                           LittleEndianConsts.BYTE_SIZE))[ 0 ];
225      }
226  
227      /**
228       * write the value out to an array of bytes at the appropriate
229       * offset
230       *
231       * @param data the array of bytes to which the value is to be
232       *             written
233       *
234       * @exception ArrayIndexOutOfBoundsException if the offset is out
235       *            of the byte array's range
236       */
237  
238      public void writeToBytes(final byte [] data)
239          throws ArrayIndexOutOfBoundsException
240      {
241          data[ _offset ] = _value;
242      }
243  
244      /**
245       * return the value as a String
246       *
247       * @return the value as a String
248       */
249  
250      public String toString()
251      {
252          return String.valueOf(_value);
253      }
254  
255      /* **********  END  implementation of FixedField ********** */
256  }   // end public class ByteField
257  
258