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.

PieceDescriptor.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.hwpf.model;
  16. import java.nio.charset.Charset;
  17. import java.util.Objects;
  18. import org.apache.poi.common.Duplicatable;
  19. import org.apache.poi.util.Internal;
  20. import org.apache.poi.util.LittleEndian;
  21. import org.apache.poi.util.LittleEndianConsts;
  22. import org.apache.poi.util.StringUtil;
  23. @Internal
  24. public final class PieceDescriptor implements Duplicatable {
  25. private final short descriptor;
  26. int fc; // used from the outside?!?
  27. private final PropertyModifier prm;
  28. private final boolean unicode;
  29. private final Charset charset;
  30. public PieceDescriptor(PieceDescriptor other) {
  31. descriptor = other.descriptor;
  32. fc = other.fc;
  33. prm = (other.prm == null) ? null : other.prm.copy();
  34. unicode = other.unicode;
  35. charset = other.charset;
  36. }
  37. public PieceDescriptor(byte[] buf, int offset) {
  38. this(buf, offset, null);
  39. }
  40. /**
  41. * This initializer should only be used for HWPFOldDocuments.
  42. *
  43. * @param buf The buffer to read data from
  44. * @param offset The offset into the buffer to start reading from
  45. * @param charset which charset to use if this is not unicode
  46. */
  47. public PieceDescriptor(byte[] buf, int offset, Charset charset) {
  48. descriptor = LittleEndian.getShort(buf, offset);
  49. offset += LittleEndianConsts.SHORT_SIZE;
  50. fc = LittleEndian.getInt(buf, offset);
  51. offset += LittleEndianConsts.INT_SIZE;
  52. prm = new PropertyModifier(LittleEndian.getShort(buf, offset));
  53. if (charset == null) {
  54. // see if this piece uses unicode.
  55. //From the documentation: If the second most significant bit
  56. //is clear, then this indicates the actual file offset of the Unicode character (two bytes). If the
  57. //second most significant bit is set, then the actual address of the codepage-1252
  58. //compressed version of the Unicode character (one byte), is actually at the offset indicated
  59. //by clearing this bit and dividing by two.
  60. if ((fc & 0x40000000) == 0) {
  61. unicode = true;
  62. this.charset = null;
  63. } else {
  64. unicode = false;
  65. fc &= ~(0x40000000);//gives me FC in doc stream
  66. fc /= 2;
  67. this.charset = StringUtil.WIN_1252;
  68. }
  69. } else {
  70. if (charset == StringUtil.UTF16LE) {
  71. unicode = true;
  72. } else {
  73. unicode = false;
  74. }
  75. this.charset = charset;
  76. }
  77. }
  78. public int getFilePosition() {
  79. return fc;
  80. }
  81. public void setFilePosition(int pos) {
  82. fc = pos;
  83. }
  84. public boolean isUnicode() {
  85. return unicode;
  86. }
  87. @Override
  88. public int hashCode() {
  89. return Objects.hash(descriptor,prm,unicode);
  90. }
  91. /**
  92. * @return charset to use if this is not a Unicode PieceDescriptor
  93. * this can be <code>null</code>
  94. */
  95. public Charset getCharset() {
  96. return charset;
  97. }
  98. public PropertyModifier getPrm() {
  99. return prm;
  100. }
  101. protected byte[] toByteArray() {
  102. // set up the fc
  103. int tempFc = fc;
  104. if (!unicode) {
  105. tempFc *= 2;
  106. tempFc |= (0x40000000);
  107. }
  108. int offset = 0;
  109. byte[] buf = new byte[8];
  110. LittleEndian.putShort(buf, offset, descriptor);
  111. offset += LittleEndianConsts.SHORT_SIZE;
  112. LittleEndian.putInt(buf, offset, tempFc);
  113. offset += LittleEndianConsts.INT_SIZE;
  114. LittleEndian.putShort(buf, offset, prm.getValue());
  115. return buf;
  116. }
  117. public static int getSizeInBytes() {
  118. return 8;
  119. }
  120. @Override
  121. public boolean equals(Object obj) {
  122. if (this == obj)
  123. return true;
  124. if (obj == null)
  125. return false;
  126. if (getClass() != obj.getClass())
  127. return false;
  128. PieceDescriptor other = (PieceDescriptor) obj;
  129. if (descriptor != other.descriptor)
  130. return false;
  131. if (prm == null) {
  132. if (other.prm != null)
  133. return false;
  134. } else if (!prm.equals(other.prm))
  135. return false;
  136. if (unicode != other.unicode)
  137. return false;
  138. return true;
  139. }
  140. @Override
  141. public String toString() {
  142. return "PieceDescriptor (pos: " + getFilePosition() + "; "
  143. + (isUnicode() ? "unicode" : "non-unicode") + "; prm: "
  144. + getPrm() + ")";
  145. }
  146. @Override
  147. public PieceDescriptor copy() {
  148. return new PieceDescriptor(this);
  149. }
  150. }