1
2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
18
19
20 package org.apache.poi.poifs.storage;
21
22 import java.io;
23
24 import java.util;
25
26 /**
27 * A simple implementation of BlockList
28 *
29 * @author Marc Johnson (mjohnson at apache dot org
30 */
31
32 class BlockListImpl
33 implements BlockList
34 {
35 private ListManagedBlock[] _blocks;
36 private BlockAllocationTableReader _bat;
37
38 /**
39 * Constructor BlockListImpl
40 */
41
42 protected BlockListImpl()
43 {
44 _blocks = new ListManagedBlock[ 0 ];
45 _bat = null;
46 }
47
48 /**
49 * provide blocks to manage
50 *
51 * @param blocks blocks to be managed
52 */
53
54 protected void setBlocks(final ListManagedBlock [] blocks)
55 {
56 _blocks = blocks;
57 }
58
59 /* ********** START implementation of BlockList ********** */
60
61 /**
62 * remove the specified block from the list
63 *
64 * @param index the index of the specified block; if the index is
65 * out of range, that's ok
66 */
67
68 public void zap(final int index)
69 {
70 if ((index >= 0) && (index < _blocks.length))
71 {
72 _blocks[ index ] = null;
73 }
74 }
75
76 /**
77 * remove and return the specified block from the list
78 *
79 * @param index the index of the specified block
80 *
81 * @return the specified block
82 *
83 * @exception IOException if the index is out of range or has
84 * already been removed
85 */
86
87 public ListManagedBlock remove(final int index)
88 throws IOException
89 {
90 ListManagedBlock result = null;
91
92 try
93 {
94 result = _blocks[ index ];
95 if (result == null)
96 {
97 throw new IOException("block[ " + index
98 + " ] already removed");
99 }
100 _blocks[ index ] = null;
101 }
102 catch (ArrayIndexOutOfBoundsException ignored)
103 {
104 throw new IOException("Cannot remove block[ " + index
105 + " ]; out of range[ 0 - " +
106 (_blocks.length-1) + " ]");
107 }
108 return result;
109 }
110
111 /**
112 * get the blocks making up a particular stream in the list. The
113 * blocks are removed from the list.
114 *
115 * @param startBlock the index of the first block in the stream
116 *
117 * @return the stream as an array of correctly ordered blocks
118 *
119 * @exception IOException if blocks are missing
120 */
121
122 public ListManagedBlock [] fetchBlocks(final int startBlock)
123 throws IOException
124 {
125 if (_bat == null)
126 {
127 throw new IOException(
128 "Improperly initialized list: no block allocation table provided");
129 }
130 return _bat.fetchBlocks(startBlock, this);
131 }
132
133 /**
134 * set the associated BlockAllocationTable
135 *
136 * @param bat the associated BlockAllocationTable
137 *
138 * @exception IOException
139 */
140
141 public void setBAT(final BlockAllocationTableReader bat)
142 throws IOException
143 {
144 if (_bat != null)
145 {
146 throw new IOException(
147 "Attempt to replace existing BlockAllocationTable");
148 }
149 _bat = bat;
150 }
151
152 /* ********** END implementation of BlockList ********** */
153 } // end package-scope class BlockListImpl
154