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.

CString.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.GenericRecordUtil;
  21. import org.apache.poi.util.IOUtils;
  22. import org.apache.poi.util.LittleEndian;
  23. import org.apache.poi.util.StringUtil;
  24. /**
  25. * A CString (type 4026). Holds a unicode string, and the first two bytes
  26. * of the record header normally encode the count. Typically attached to
  27. * some complex sequence of records, eg Commetns.
  28. *
  29. * @author Nick Burch
  30. */
  31. public final class CString extends RecordAtom {
  32. //arbitrarily selected; may need to increase
  33. private static final int MAX_RECORD_LENGTH = 1_000_000;
  34. private byte[] _header;
  35. /** The bytes that make up the text */
  36. private byte[] _text;
  37. /** Grabs the text. Never <code>null</code> */
  38. public String getText() {
  39. return StringUtil.getFromUnicodeLE(_text);
  40. }
  41. /** Updates the text in the Atom. */
  42. public void setText(String text) {
  43. // Convert to little endian unicode
  44. _text = new byte[text.length()*2];
  45. StringUtil.putUnicodeLE(text,_text,0);
  46. // Update the size (header bytes 5-8)
  47. LittleEndian.putInt(_header,4,_text.length);
  48. }
  49. /**
  50. * Grabs the count, from the first two bytes of the header.
  51. * The meaning of the count is specific to the type of the parent record
  52. */
  53. public int getOptions() {
  54. return LittleEndian.getShort(_header);
  55. }
  56. /**
  57. * Sets the count
  58. * The meaning of the count is specific to the type of the parent record
  59. */
  60. public void setOptions(int count) {
  61. LittleEndian.putShort(_header, 0, (short)count);
  62. }
  63. /* *************** record code follows ********************** */
  64. /**
  65. * For the CStrubg Atom
  66. */
  67. protected CString(byte[] source, int start, int len) {
  68. // Sanity Checking
  69. if(len < 8) { len = 8; }
  70. // Get the header
  71. _header = new byte[8];
  72. System.arraycopy(source,start,_header,0,8);
  73. // Grab the text
  74. _text = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH);
  75. System.arraycopy(source,start+8,_text,0,len-8);
  76. }
  77. /**
  78. * Create an empty CString
  79. */
  80. public CString() {
  81. // 0 length header
  82. _header = new byte[] { 0, 0, 0xBA-256, 0x0f, 0, 0, 0, 0 };
  83. // Empty text
  84. _text = new byte[0];
  85. }
  86. /**
  87. * We are of type 4026
  88. */
  89. public long getRecordType() {
  90. return RecordTypes.CString.typeID;
  91. }
  92. /**
  93. * Write the contents of the record back, so it can be written
  94. * to disk
  95. */
  96. public void writeOut(OutputStream out) throws IOException {
  97. // Header - size or type unchanged
  98. out.write(_header);
  99. // Write out our text
  100. out.write(_text);
  101. }
  102. /**
  103. * Gets a string representation of this object, primarily for debugging.
  104. * @return a string representation of this object.
  105. */
  106. public String toString() {
  107. return getText();
  108. }
  109. @Override
  110. public Map<String, Supplier<?>> getGenericProperties() {
  111. return GenericRecordUtil.getGenericProperties("text", this::getText);
  112. }
  113. }