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.

ComplexFileTable.java 4.2KB

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