PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/HeaderRecord.java

http://github.com/openmicroscopy/bioformats
Java | 236 lines | 124 code | 30 blank | 82 comment | 11 complexity | 205e833b47fcfd91a557536c7d6e451f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.record;
  38. import loci.poi.util.LittleEndian;
  39. import loci.poi.util.StringUtil;
  40. /**
  41. * Title: Header Record<P>
  42. * Description: Specifies a header for a sheet<P>
  43. * REFERENCE: PG 321 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  44. * @author Andrew C. Oliver (acoliver at apache dot org)
  45. * @author Shawn Laubach (slaubach at apache dot org) Modified 3/14/02
  46. * @author Jason Height (jheight at chariot dot net dot au)
  47. * @version 2.0-pre
  48. */
  49. public class HeaderRecord
  50. extends Record
  51. {
  52. public final static short sid = 0x14;
  53. private byte field_1_header_len;
  54. private byte field_2_reserved;
  55. private byte field_3_unicode_flag;
  56. private String field_4_header;
  57. public HeaderRecord()
  58. {
  59. }
  60. /**
  61. * Constructs an Header record and sets its fields appropriately.
  62. * @param in the RecordInputstream to read the record from
  63. */
  64. public HeaderRecord(RecordInputStream in)
  65. {
  66. super(in);
  67. }
  68. protected void validateSid(short id)
  69. {
  70. if (id != sid)
  71. {
  72. throw new RecordFormatException("NOT A HEADERRECORD");
  73. }
  74. }
  75. protected void fillFields(RecordInputStream in)
  76. {
  77. if (in.remaining() > 0)
  78. {
  79. field_1_header_len = in.readByte();
  80. /** These two fields are a bit odd. They are not documented*/
  81. field_2_reserved = in.readByte();
  82. field_3_unicode_flag = in.readByte(); // unicode
  83. if(isMultibyte())
  84. {
  85. field_4_header = in.readUnicodeLEString(LittleEndian.ubyteToInt( field_1_header_len));
  86. }
  87. else
  88. {
  89. field_4_header = in.readCompressedUnicode(LittleEndian.ubyteToInt( field_1_header_len));
  90. }
  91. }
  92. }
  93. /**
  94. * see the unicode flag
  95. *
  96. * @return boolean flag
  97. * true:footer string has at least one multibyte character
  98. */
  99. public boolean isMultibyte() {
  100. return ((field_3_unicode_flag & 0xFF) == 1);
  101. }
  102. /**
  103. * set the length of the header string
  104. *
  105. * @param len length of the header string
  106. * @see #setHeader(String)
  107. */
  108. public void setHeaderLength(byte len)
  109. {
  110. field_1_header_len = len;
  111. }
  112. /**
  113. * set the header string
  114. *
  115. * @param header string to display
  116. * @see #setHeaderLength(byte)
  117. */
  118. public void setHeader(String header)
  119. {
  120. field_4_header = header;
  121. field_3_unicode_flag =
  122. (byte) (StringUtil.hasMultibyte(field_4_header) ? 1 : 0);
  123. }
  124. /**
  125. * get the length of the header string
  126. *
  127. * @return length of the header string
  128. * @see #getHeader()
  129. */
  130. public short getHeaderLength()
  131. {
  132. return (short)(0xFF & field_1_header_len); // [Shawn] Fixed needing unsigned byte
  133. }
  134. /**
  135. * get the header string
  136. *
  137. * @return header string to display
  138. * @see #getHeaderLength()
  139. */
  140. public String getHeader()
  141. {
  142. return field_4_header;
  143. }
  144. public String toString()
  145. {
  146. StringBuffer buffer = new StringBuffer();
  147. buffer.append("[HEADER]\n");
  148. buffer.append(" .length = ").append(getHeaderLength())
  149. .append("\n");
  150. buffer.append(" .header = ").append(getHeader())
  151. .append("\n");
  152. buffer.append("[/HEADER]\n");
  153. return buffer.toString();
  154. }
  155. public int serialize(int offset, byte [] data)
  156. {
  157. int len = 4;
  158. if (getHeaderLength() != 0)
  159. {
  160. len+=3; // [Shawn] Fixed for two null bytes in the length
  161. }
  162. short bytelen = (short)(isMultibyte() ?
  163. getHeaderLength()*2 : getHeaderLength() );
  164. LittleEndian.putShort(data, 0 + offset, sid);
  165. LittleEndian.putShort(data, 2 + offset,
  166. ( short ) ((len - 4) + bytelen));
  167. if (getHeaderLength() > 0)
  168. {
  169. data[ 4 + offset ] = (byte)getHeaderLength();
  170. data[ 6 + offset ] = field_3_unicode_flag;
  171. if(isMultibyte())
  172. {
  173. StringUtil.putUnicodeLE(getHeader(), data, 7 + offset);
  174. }
  175. else
  176. {
  177. StringUtil.putCompressedUnicode(getHeader(), data, 7 + offset); // [Shawn] Place the string in the correct offset
  178. }
  179. }
  180. return getRecordSize();
  181. }
  182. public int getRecordSize()
  183. {
  184. int retval = 4;
  185. if (getHeaderLength() != 0)
  186. {
  187. retval+=3; // [Shawn] Fixed for two null bytes in the length
  188. }
  189. return (isMultibyte() ?
  190. (retval + getHeaderLength()*2) : (retval + getHeaderLength()));
  191. }
  192. public short getSid()
  193. {
  194. return sid;
  195. }
  196. public Object clone() {
  197. HeaderRecord rec = new HeaderRecord();
  198. rec.field_1_header_len = field_1_header_len;
  199. rec.field_2_reserved = field_2_reserved;
  200. rec.field_3_unicode_flag = field_3_unicode_flag;
  201. rec.field_4_header = field_4_header;
  202. return rec;
  203. }
  204. }