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 4.0KB

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