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.

LongField.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 a long (16-bit) field at a fixed location within
  20. * a byte array
  21. */
  22. public class LongField
  23. implements FixedField
  24. {
  25. private long _value;
  26. private final int _offset;
  27. /**
  28. * construct the LongField 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 offset is negative
  34. */
  35. public LongField(final int offset)
  36. throws ArrayIndexOutOfBoundsException
  37. {
  38. if (offset < 0)
  39. {
  40. throw new ArrayIndexOutOfBoundsException("Illegal offset: "
  41. + offset);
  42. }
  43. _offset = offset;
  44. }
  45. /**
  46. * construct the LongField 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 offset is negative
  53. */
  54. public LongField(final int offset, final long value)
  55. throws ArrayIndexOutOfBoundsException
  56. {
  57. this(offset);
  58. set(value);
  59. }
  60. /**
  61. * Construct the LongField with its offset into its containing
  62. * byte array and initialize its value from its byte array
  63. *
  64. * @param offset of the field within its byte array
  65. * @param data the byte array to read the value from
  66. *
  67. * @throws ArrayIndexOutOfBoundsException if the offset is not
  68. * within the range of 0..(data.length - 1)
  69. */
  70. public LongField(final int offset, final byte [] data)
  71. throws ArrayIndexOutOfBoundsException
  72. {
  73. this(offset);
  74. readFromBytes(data);
  75. }
  76. /**
  77. * construct the LongField with its offset into its containing
  78. * byte array, initialize its value, and write the value to a byte
  79. * array
  80. *
  81. * @param offset of the field within its byte array
  82. * @param value the initial value
  83. * @param data the byte array to write the value to
  84. *
  85. * @throws ArrayIndexOutOfBoundsException if offset is negative
  86. */
  87. public LongField(final int offset, final long value, final byte [] data)
  88. throws ArrayIndexOutOfBoundsException
  89. {
  90. this(offset);
  91. set(value, data);
  92. }
  93. /**
  94. * get the LongField's current value
  95. *
  96. * @return current value
  97. */
  98. public long get()
  99. {
  100. return _value;
  101. }
  102. /**
  103. * set the LongField's current value
  104. *
  105. * @param value to be set
  106. */
  107. public void set(final long value)
  108. {
  109. _value = value;
  110. }
  111. /**
  112. * set the LongField's current value and write it to a byte array
  113. *
  114. * @param value to be set
  115. * @param data the byte array to write the value to
  116. *
  117. * @throws ArrayIndexOutOfBoundsException if the offset is out
  118. * of range
  119. */
  120. public void set(final long value, final byte [] data)
  121. throws ArrayIndexOutOfBoundsException
  122. {
  123. _value = value;
  124. writeToBytes(data);
  125. }
  126. /* ********** START implementation of FixedField ********** */
  127. /**
  128. * set the value from its offset into an array of bytes
  129. *
  130. * @param data the byte array from which the value is to be read
  131. *
  132. * @throws ArrayIndexOutOfBoundsException if the offset is out
  133. * of range
  134. */
  135. public void readFromBytes(final byte [] data)
  136. throws ArrayIndexOutOfBoundsException
  137. {
  138. _value = LittleEndian.getLong(data, _offset);
  139. }
  140. /**
  141. * set the value from an InputStream
  142. *
  143. * @param stream the InputStream from which the value is to be
  144. * read
  145. *
  146. * @throws BufferUnderrunException if there is not enough data
  147. * available from the InputStream
  148. * @throws IOException if an IOException is thrown from reading
  149. * the InputStream
  150. */
  151. public void readFromStream(final InputStream stream)
  152. throws IOException
  153. {
  154. _value = LittleEndian.readLong(stream);
  155. }
  156. /**
  157. * write the value out to an array of bytes at the appropriate
  158. * offset
  159. *
  160. * @param data the array of bytes to which the value is to be
  161. * written
  162. *
  163. * @throws ArrayIndexOutOfBoundsException if the offset is out
  164. * of range
  165. */
  166. public void writeToBytes(final byte [] data)
  167. throws ArrayIndexOutOfBoundsException
  168. {
  169. LittleEndian.putLong(data, _offset, _value);
  170. }
  171. /**
  172. * return the value as a String
  173. *
  174. * @return the value as a String
  175. */
  176. public String toString()
  177. {
  178. return String.valueOf(_value);
  179. }
  180. /* ********** END implementation of FixedField ********** */
  181. } // end public class LongField