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.9KB

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