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.

OldLabelRecord.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.util.GenericRecordUtil;
  19. import org.apache.poi.util.HexDump;
  20. import org.apache.poi.util.IOUtils;
  21. import org.apache.poi.util.POILogFactory;
  22. import org.apache.poi.util.POILogger;
  23. import org.apache.poi.util.RecordFormatException;
  24. /**
  25. * Biff2 - Biff 4 Label Record (0x0004 / 0x0204) - read only support for
  26. * strings stored directly in the cell, from the older file formats that
  27. * didn't use {@link LabelSSTRecord}
  28. */
  29. public final class OldLabelRecord extends OldCellRecord {
  30. private static final POILogger LOG = POILogFactory.getLogger(OldLabelRecord.class);
  31. //arbitrarily set, may need to increase
  32. private static final int MAX_RECORD_LENGTH = 100_000;
  33. public static final short biff2_sid = 0x0004;
  34. public static final short biff345_sid = 0x0204;
  35. private short field_4_string_len;
  36. private final byte[] field_5_bytes;
  37. private CodepageRecord codepage;
  38. /**
  39. * @param in the RecordInputstream to read the record from
  40. */
  41. public OldLabelRecord(RecordInputStream in)
  42. {
  43. super(in, in.getSid() == biff2_sid);
  44. if (isBiff2()) {
  45. field_4_string_len = (short)in.readUByte();
  46. } else {
  47. field_4_string_len = in.readShort();
  48. }
  49. // Can only decode properly later when you know the codepage
  50. field_5_bytes = IOUtils.safelyAllocate(field_4_string_len, MAX_RECORD_LENGTH);
  51. in.read(field_5_bytes, 0, field_4_string_len);
  52. if (in.remaining() > 0) {
  53. LOG.log(POILogger.INFO,
  54. "LabelRecord data remains: ", in.remaining(),
  55. " : ", HexDump.toHex(in.readRemainder())
  56. );
  57. }
  58. }
  59. public void setCodePage(CodepageRecord codepage) {
  60. this.codepage = codepage;
  61. }
  62. /**
  63. * get the number of characters this string contains
  64. * @return number of characters
  65. */
  66. public short getStringLength()
  67. {
  68. return field_4_string_len;
  69. }
  70. /**
  71. * Get the String of the cell
  72. *
  73. * @return the String of the cell
  74. */
  75. public String getValue()
  76. {
  77. return OldStringRecord.getString(field_5_bytes, codepage);
  78. }
  79. /**
  80. * Not supported
  81. *
  82. * @param offset not supported
  83. * @param data not supported
  84. * @return not supported
  85. */
  86. public int serialize(int offset, byte [] data) {
  87. throw new RecordFormatException("Old Label Records are supported READ ONLY");
  88. }
  89. public int getRecordSize() {
  90. throw new RecordFormatException("Old Label Records are supported READ ONLY");
  91. }
  92. @Override
  93. public HSSFRecordTypes getGenericRecordType() {
  94. return HSSFRecordTypes.LABEL;
  95. }
  96. @Override
  97. public Map<String, Supplier<?>> getGenericProperties() {
  98. return GenericRecordUtil.getGenericProperties(
  99. "base", super::getGenericProperties,
  100. "stringLength", this::getStringLength,
  101. "value", this::getValue
  102. );
  103. }
  104. }