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.

OldTextPieceTable.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.ArrayList;
  18. import java.util.Collections;
  19. import org.apache.poi.hwpf.util.DoubleByteUtil;
  20. import org.apache.poi.util.IOUtils;
  21. import org.apache.poi.util.Internal;
  22. @Internal
  23. public class OldTextPieceTable extends TextPieceTable {
  24. //arbitrarily selected; may need to increase
  25. private static final int MAX_RECORD_LENGTH = 100_000_000;
  26. public OldTextPieceTable() {
  27. super();
  28. }
  29. public OldTextPieceTable(byte[] documentStream, byte[] tableStream,
  30. int offset, int size, int fcMin, Charset charset) {
  31. //super(documentStream, tableStream, offset, size, fcMin, charset);
  32. // get our plex of PieceDescriptors
  33. PlexOfCps pieceTable = new PlexOfCps(tableStream, offset, size,
  34. PieceDescriptor.getSizeInBytes());
  35. int length = pieceTable.length();
  36. PieceDescriptor[] pieces = new PieceDescriptor[length];
  37. // iterate through piece descriptors raw bytes and create
  38. // PieceDescriptor objects
  39. for (int x = 0; x < length; x++) {
  40. GenericPropertyNode node = pieceTable.getProperty(x);
  41. pieces[x] = new PieceDescriptor(node.getBytes(), 0, charset);
  42. }
  43. // Figure out the cp of the earliest text piece
  44. // Note that text pieces don't have to be stored in order!
  45. _cpMin = pieces[0].getFilePosition() - fcMin;
  46. for (PieceDescriptor piece : pieces) {
  47. int start = piece.getFilePosition() - fcMin;
  48. if (start < _cpMin) {
  49. _cpMin = start;
  50. }
  51. }
  52. // using the PieceDescriptors, build our list of TextPieces.
  53. for (int x = 0; x < pieces.length; x++) {
  54. int start = pieces[x].getFilePosition();
  55. GenericPropertyNode node = pieceTable.getProperty(x);
  56. // Grab the start and end, which are in characters
  57. int nodeStartChars = node.getStart();
  58. int nodeEndChars = node.getEnd();
  59. // What's the relationship between bytes and characters?
  60. boolean unicode = pieces[x].isUnicode();
  61. int multiple = 1;
  62. if (unicode ||
  63. (charset != null && DoubleByteUtil.DOUBLE_BYTE_CHARSETS.contains(charset))) {
  64. multiple = 2;
  65. }
  66. // Figure out the length, in bytes and chars
  67. int textSizeChars = (nodeEndChars - nodeStartChars);
  68. int textSizeBytes = textSizeChars * multiple;
  69. // Grab the data that makes up the piece
  70. byte[] buf = IOUtils.safelyClone(documentStream, start, textSizeBytes, MAX_RECORD_LENGTH);
  71. // And now build the piece
  72. final TextPiece newTextPiece = newTextPiece(nodeStartChars, nodeEndChars, buf, pieces[x]);
  73. _textPieces.add(newTextPiece);
  74. }
  75. // In the interest of our sanity, now sort the text pieces
  76. // into order, if they're not already
  77. Collections.sort(_textPieces);
  78. _textPiecesFCOrder = new ArrayList<>(_textPieces);
  79. _textPiecesFCOrder.sort(byFilePosition());
  80. }
  81. @Override
  82. protected TextPiece newTextPiece(int nodeStartChars, int nodeEndChars, byte[] buf, PieceDescriptor pd) {
  83. return new OldTextPiece(nodeStartChars, nodeEndChars, buf, pd);
  84. }
  85. @Override
  86. protected int getEncodingMultiplier(TextPiece textPiece) {
  87. Charset charset = textPiece.getPieceDescriptor().getCharset();
  88. if (charset != null && DoubleByteUtil.DOUBLE_BYTE_CHARSETS.contains(charset)) {
  89. return 2;
  90. }
  91. return 1;
  92. }
  93. }