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.

TextBytesAtom.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.hslf.record;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.util.Map;
  19. import java.util.function.Supplier;
  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.LittleEndian;
  24. import org.apache.poi.util.StringUtil;
  25. /**
  26. * A TextBytesAtom (type 4008). Holds text in ascii form (unknown
  27. * code page, for now assumed to be the default of
  28. * org.apache.poi.util.StringUtil, which is the Excel default).
  29. * The trailing return character is always stripped from this
  30. */
  31. public final class TextBytesAtom extends RecordAtom {
  32. public static final long _type = RecordTypes.TextBytesAtom.typeID;
  33. //arbitrarily selected; may need to increase
  34. private static final int MAX_RECORD_LENGTH = 1_000_000;
  35. private byte[] _header;
  36. /** The bytes that make up the text */
  37. private byte[] _text;
  38. /** Grabs the text. Uses the default codepage */
  39. public String getText() {
  40. return StringUtil.getFromCompressedUnicode(_text,0,_text.length);
  41. }
  42. /** Updates the text in the Atom. Must be 8 bit ascii */
  43. public void setText(byte[] b) {
  44. // Set the text
  45. _text = b.clone();
  46. // Update the size (header bytes 5-8)
  47. LittleEndian.putInt(_header,4,_text.length);
  48. }
  49. /* *************** record code follows ********************** */
  50. /**
  51. * For the TextBytes Atom
  52. */
  53. protected TextBytesAtom(byte[] source, int start, int len) {
  54. // Sanity Checking
  55. if(len < 8) { len = 8; }
  56. // Get the header
  57. _header = new byte[8];
  58. System.arraycopy(source,start,_header,0,8);
  59. // Grab the text
  60. _text = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH);
  61. System.arraycopy(source,start+8,_text,0,len-8);
  62. }
  63. /**
  64. * Create an empty TextBytes Atom
  65. */
  66. public TextBytesAtom() {
  67. _header = new byte[8];
  68. LittleEndian.putUShort(_header, 0, 0);
  69. LittleEndian.putUShort(_header, 2, (int)_type);
  70. LittleEndian.putInt(_header, 4, 0);
  71. _text = new byte[]{};
  72. }
  73. /**
  74. * We are of type 4008
  75. */
  76. @Override
  77. public long getRecordType() { return _type; }
  78. /**
  79. * Write the contents of the record back, so it can be written
  80. * to disk
  81. */
  82. @Override
  83. public void writeOut(OutputStream out) throws IOException {
  84. // Header - size or type unchanged
  85. out.write(_header);
  86. // Write out our text
  87. out.write(_text);
  88. }
  89. /**
  90. * dump debug info; use getText() to return a string
  91. * representation of the atom
  92. */
  93. @Override
  94. public String toString() {
  95. return GenericRecordJsonWriter.marshal(this);
  96. }
  97. @Override
  98. public Map<String, Supplier<?>> getGenericProperties() {
  99. return GenericRecordUtil.getGenericProperties(
  100. "text", this::getText
  101. );
  102. }
  103. }