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.

NPropertyTable.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.poifs.property;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.nio.ByteBuffer;
  19. import java.util.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import org.apache.poi.poifs.common.POIFSBigBlockSize;
  23. import org.apache.poi.poifs.common.POIFSConstants;
  24. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  25. import org.apache.poi.poifs.filesystem.NPOIFSStream;
  26. import org.apache.poi.poifs.storage.HeaderBlock;
  27. /**
  28. * This class embodies the Property Table for a {@link NPOIFSFileSystem};
  29. * this is basically the directory for all of the documents in the
  30. * filesystem.
  31. */
  32. public final class NPropertyTable extends PropertyTableBase {
  33. private POIFSBigBlockSize _bigBigBlockSize;
  34. public NPropertyTable(HeaderBlock headerBlock)
  35. {
  36. super(headerBlock);
  37. _bigBigBlockSize = headerBlock.getBigBlockSize();
  38. }
  39. /**
  40. * reading constructor (used when we've read in a file and we want
  41. * to extract the property table from it). Populates the
  42. * properties thoroughly
  43. *
  44. * @param headerBlock the header block of the file
  45. * @param filesystem the filesystem to read from
  46. *
  47. * @exception IOException if anything goes wrong (which should be
  48. * a result of the input being NFG)
  49. */
  50. public NPropertyTable(final HeaderBlock headerBlock,
  51. final NPOIFSFileSystem filesystem)
  52. throws IOException
  53. {
  54. super(
  55. headerBlock,
  56. buildProperties(
  57. (new NPOIFSStream(filesystem, headerBlock.getPropertyStart())).iterator(),
  58. headerBlock.getBigBlockSize()
  59. )
  60. );
  61. _bigBigBlockSize = headerBlock.getBigBlockSize();
  62. }
  63. /**
  64. * Builds
  65. * @param startAt
  66. * @param filesystem
  67. * @return
  68. * @throws IOException
  69. */
  70. private static List<Property> buildProperties(final Iterator<ByteBuffer> dataSource,
  71. final POIFSBigBlockSize bigBlockSize) throws IOException
  72. {
  73. List<Property> properties = new ArrayList<Property>();
  74. while(dataSource.hasNext()) {
  75. ByteBuffer bb = dataSource.next();
  76. // Turn it into an array
  77. byte[] data;
  78. if(bb.hasArray() && bb.arrayOffset() == 0 &&
  79. bb.array().length == bigBlockSize.getBigBlockSize()) {
  80. data = bb.array();
  81. } else {
  82. data = new byte[bigBlockSize.getBigBlockSize()];
  83. bb.get(data, 0, data.length);
  84. }
  85. PropertyFactory.convertToProperties(data, properties);
  86. }
  87. return properties;
  88. }
  89. /**
  90. * Return the number of BigBlock's this instance uses
  91. *
  92. * @return count of BigBlock instances
  93. */
  94. public int countBlocks()
  95. {
  96. int size = _properties.size() * POIFSConstants.PROPERTY_SIZE;
  97. return (int)Math.ceil(size / _bigBigBlockSize.getBigBlockSize());
  98. }
  99. /**
  100. * Writes the properties out into the given low-level stream
  101. */
  102. public void write(NPOIFSStream stream) throws IOException {
  103. // TODO - Use a streaming write
  104. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  105. for(Property property : _properties) {
  106. property.writeData(baos);
  107. }
  108. stream.updateContents(baos.toByteArray());
  109. // Update the start position if needed
  110. if(getStartBlock() != stream.getStartBlock()) {
  111. setStartBlock(stream.getStartBlock());
  112. }
  113. }
  114. }