001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import org.apache.poi.util.LittleEndian.BufferUnderrunException;
021:
022: import java.io.*;
023:
024: /**
025: * representation of an integer (32-bit) field at a fixed location
026: * within a byte array
027: *
028: * @author Marc Johnson (mjohnson at apache dot org
029: */
030:
031: public class IntegerField implements FixedField {
032: private int _value;
033: private final int _offset;
034:
035: /**
036: * construct the IntegerField with its offset into its containing
037: * byte array
038: *
039: * @param offset of the field within its byte array
040: *
041: * @exception ArrayIndexOutOfBoundsException if the offset is
042: * negative
043: */
044:
045: public IntegerField(final int offset)
046: throws ArrayIndexOutOfBoundsException {
047: if (offset < 0) {
048: throw new ArrayIndexOutOfBoundsException("negative offset");
049: }
050: _offset = offset;
051: }
052:
053: /**
054: * construct the IntegerField with its offset into its containing
055: * byte array and initialize its value
056: *
057: * @param offset of the field within its byte array
058: * @param value the initial value
059: *
060: * @exception ArrayIndexOutOfBoundsException if the offset is
061: * negative
062: */
063:
064: public IntegerField(final int offset, final int value)
065: throws ArrayIndexOutOfBoundsException {
066: this (offset);
067: set(value);
068: }
069:
070: /**
071: * Construct the IntegerField with its offset into its containing
072: * byte array and initialize its value from its byte array
073: *
074: * @param offset of the field within its byte array
075: * @param data the byte array to read the value from
076: *
077: * @exception ArrayIndexOutOfBoundsException if the offset is not
078: * within the range of 0..(data.length - 1)
079: */
080:
081: public IntegerField(final int offset, final byte[] data)
082: throws ArrayIndexOutOfBoundsException {
083: this (offset);
084: readFromBytes(data);
085: }
086:
087: /**
088: * construct the IntegerField with its offset into its containing
089: * byte array, initialize its value, and write the value to a byte
090: * array
091: *
092: * @param offset of the field within its byte array
093: * @param value the initial value
094: * @param data the byte array to write the value to
095: *
096: * @exception ArrayIndexOutOfBoundsException if the offset is
097: * negative or too large
098: */
099:
100: public IntegerField(final int offset, final int value,
101: final byte[] data) throws ArrayIndexOutOfBoundsException {
102: this (offset);
103: set(value, data);
104: }
105:
106: /**
107: * get the IntegerField's current value
108: *
109: * @return current value
110: */
111:
112: public int get() {
113: return _value;
114: }
115:
116: /**
117: * set the IntegerField's current value
118: *
119: * @param value to be set
120: */
121:
122: public void set(final int value) {
123: _value = value;
124: }
125:
126: /**
127: * set the IntegerField's current value and write it to a byte
128: * array
129: *
130: * @param value to be set
131: * @param data the byte array to write the value to
132: *
133: * @exception ArrayIndexOutOfBoundsException if the offset is too
134: * large
135: */
136:
137: public void set(final int value, final byte[] data)
138: throws ArrayIndexOutOfBoundsException {
139: _value = value;
140: writeToBytes(data);
141: }
142:
143: /* ********** START implementation of FixedField ********** */
144:
145: /**
146: * set the value from its offset into an array of bytes
147: *
148: * @param data the byte array from which the value is to be read
149: *
150: * @exception ArrayIndexOutOfBoundsException if the offset is too
151: * large
152: */
153:
154: public void readFromBytes(final byte[] data)
155: throws ArrayIndexOutOfBoundsException {
156: _value = LittleEndian.getInt(data, _offset);
157: }
158:
159: /**
160: * set the value from an InputStream
161: *
162: * @param stream the InputStream from which the value is to be
163: * read
164: *
165: * @exception BufferUnderrunException if there is not enough data
166: * available from the InputStream
167: * @exception IOException if an IOException is thrown from reading
168: * the InputStream
169: */
170:
171: public void readFromStream(final InputStream stream)
172: throws IOException, BufferUnderrunException {
173: _value = LittleEndian.readInt(stream);
174: }
175:
176: /**
177: * write the value out to an array of bytes at the appropriate
178: * offset
179: *
180: * @param data the array of bytes to which the value is to be
181: * written
182: *
183: * @exception ArrayIndexOutOfBoundsException if the offset is too
184: * large
185: */
186:
187: public void writeToBytes(final byte[] data)
188: throws ArrayIndexOutOfBoundsException {
189: LittleEndian.putInt(data, _offset, _value);
190: }
191:
192: /**
193: * return the value as a String
194: *
195: * @return the value as a String
196: */
197:
198: public String toString() {
199: return String.valueOf(_value);
200: }
201:
202: /* ********** END implementation of FixedField ********** */
203: } // end public class IntegerField
|