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 a short (16-bit) field at a fixed location within
026: * a byte array
027: *
028: * @author Marc Johnson (mjohnson at apache dot org
029: */
030:
031: public class ShortField implements FixedField {
032: private short _value;
033: private final int _offset;
034:
035: /**
036: * construct the ShortField 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 offset is negative
042: */
043:
044: public ShortField(final int offset)
045: throws ArrayIndexOutOfBoundsException {
046: if (offset < 0) {
047: throw new ArrayIndexOutOfBoundsException("Illegal offset: "
048: + offset);
049: }
050: _offset = offset;
051: }
052:
053: /**
054: * construct the ShortField 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 offset is negative
061: */
062:
063: public ShortField(final int offset, final short value)
064: throws ArrayIndexOutOfBoundsException {
065: this (offset);
066: set(value);
067: }
068:
069: /**
070: * Construct the ShortField with its offset into its containing
071: * byte array and initialize its value from its byte array
072: *
073: * @param offset of the field within its byte array
074: * @param data the byte array to read the value from
075: *
076: * @exception ArrayIndexOutOfBoundsException if the offset is not
077: * within the range of 0..(data.length - 1)
078: */
079:
080: public ShortField(final int offset, final byte[] data)
081: throws ArrayIndexOutOfBoundsException {
082: this (offset);
083: readFromBytes(data);
084: }
085:
086: /**
087: * construct the ShortField with its offset into its containing
088: * byte array, initialize its value, and write its value to its
089: * byte array
090: *
091: * @param offset of the field within its byte array
092: * @param value the initial value
093: * @param data the byte array to write the value to
094: *
095: * @exception ArrayIndexOutOfBoundsException if offset is negative
096: */
097:
098: public ShortField(final int offset, final short value,
099: final byte[] data) throws ArrayIndexOutOfBoundsException {
100: this (offset);
101: set(value, data);
102: }
103:
104: /**
105: * get the ShortField's current value
106: *
107: * @return current value
108: */
109:
110: public short get() {
111: return _value;
112: }
113:
114: /**
115: * set the ShortField's current value
116: *
117: * @param value to be set
118: */
119:
120: public void set(final short value) {
121: _value = value;
122: }
123:
124: /**
125: * set the ShortField's current value and write it to a byte array
126: *
127: * @param value to be set
128: * @param data the byte array to write the value to
129: *
130: * @exception ArrayIndexOutOfBoundsException if the offset is out
131: * of range
132: */
133:
134: public void set(final short value, final byte[] data)
135: throws ArrayIndexOutOfBoundsException {
136: _value = value;
137: writeToBytes(data);
138: }
139:
140: /* ********** START implementation of FixedField ********** */
141:
142: /**
143: * set the value from its offset into an array of bytes
144: *
145: * @param data the byte array from which the value is to be read
146: *
147: * @exception ArrayIndexOutOfBoundsException if the offset is out
148: * of range
149: */
150:
151: public void readFromBytes(final byte[] data)
152: throws ArrayIndexOutOfBoundsException {
153: _value = LittleEndian.getShort(data, _offset);
154: }
155:
156: /**
157: * set the value from an InputStream
158: *
159: * @param stream the InputStream from which the value is to be
160: * read
161: *
162: * @exception BufferUnderrunException if there is not enough data
163: * available from the InputStream
164: * @exception IOException if an IOException is thrown from reading
165: * the InputStream
166: */
167:
168: public void readFromStream(final InputStream stream)
169: throws IOException, BufferUnderrunException {
170: _value = LittleEndian.readShort(stream);
171: }
172:
173: /**
174: * write the value out to an array of bytes at the appropriate
175: * offset
176: *
177: * @param data the array of bytes to which the value is to be
178: * written
179: *
180: * @exception ArrayIndexOutOfBoundsException if the offset is out
181: * of range
182: */
183:
184: public void writeToBytes(final byte[] data)
185: throws ArrayIndexOutOfBoundsException {
186: LittleEndian.putShort(data, _offset, _value);
187: }
188:
189: /**
190: * return the value as a String
191: *
192: * @return the value as a String
193: */
194:
195: public String toString() {
196: return String.valueOf(_value);
197: }
198:
199: /* ********** END implementation of FixedField ********** */
200: } // end public class ShortField
|