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.

BlockStore.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.filesystem;
  16. import java.io.IOException;
  17. import java.nio.ByteBuffer;
  18. import org.apache.poi.poifs.storage.BATBlock.BATBlockAndIndex;
  19. /**
  20. * This abstract class describes a way to read, store, chain
  21. * and free a series of blocks (be they Big or Small ones)
  22. */
  23. public abstract class BlockStore {
  24. /**
  25. * Returns the size of the blocks managed through the block store.
  26. */
  27. protected abstract int getBlockStoreBlockSize();
  28. /**
  29. * Load the block at the given offset.
  30. */
  31. protected abstract ByteBuffer getBlockAt(final int offset) throws IOException;
  32. /**
  33. * Extends the file if required to hold blocks up to
  34. * the specified offset, and return the block from there.
  35. */
  36. protected abstract ByteBuffer createBlockIfNeeded(final int offset) throws IOException;
  37. /**
  38. * Returns the BATBlock that handles the specified offset,
  39. * and the relative index within it
  40. */
  41. protected abstract BATBlockAndIndex getBATBlockAndIndex(final int offset);
  42. /**
  43. * Works out what block follows the specified one.
  44. */
  45. protected abstract int getNextBlock(final int offset);
  46. /**
  47. * Changes the record of what block follows the specified one.
  48. */
  49. protected abstract void setNextBlock(final int offset, final int nextBlock);
  50. /**
  51. * Finds a free block, and returns its offset.
  52. * This method will extend the file/stream if needed, and if doing
  53. * so, allocate new FAT blocks to address the extra space.
  54. */
  55. protected abstract int getFreeBlock() throws IOException;
  56. /**
  57. * Creates a Detector for loops in the chain
  58. */
  59. protected abstract ChainLoopDetector getChainLoopDetector() throws IOException;
  60. /**
  61. * Used to detect if a chain has a loop in it, so
  62. * we can bail out with an error rather than
  63. * spinning away for ever...
  64. */
  65. protected class ChainLoopDetector {
  66. private boolean[] used_blocks;
  67. protected ChainLoopDetector(long rawSize) {
  68. int numBlocks = (int)Math.ceil( rawSize / getBlockStoreBlockSize() );
  69. used_blocks = new boolean[numBlocks];
  70. }
  71. protected void claim(int offset) {
  72. if(offset >= used_blocks.length) {
  73. // They're writing, and have had new blocks requested
  74. // for the write to proceed. That means they're into
  75. // blocks we've allocated for them, so are safe
  76. return;
  77. }
  78. // Claiming an existing block, ensure there's no loop
  79. if(used_blocks[offset]) {
  80. throw new IllegalStateException(
  81. "Potential loop detected - Block " + offset +
  82. " was already claimed but was just requested again"
  83. );
  84. }
  85. used_blocks[offset] = true;
  86. }
  87. }
  88. }