PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/poifs/storage/SmallBlockTableWriter.java

http://github.com/openmicroscopy/bioformats
Java | 174 lines | 65 code | 26 blank | 83 comment | 7 complexity | cf01af29ac515ba6e90066ff6f3822e7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.poifs.storage;
  38. import loci.poi.poifs.filesystem.BATManaged;
  39. import loci.poi.poifs.filesystem.POIFSDocument;
  40. import loci.poi.poifs.property.RootProperty;
  41. import java.util.*;
  42. import java.io.*;
  43. /**
  44. * This class implements storage for writing the small blocks used by
  45. * small documents.
  46. *
  47. * @author Marc Johnson (mjohnson at apache dot org)
  48. */
  49. public class SmallBlockTableWriter
  50. implements BlockWritable, BATManaged
  51. {
  52. private BlockAllocationTableWriter _sbat;
  53. private List _small_blocks;
  54. private int _big_block_count;
  55. private RootProperty _root;
  56. /**
  57. * Creates new SmallBlockTable
  58. *
  59. * @param documents a List of POIFSDocument instances
  60. * @param root the Filesystem's root property
  61. */
  62. public SmallBlockTableWriter(final List documents,
  63. final RootProperty root)
  64. {
  65. _sbat = new BlockAllocationTableWriter();
  66. _small_blocks = new ArrayList();
  67. _root = root;
  68. Iterator iter = documents.iterator();
  69. int size = 0;
  70. while (iter.hasNext())
  71. {
  72. POIFSDocument doc = ( POIFSDocument ) iter.next();
  73. BlockWritable[] blocks = doc.getSmallBlocks();
  74. if (size == 0) size = doc.getBigBlockSize();
  75. if (blocks.length != 0)
  76. {
  77. doc.setStartBlock(_sbat.allocateSpace(blocks.length));
  78. for (int j = 0; j < blocks.length; j++)
  79. {
  80. _small_blocks.add(blocks[ j ]);
  81. }
  82. }
  83. }
  84. _sbat.simpleCreateBlocks(size);
  85. _root.setSize(_small_blocks.size());
  86. _big_block_count = SmallDocumentBlock.fill(_small_blocks, size);
  87. }
  88. /**
  89. * Get the number of SBAT blocks
  90. *
  91. * @return number of SBAT big blocks
  92. */
  93. public int getSBATBlockCount()
  94. {
  95. return (_big_block_count + 15) / 16;
  96. }
  97. /**
  98. * Get the SBAT
  99. *
  100. * @return the Small Block Allocation Table
  101. */
  102. public BlockAllocationTableWriter getSBAT()
  103. {
  104. return _sbat;
  105. }
  106. /* ********** START implementation of BATManaged ********** */
  107. /**
  108. * Return the number of BigBlock's this instance uses
  109. *
  110. * @return count of BigBlock instances
  111. */
  112. public int countBlocks()
  113. {
  114. return _big_block_count;
  115. }
  116. /**
  117. * Set the start block for this instance
  118. *
  119. * @param start_block
  120. */
  121. public void setStartBlock(int start_block)
  122. {
  123. _root.setStartBlock(start_block);
  124. }
  125. /* ********** END implementation of BATManaged ********** */
  126. /* ********** START implementation of BlockWritable ********** */
  127. /**
  128. * Write the storage to an OutputStream
  129. *
  130. * @param stream the OutputStream to which the stored data should
  131. * be written
  132. *
  133. * @exception IOException on problems writing to the specified
  134. * stream
  135. */
  136. public void writeBlocks(final OutputStream stream)
  137. throws IOException
  138. {
  139. Iterator iter = _small_blocks.iterator();
  140. while (iter.hasNext())
  141. {
  142. (( BlockWritable ) iter.next()).writeBlocks(stream);
  143. }
  144. }
  145. /* ********** END implementation of BlockWritable ********** */
  146. }