001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.storage;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: /**
025: * A simple implementation of BlockList
026: *
027: * @author Marc Johnson (mjohnson at apache dot org
028: */
029:
030: class BlockListImpl implements BlockList {
031: private ListManagedBlock[] _blocks;
032: private BlockAllocationTableReader _bat;
033:
034: /**
035: * Constructor BlockListImpl
036: */
037:
038: protected BlockListImpl() {
039: _blocks = new ListManagedBlock[0];
040: _bat = null;
041: }
042:
043: /**
044: * provide blocks to manage
045: *
046: * @param blocks blocks to be managed
047: */
048:
049: protected void setBlocks(final ListManagedBlock[] blocks) {
050: _blocks = blocks;
051: }
052:
053: /* ********** START implementation of BlockList ********** */
054:
055: /**
056: * remove the specified block from the list
057: *
058: * @param index the index of the specified block; if the index is
059: * out of range, that's ok
060: */
061:
062: public void zap(final int index) {
063: if ((index >= 0) && (index < _blocks.length)) {
064: _blocks[index] = null;
065: }
066: }
067:
068: /**
069: * remove and return the specified block from the list
070: *
071: * @param index the index of the specified block
072: *
073: * @return the specified block
074: *
075: * @exception IOException if the index is out of range or has
076: * already been removed
077: */
078:
079: public ListManagedBlock remove(final int index) throws IOException {
080: ListManagedBlock result = null;
081:
082: try {
083: result = _blocks[index];
084: if (result == null) {
085: throw new IOException("block[ " + index
086: + " ] already removed");
087: }
088: _blocks[index] = null;
089: } catch (ArrayIndexOutOfBoundsException ignored) {
090: throw new IOException("Cannot remove block[ " + index
091: + " ]; out of range");
092: }
093: return result;
094: }
095:
096: /**
097: * get the blocks making up a particular stream in the list. The
098: * blocks are removed from the list.
099: *
100: * @param startBlock the index of the first block in the stream
101: *
102: * @return the stream as an array of correctly ordered blocks
103: *
104: * @exception IOException if blocks are missing
105: */
106:
107: public ListManagedBlock[] fetchBlocks(final int startBlock)
108: throws IOException {
109: if (_bat == null) {
110: throw new IOException(
111: "Improperly initialized list: no block allocation table provided");
112: }
113: return _bat.fetchBlocks(startBlock, this );
114: }
115:
116: /**
117: * set the associated BlockAllocationTable
118: *
119: * @param bat the associated BlockAllocationTable
120: *
121: * @exception IOException
122: */
123:
124: public void setBAT(final BlockAllocationTableReader bat)
125: throws IOException {
126: if (_bat != null) {
127: throw new IOException(
128: "Attempt to replace existing BlockAllocationTable");
129: }
130: _bat = bat;
131: }
132:
133: /* ********** END implementation of BlockList ********** */
134: } // end package-scope class BlockListImpl
|