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.

OldStringRecord.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.UnsupportedEncodingException;
  17. import java.util.Map;
  18. import java.util.function.Supplier;
  19. import org.apache.poi.common.usermodel.GenericRecord;
  20. import org.apache.poi.hpsf.Property;
  21. import org.apache.poi.util.CodePageUtil;
  22. import org.apache.poi.util.GenericRecordJsonWriter;
  23. import org.apache.poi.util.GenericRecordUtil;
  24. import org.apache.poi.util.IOUtils;
  25. /**
  26. * Biff2 - Biff 4 Label Record (0x0007 / 0x0207) - read only support for
  27. * formula string results.
  28. */
  29. public final class OldStringRecord implements GenericRecord {
  30. //arbitrarily selected; may need to increase
  31. private static final int MAX_RECORD_LENGTH = 100_000;
  32. public static final short biff2_sid = 0x0007;
  33. public static final short biff345_sid = 0x0207;
  34. private short sid;
  35. private short field_1_string_len;
  36. private byte[] field_2_bytes;
  37. private CodepageRecord codepage;
  38. /**
  39. * @param in the RecordInputstream to read the record from
  40. */
  41. public OldStringRecord(RecordInputStream in) {
  42. sid = in.getSid();
  43. if (in.getSid() == biff2_sid) {
  44. field_1_string_len = (short)in.readUByte();
  45. } else {
  46. field_1_string_len = in.readShort();
  47. }
  48. // Can only decode properly later when you know the codepage
  49. field_2_bytes = IOUtils.safelyAllocate(field_1_string_len, MAX_RECORD_LENGTH);
  50. in.read(field_2_bytes, 0, field_1_string_len);
  51. }
  52. public boolean isBiff2() {
  53. return sid == biff2_sid;
  54. }
  55. public short getSid() {
  56. return sid;
  57. }
  58. public void setCodePage(CodepageRecord codepage) {
  59. this.codepage = codepage;
  60. }
  61. /**
  62. * @return The string represented by this record.
  63. */
  64. public String getString()
  65. {
  66. return getString(field_2_bytes, codepage);
  67. }
  68. protected static String getString(byte[] data, CodepageRecord codepage) {
  69. int cp = Property.DEFAULT_CODEPAGE;
  70. if (codepage != null) {
  71. cp = codepage.getCodepage() & 0xffff;
  72. }
  73. try {
  74. return CodePageUtil.getStringFromCodePage(data, cp);
  75. } catch (UnsupportedEncodingException uee) {
  76. throw new IllegalArgumentException("Unsupported codepage requested", uee);
  77. }
  78. }
  79. @Override
  80. public HSSFRecordTypes getGenericRecordType() {
  81. return HSSFRecordTypes.STRING;
  82. }
  83. @Override
  84. public Map<String, Supplier<?>> getGenericProperties() {
  85. return GenericRecordUtil.getGenericProperties("string", this::getString);
  86. }
  87. public String toString() {
  88. return GenericRecordJsonWriter.marshal(this);
  89. }
  90. }