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.

UnicodeString.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.hpsf;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.io.UnsupportedEncodingException;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.poi.util.CodePageUtil;
  22. import org.apache.poi.util.IOUtils;
  23. import org.apache.poi.util.Internal;
  24. import org.apache.poi.util.LittleEndian;
  25. import org.apache.poi.util.LittleEndianByteArrayInputStream;
  26. import org.apache.poi.util.LittleEndianConsts;
  27. import org.apache.poi.util.StringUtil;
  28. @Internal
  29. public class UnicodeString {
  30. private static final Logger LOG = LogManager.getLogger(UnicodeString.class);
  31. //arbitrarily selected; may need to increase
  32. private static final int MAX_RECORD_LENGTH = 100_000;
  33. private byte[] _value;
  34. public void read(LittleEndianByteArrayInputStream lei) {
  35. final int length = lei.readInt();
  36. final int unicodeBytes = length*2;
  37. _value = IOUtils.safelyAllocate(unicodeBytes, MAX_RECORD_LENGTH);
  38. // If Length is zero, this field MUST be zero bytes in length. If Length is
  39. // nonzero, this field MUST be a null-terminated array of 16-bit Unicode characters, followed by
  40. // zero padding to a multiple of 4 bytes. The string represented by this field SHOULD NOT
  41. // contain embedded or additional trailing null characters.
  42. if (length == 0) {
  43. return;
  44. }
  45. final int offset = lei.getReadIndex();
  46. lei.readFully(_value);
  47. if (_value[unicodeBytes-2] != 0 || _value[unicodeBytes-1] != 0) {
  48. String msg = "UnicodeString started at offset #" + offset + " is not NULL-terminated";
  49. throw new IllegalPropertySetDataException(msg);
  50. }
  51. TypedPropertyValue.skipPadding(lei);
  52. }
  53. public byte[] getValue() {
  54. return _value;
  55. }
  56. public String toJavaString() {
  57. if ( _value.length == 0 ) {
  58. return null;
  59. }
  60. String result = StringUtil.getFromUnicodeLE( _value, 0, _value.length >> 1 );
  61. final int terminator = result.indexOf( '\0' );
  62. if ( terminator == -1 ) {
  63. LOG.atWarn().log("String terminator (\\0) for UnicodeString property value not found. " +
  64. "Continue without trimming and hope for the best.");
  65. return result;
  66. }
  67. if ( terminator != result.length() - 1 ) {
  68. LOG.atWarn().log("String terminator (\\0) for UnicodeString property value occured before the end of " +
  69. "string. Trimming and hope for the best.");
  70. }
  71. return result.substring( 0, terminator );
  72. }
  73. public void setJavaValue( String string ) throws UnsupportedEncodingException {
  74. _value = CodePageUtil.getBytesInCodePage(string + "\0", CodePageUtil.CP_UNICODE);
  75. }
  76. public int write( OutputStream out ) throws IOException {
  77. LittleEndian.putUInt( _value.length / 2, out );
  78. out.write( _value );
  79. return LittleEndianConsts.INT_SIZE + _value.length;
  80. }
  81. }