KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > storage > BlockListImpl


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.storage;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 /**
26  * A simple implementation of BlockList
27  *
28  * @author Marc Johnson (mjohnson at apache dot org
29  */

30
31 class BlockListImpl
32     implements BlockList
33 {
34     private ListManagedBlock[] _blocks;
35     private BlockAllocationTableReader _bat;
36
37     /**
38      * Constructor BlockListImpl
39      */

40
41     protected BlockListImpl()
42     {
43         _blocks = new ListManagedBlock[ 0 ];
44         _bat = null;
45     }
46
47     /**
48      * provide blocks to manage
49      *
50      * @param blocks blocks to be managed
51      */

52
53     protected void setBlocks(final ListManagedBlock [] blocks)
54     {
55         _blocks = blocks;
56     }
57
58     /* ********** START implementation of BlockList ********** */
59
60     /**
61      * remove the specified block from the list
62      *
63      * @param index the index of the specified block; if the index is
64      * out of range, that's ok
65      */

66
67     public void zap(final int index)
68     {
69         if ((index >= 0) && (index < _blocks.length))
70         {
71             _blocks[ index ] = null;
72         }
73     }
74
75     /**
76      * remove and return the specified block from the list
77      *
78      * @param index the index of the specified block
79      *
80      * @return the specified block
81      *
82      * @exception IOException if the index is out of range or has
83      * already been removed
84      */

85
86     public ListManagedBlock remove(final int index)
87         throws IOException
88     {
89         ListManagedBlock result = null;
90
91         try
92         {
93             result = _blocks[ index ];
94             if (result == null)
95             {
96                 throw new IOException("block[ " + index
97                                       + " ] already removed");
98             }
99             _blocks[ index ] = null;
100         }
101         catch (ArrayIndexOutOfBoundsException JavaDoc ignored)
102         {
103             throw new IOException("Cannot remove block[ " + index
104                                   + " ]; out of range");
105         }
106         return result;
107     }
108
109     /**
110      * get the blocks making up a particular stream in the list. The
111      * blocks are removed from the list.
112      *
113      * @param startBlock the index of the first block in the stream
114      *
115      * @return the stream as an array of correctly ordered blocks
116      *
117      * @exception IOException if blocks are missing
118      */

119
120     public ListManagedBlock [] fetchBlocks(final int startBlock)
121         throws IOException
122     {
123         if (_bat == null)
124         {
125             throw new IOException(
126                 "Improperly initialized list: no block allocation table provided");
127         }
128         return _bat.fetchBlocks(startBlock, this);
129     }
130
131     /**
132      * set the associated BlockAllocationTable
133      *
134      * @param bat the associated BlockAllocationTable
135      *
136      * @exception IOException
137      */

138
139     public void setBAT(final BlockAllocationTableReader bat)
140         throws IOException
141     {
142         if (_bat != null)
143         {
144             throw new IOException(
145                 "Attempt to replace existing BlockAllocationTable");
146         }
147         _bat = bat;
148     }
149
150     /* ********** END implementation of BlockList ********** */
151 } // end package-scope class BlockListImpl
152

153
Popular Tags