You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ByteField.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import org.apache.poi.util.LittleEndian.BufferUnderrunException;
  17. import java.io.*;
  18. import java.nio.BufferUnderflowException;
  19. /**
  20. * representation of a byte (8-bit) field at a fixed location within a
  21. * byte array
  22. */
  23. public class ByteField
  24. implements FixedField
  25. {
  26. private static final byte _default_value = 0;
  27. private byte _value;
  28. private final int _offset;
  29. /**
  30. * construct the ByteField with its offset into its containing
  31. * byte array and a default value of 0
  32. *
  33. * @param offset of the field within its byte array
  34. *
  35. * @throws ArrayIndexOutOfBoundsException if offset is negative
  36. */
  37. public ByteField(final int offset)
  38. throws ArrayIndexOutOfBoundsException
  39. {
  40. this(offset, _default_value);
  41. }
  42. /**
  43. * construct the ByteField with its offset into its containing
  44. * byte array and initialize its value
  45. *
  46. * @param offset of the field within its byte array
  47. * @param value the initial value
  48. *
  49. * @throws ArrayIndexOutOfBoundsException if offset is negative
  50. */
  51. public ByteField(final int offset, final byte value)
  52. throws ArrayIndexOutOfBoundsException
  53. {
  54. if (offset < 0)
  55. {
  56. throw new ArrayIndexOutOfBoundsException(
  57. "offset cannot be negative");
  58. }
  59. _offset = offset;
  60. set(value);
  61. }
  62. /**
  63. * Construct the ByteField with its offset into its containing
  64. * byte array and initialize its value from its byte array
  65. *
  66. * @param offset of the field within its byte array
  67. * @param data the byte array to read the value from
  68. *
  69. * @throws ArrayIndexOutOfBoundsException if the offset is not
  70. * within the range of 0..(data.length - 1)
  71. */
  72. public ByteField(final int offset, final byte [] data)
  73. throws ArrayIndexOutOfBoundsException
  74. {
  75. this(offset);
  76. readFromBytes(data);
  77. }
  78. /**
  79. * construct the ByteField with its offset into its containing
  80. * byte array, initialize its value, and write its value to its
  81. * byte array
  82. *
  83. * @param offset of the field within its byte array
  84. * @param value the initial value
  85. * @param data the byte array to write the value to
  86. *
  87. * @throws ArrayIndexOutOfBoundsException if the offset is not
  88. * within the range of 0..(data.length - 1)
  89. */
  90. public ByteField(final int offset, final byte value, final byte [] data)
  91. throws ArrayIndexOutOfBoundsException
  92. {
  93. this(offset, value);
  94. writeToBytes(data);
  95. }
  96. /**
  97. * get the ByteField's current value
  98. *
  99. * @return current value
  100. */
  101. public byte get()
  102. {
  103. return _value;
  104. }
  105. /**
  106. * set the ByteField's current value
  107. *
  108. * @param value to be set
  109. */
  110. public void set(final byte value)
  111. {
  112. _value = value;
  113. }
  114. /**
  115. * set the ByteField's current value and write it to a byte array
  116. *
  117. * @param value to be set
  118. * @param data the byte array to write the value to
  119. *
  120. * @throws ArrayIndexOutOfBoundsException if the offset is out
  121. * of the byte array's range
  122. */
  123. public void set(final byte value, final byte [] data)
  124. throws ArrayIndexOutOfBoundsException
  125. {
  126. set(value);
  127. writeToBytes(data);
  128. }
  129. /* ********** START implementation of FixedField ********** */
  130. /**
  131. * set the value from its offset into an array of bytes
  132. *
  133. * @param data the byte array from which the value is to be read
  134. *
  135. * @throws ArrayIndexOutOfBoundsException if the offset is out
  136. * of range of the bte array
  137. */
  138. public void readFromBytes(final byte [] data)
  139. throws ArrayIndexOutOfBoundsException
  140. {
  141. _value = data[ _offset ];
  142. }
  143. /**
  144. * set the value from an InputStream
  145. *
  146. * @param stream the InputStream from which the value is to be
  147. * read
  148. *
  149. * @throws BufferUnderrunException if there is not enough data
  150. * available from the InputStream
  151. * @throws IOException if an IOException is thrown from reading
  152. * the InputStream
  153. */
  154. public void readFromStream(final InputStream stream)
  155. throws IOException
  156. {
  157. // TODO - are these ~Field used / necessary
  158. int ib = stream.read();
  159. if (ib < 0) {
  160. throw new BufferUnderflowException();
  161. }
  162. _value = (byte) ib;
  163. }
  164. /**
  165. * write the value out to an array of bytes at the appropriate
  166. * offset
  167. *
  168. * @param data the array of bytes to which the value is to be
  169. * written
  170. *
  171. * @throws ArrayIndexOutOfBoundsException if the offset is out
  172. * of the byte array's range
  173. */
  174. public void writeToBytes(final byte [] data)
  175. throws ArrayIndexOutOfBoundsException
  176. {
  177. data[ _offset ] = _value;
  178. }
  179. /**
  180. * return the value as a String
  181. *
  182. * @return the value as a String
  183. */
  184. public String toString()
  185. {
  186. return String.valueOf(_value);
  187. }
  188. /* ********** END implementation of FixedField ********** */
  189. } // end public class ByteField