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.

IntegerField.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /**
  19. * representation of an integer (32-bit) field at a fixed location
  20. * within a byte array
  21. */
  22. public class IntegerField
  23. implements FixedField
  24. {
  25. private int _value;
  26. private final int _offset;
  27. /**
  28. * construct the IntegerField with its offset into its containing
  29. * byte array
  30. *
  31. * @param offset of the field within its byte array
  32. *
  33. * @throws ArrayIndexOutOfBoundsException if the offset is
  34. * negative
  35. */
  36. public IntegerField(final int offset)
  37. throws ArrayIndexOutOfBoundsException
  38. {
  39. if (offset < 0)
  40. {
  41. throw new ArrayIndexOutOfBoundsException("negative offset");
  42. }
  43. _offset = offset;
  44. }
  45. /**
  46. * construct the IntegerField with its offset into its containing
  47. * byte array and initialize its value
  48. *
  49. * @param offset of the field within its byte array
  50. * @param value the initial value
  51. *
  52. * @throws ArrayIndexOutOfBoundsException if the offset is
  53. * negative
  54. */
  55. public IntegerField(final int offset, final int value)
  56. throws ArrayIndexOutOfBoundsException
  57. {
  58. this(offset);
  59. set(value);
  60. }
  61. /**
  62. * Construct the IntegerField with its offset into its containing
  63. * byte array and initialize its value from its byte array
  64. *
  65. * @param offset of the field within its byte array
  66. * @param data the byte array to read the value from
  67. *
  68. * @throws ArrayIndexOutOfBoundsException if the offset is not
  69. * within the range of 0..(data.length - 1)
  70. */
  71. public IntegerField(final int offset, final byte [] data)
  72. throws ArrayIndexOutOfBoundsException
  73. {
  74. this(offset);
  75. readFromBytes(data);
  76. }
  77. /**
  78. * construct the IntegerField with its offset into its containing
  79. * byte array, initialize its value, and write the value to a byte
  80. * array
  81. *
  82. * @param offset of the field within its byte array
  83. * @param value the initial value
  84. * @param data the byte array to write the value to
  85. *
  86. * @throws ArrayIndexOutOfBoundsException if the offset is
  87. * negative or too large
  88. */
  89. public IntegerField(final int offset, final int value, final byte [] data)
  90. throws ArrayIndexOutOfBoundsException
  91. {
  92. this(offset);
  93. set(value, data);
  94. }
  95. /**
  96. * get the IntegerField's current value
  97. *
  98. * @return current value
  99. */
  100. public int get()
  101. {
  102. return _value;
  103. }
  104. /**
  105. * set the IntegerField's current value
  106. *
  107. * @param value to be set
  108. */
  109. public void set(final int value)
  110. {
  111. _value = value;
  112. }
  113. /**
  114. * set the IntegerField's current value and write it to a byte
  115. * 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 too
  121. * large
  122. */
  123. public void set(final int value, final byte [] data)
  124. throws ArrayIndexOutOfBoundsException
  125. {
  126. _value = 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 too
  136. * large
  137. */
  138. public void readFromBytes(final byte [] data)
  139. throws ArrayIndexOutOfBoundsException
  140. {
  141. _value = LittleEndian.getInt(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. _value = LittleEndian.readInt(stream);
  158. }
  159. /**
  160. * write the value out to an array of bytes at the appropriate
  161. * offset
  162. *
  163. * @param data the array of bytes to which the value is to be
  164. * written
  165. *
  166. * @throws ArrayIndexOutOfBoundsException if the offset is too
  167. * large
  168. */
  169. public void writeToBytes(final byte [] data)
  170. throws ArrayIndexOutOfBoundsException
  171. {
  172. LittleEndian.putInt(data, _offset, _value);
  173. }
  174. /**
  175. * return the value as a String
  176. *
  177. * @return the value as a String
  178. */
  179. public String toString()
  180. {
  181. return String.valueOf(_value);
  182. }
  183. /* ********** END implementation of FixedField ********** */
  184. } // end public class IntegerField