Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ComplexFileTable.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.nio.charset.Charset;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import org.apache.poi.hwpf.HWPFDocument;
  22. import org.apache.poi.hwpf.model.io.HWPFFileSystem;
  23. import org.apache.poi.hwpf.sprm.SprmBuffer;
  24. import org.apache.poi.util.IOUtils;
  25. import org.apache.poi.util.Internal;
  26. import org.apache.poi.util.LittleEndian;
  27. import org.apache.poi.util.LittleEndianConsts;
  28. import org.apache.poi.util.StringUtil;
  29. @Internal
  30. public class ComplexFileTable {
  31. private static final byte GRPPRL_TYPE = 1;
  32. private static final byte TEXT_PIECE_TABLE_TYPE = 2;
  33. protected TextPieceTable _tpt;
  34. private SprmBuffer[] _grpprls;
  35. public ComplexFileTable() {
  36. _tpt = new TextPieceTable();
  37. }
  38. protected ComplexFileTable(byte[] documentStream, byte[] tableStream, int offset, int fcMin,
  39. Charset charset) throws IOException {
  40. //skips through the prms before we reach the piece table. These contain data
  41. //for actual fast saved files
  42. List<SprmBuffer> sprmBuffers = new LinkedList<>();
  43. while (tableStream[offset] == GRPPRL_TYPE) {
  44. offset++;
  45. int size = LittleEndian.getShort(tableStream, offset);
  46. offset += LittleEndianConsts.SHORT_SIZE;
  47. byte[] bs = IOUtils.safelyClone(tableStream, offset, size, HWPFDocument.getMaxRecordLength());
  48. offset += size;
  49. SprmBuffer sprmBuffer = new SprmBuffer(bs, false, 0);
  50. sprmBuffers.add(sprmBuffer);
  51. }
  52. this._grpprls = sprmBuffers.toArray(new SprmBuffer[0]);
  53. if (tableStream[offset] != TEXT_PIECE_TABLE_TYPE) {
  54. throw new IOException("The text piece table is corrupted, expected byte value " + TEXT_PIECE_TABLE_TYPE +
  55. " but had " + tableStream[offset]);
  56. }
  57. int pieceTableSize = LittleEndian.getInt(tableStream, ++offset);
  58. offset += LittleEndianConsts.INT_SIZE;
  59. _tpt = newTextPieceTable(documentStream, tableStream, offset, pieceTableSize, fcMin, charset);
  60. }
  61. public ComplexFileTable(byte[] documentStream, byte[] tableStream, int offset, int fcMin) throws IOException {
  62. this(documentStream, tableStream, offset, fcMin, StringUtil.WIN_1252);
  63. }
  64. public TextPieceTable getTextPieceTable() {
  65. return _tpt;
  66. }
  67. public SprmBuffer[] getGrpprls() {
  68. return _grpprls;
  69. }
  70. @Deprecated
  71. public void writeTo(HWPFFileSystem sys) throws IOException {
  72. ByteArrayOutputStream docStream = sys.getStream("WordDocument");
  73. ByteArrayOutputStream tableStream = sys.getStream("1Table");
  74. writeTo(docStream, tableStream);
  75. }
  76. public void writeTo(ByteArrayOutputStream wordDocumentStream,
  77. ByteArrayOutputStream tableStream) throws IOException {
  78. tableStream.write(TEXT_PIECE_TABLE_TYPE);
  79. byte[] table = _tpt.writeTo(wordDocumentStream);
  80. byte[] numHolder = new byte[LittleEndianConsts.INT_SIZE];
  81. LittleEndian.putInt(numHolder, 0, table.length);
  82. tableStream.write(numHolder);
  83. tableStream.write(table);
  84. }
  85. protected TextPieceTable newTextPieceTable(byte[] documentStream,
  86. byte[] tableStream, int offset, int pieceTableSize, int fcMin,
  87. Charset charset) {
  88. return new TextPieceTable(documentStream, tableStream, offset, pieceTableSize, fcMin);
  89. }
  90. }