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 byte (8-bit) field at a fixed location within a
026: * byte array
027: *
028: * @author Marc Johnson (mjohnson at apache dot org
029: */
030:
031: public class ByteField implements FixedField {
032: private static final byte _default_value = 0;
033: private byte _value;
034: private final int _offset;
035:
036: /**
037: * construct the ByteField with its offset into its containing
038: * byte array and a default value of 0
039: *
040: * @param offset of the field within its byte array
041: *
042: * @exception ArrayIndexOutOfBoundsException if offset is negative
043: */
044:
045: public ByteField(final int offset)
046: throws ArrayIndexOutOfBoundsException {
047: this (offset, _default_value);
048: }
049:
050: /**
051: * construct the ByteField with its offset into its containing
052: * byte array and initialize its value
053: *
054: * @param offset of the field within its byte array
055: * @param value the initial value
056: *
057: * @exception ArrayIndexOutOfBoundsException if offset is negative
058: */
059:
060: public ByteField(final int offset, final byte value)
061: throws ArrayIndexOutOfBoundsException {
062: if (offset < 0) {
063: throw new ArrayIndexOutOfBoundsException(
064: "offset cannot be negative");
065: }
066: _offset = offset;
067: set(value);
068: }
069:
070: /**
071: * Construct the ByteField 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 ByteField(final int offset, final byte[] data)
082: throws ArrayIndexOutOfBoundsException {
083: this (offset);
084: readFromBytes(data);
085: }
086:
087: /**
088: * construct the ByteField with its offset into its containing
089: * byte array, initialize its value, and write its value to its
090: * byte 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 not
097: * within the range of 0..(data.length - 1)
098: */
099:
100: public ByteField(final int offset, final byte value,
101: final byte[] data) throws ArrayIndexOutOfBoundsException {
102: this (offset, value);
103: writeToBytes(data);
104: }
105:
106: /**
107: * get the ByteField's current value
108: *
109: * @return current value
110: */
111:
112: public byte get() {
113: return _value;
114: }
115:
116: /**
117: * set the ByteField's current value
118: *
119: * @param value to be set
120: */
121:
122: public void set(final byte value) {
123: _value = value;
124: }
125:
126: /**
127: * set the ByteField's current value and write it to a byte array
128: *
129: * @param value to be set
130: * @param data the byte array to write the value to
131: *
132: * @exception ArrayIndexOutOfBoundsException if the offset is out
133: * of the byte array's range
134: */
135:
136: public void set(final byte value, final byte[] data)
137: throws ArrayIndexOutOfBoundsException {
138: set(value);
139: writeToBytes(data);
140: }
141:
142: /* ********** START implementation of FixedField ********** */
143:
144: /**
145: * set the value from its offset into an array of bytes
146: *
147: * @param data the byte array from which the value is to be read
148: *
149: * @exception ArrayIndexOutOfBoundsException if the offset is out
150: * of range of the bte array
151: */
152:
153: public void readFromBytes(final byte[] data)
154: throws ArrayIndexOutOfBoundsException {
155: _value = data[_offset];
156: }
157:
158: /**
159: * set the value from an InputStream
160: *
161: * @param stream the InputStream from which the value is to be
162: * read
163: *
164: * @exception BufferUnderrunException if there is not enough data
165: * available from the InputStream
166: * @exception IOException if an IOException is thrown from reading
167: * the InputStream
168: */
169:
170: public void readFromStream(final InputStream stream)
171: throws IOException, BufferUnderrunException {
172: _value = (LittleEndian.readFromStream(stream,
173: LittleEndianConsts.BYTE_SIZE))[0];
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 out
184: * of the byte array's range
185: */
186:
187: public void writeToBytes(final byte[] data)
188: throws ArrayIndexOutOfBoundsException {
189: 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 ByteField
|