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.

BlockListImpl.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.storage;
  16. import java.io.IOException;
  17. /**
  18. * A simple implementation of BlockList
  19. *
  20. * @author Marc Johnson (mjohnson at apache dot org
  21. */
  22. abstract class BlockListImpl implements BlockList {
  23. private ListManagedBlock[] _blocks;
  24. private BlockAllocationTableReader _bat;
  25. protected BlockListImpl()
  26. {
  27. _blocks = new ListManagedBlock[ 0 ];
  28. _bat = null;
  29. }
  30. /**
  31. * provide blocks to manage
  32. *
  33. * @param blocks blocks to be managed
  34. */
  35. protected void setBlocks(final ListManagedBlock [] blocks)
  36. {
  37. _blocks = blocks;
  38. }
  39. /**
  40. * remove the specified block from the list
  41. *
  42. * @param index the index of the specified block; if the index is
  43. * out of range, that's ok
  44. */
  45. public void zap(final int index)
  46. {
  47. if ((index >= 0) && (index < _blocks.length))
  48. {
  49. _blocks[ index ] = null;
  50. }
  51. }
  52. /**
  53. * Unit testing method. Gets, without sanity checks or
  54. * removing.
  55. */
  56. protected ListManagedBlock get(final int index) {
  57. return _blocks[index];
  58. }
  59. /**
  60. * remove and return the specified block from the list
  61. *
  62. * @param index the index of the specified block
  63. *
  64. * @return the specified block
  65. *
  66. * @exception IOException if the index is out of range or has
  67. * already been removed
  68. */
  69. public ListManagedBlock remove(final int index)
  70. throws IOException
  71. {
  72. ListManagedBlock result = null;
  73. try
  74. {
  75. result = _blocks[ index ];
  76. if (result == null)
  77. {
  78. throw new IOException(
  79. "block[ " + index + " ] already removed - " +
  80. "does your POIFS have circular or duplicate block references?"
  81. );
  82. }
  83. _blocks[ index ] = null;
  84. }
  85. catch (ArrayIndexOutOfBoundsException ignored)
  86. {
  87. throw new IOException("Cannot remove block[ " + index
  88. + " ]; out of range[ 0 - " +
  89. (_blocks.length-1) + " ]");
  90. }
  91. return result;
  92. }
  93. /**
  94. * get the blocks making up a particular stream in the list. The
  95. * blocks are removed from the list.
  96. *
  97. * @param startBlock the index of the first block in the stream
  98. *
  99. * @return the stream as an array of correctly ordered blocks
  100. *
  101. * @exception IOException if blocks are missing
  102. */
  103. public ListManagedBlock [] fetchBlocks(final int startBlock, final int headerPropertiesStartBlock)
  104. throws IOException
  105. {
  106. if (_bat == null)
  107. {
  108. throw new IOException(
  109. "Improperly initialized list: no block allocation table provided");
  110. }
  111. return _bat.fetchBlocks(startBlock, headerPropertiesStartBlock, this);
  112. }
  113. /**
  114. * set the associated BlockAllocationTable
  115. *
  116. * @param bat the associated BlockAllocationTable
  117. */
  118. public void setBAT(final BlockAllocationTableReader bat)
  119. throws IOException
  120. {
  121. if (_bat != null)
  122. {
  123. throw new IOException(
  124. "Attempt to replace existing BlockAllocationTable");
  125. }
  126. _bat = bat;
  127. }
  128. /**
  129. * Returns the count of the number of blocks
  130. */
  131. public int blockCount() {
  132. return _blocks.length;
  133. }
  134. /**
  135. * Returns the number of remaining blocks
  136. */
  137. protected int remainingBlocks() {
  138. int c = 0;
  139. for(int i=0; i<_blocks.length; i++) {
  140. if(_blocks[i] != null) c++;
  141. }
  142. return c;
  143. }
  144. }