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.

OldSheetRecord.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.hssf.record;
  16. import java.io.IOException;
  17. import org.apache.poi.util.HexDump;
  18. import org.apache.poi.util.IOUtils;
  19. import org.apache.poi.util.RecordFormatException;
  20. /**
  21. * Title: Bound Sheet Record (aka BundleSheet) (0x0085) for BIFF 5<P>
  22. * Description: Defines a sheet within a workbook. Basically stores the sheet name
  23. * and tells where the Beginning of file record is within the HSSF
  24. * file.
  25. */
  26. public final class OldSheetRecord {
  27. //arbitrarily selected; may need to increase
  28. private static final int MAX_RECORD_LENGTH = 100_000;
  29. public static final short sid = 0x0085;
  30. private int field_1_position_of_BOF;
  31. private int field_2_visibility;
  32. private int field_3_type;
  33. private byte[] field_5_sheetname;
  34. private CodepageRecord codepage;
  35. public OldSheetRecord(RecordInputStream in) {
  36. field_1_position_of_BOF = in.readInt();
  37. field_2_visibility = in.readUByte();
  38. field_3_type = in.readUByte();
  39. int field_4_sheetname_length = in.readUByte();
  40. if (field_4_sheetname_length > 0) {
  41. in.mark(1);
  42. byte b = in.readByte();
  43. // if the sheet name starts with a 0, we need to skip one byte, otherwise the following records will
  44. // fail with a LeftOverDataException
  45. if (b != 0) {
  46. try {
  47. in.reset();
  48. } catch (IOException e) {
  49. throw new RecordFormatException(e);
  50. }
  51. }
  52. }
  53. field_5_sheetname = IOUtils.safelyAllocate(field_4_sheetname_length, MAX_RECORD_LENGTH);
  54. in.read(field_5_sheetname, 0, field_4_sheetname_length);
  55. }
  56. public void setCodePage(CodepageRecord codepage) {
  57. this.codepage = codepage;
  58. }
  59. public short getSid() {
  60. return sid;
  61. }
  62. /**
  63. * get the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
  64. *
  65. * @return offset in bytes
  66. */
  67. public int getPositionOfBof() {
  68. return field_1_position_of_BOF;
  69. }
  70. /**
  71. * get the sheetname for this sheet. (this appears in the tabs at the bottom)
  72. * @return sheetname the name of the sheet
  73. */
  74. public String getSheetname() {
  75. return OldStringRecord.getString(field_5_sheetname, codepage);
  76. }
  77. public String toString() {
  78. StringBuilder buffer = new StringBuilder();
  79. buffer.append("[BOUNDSHEET]\n");
  80. buffer.append(" .bof = ").append(HexDump.intToHex(getPositionOfBof())).append("\n");
  81. buffer.append(" .visibility = ").append(HexDump.shortToHex(field_2_visibility)).append("\n");
  82. buffer.append(" .type = ").append(HexDump.byteToHex(field_3_type)).append("\n");
  83. buffer.append(" .sheetname = ").append(getSheetname()).append("\n");
  84. buffer.append("[/BOUNDSHEET]\n");
  85. return buffer.toString();
  86. }
  87. }