KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.poi.util.LittleEndian;
22 import org.apache.poi.util.LittleEndianConsts;
23
24 import java.io.*;
25
26 import java.util.*;
27
28 /**
29  * Class LocalRawDataBlockList
30  *
31  * @author Marc Johnson(mjohnson at apache dot org)
32  */

33
34 public class LocalRawDataBlockList
35     extends RawDataBlockList
36 {
37     private List _list;
38     private RawDataBlock[] _array;
39
40     /**
41      * Constructor LocalRawDataBlockList
42      *
43      * @exception IOException
44      */

45
46     public LocalRawDataBlockList()
47         throws IOException
48     {
49         super(new ByteArrayInputStream(new byte[ 0 ]));
50         _list = new ArrayList();
51         _array = null;
52     }
53
54     /**
55      * create and a new XBAT block
56      *
57      * @param start index of first BAT block
58      * @param end index of last BAT block
59      * @param chain index of next XBAT block
60      *
61      * @exception IOException
62      */

63
64     public void createNewXBATBlock(final int start, final int end,
65                                    final int chain)
66         throws IOException
67     {
68         byte[] data = new byte[ 512 ];
69         int offset = 0;
70
71         for (int k = start; k <= end; k++)
72         {
73             LittleEndian.putInt(data, offset, k);
74             offset += LittleEndianConsts.INT_SIZE;
75         }
76         while (offset != 508)
77         {
78             LittleEndian.putInt(data, offset, -1);
79             offset += LittleEndianConsts.INT_SIZE;
80         }
81         LittleEndian.putInt(data, offset, chain);
82         add(new RawDataBlock(new ByteArrayInputStream(data)));
83     }
84
85     /**
86      * create a BAT block and add it to the list
87      *
88      * @param start_index initial index for the block list
89      *
90      * @exception IOException
91      */

92
93     public void createNewBATBlock(final int start_index)
94         throws IOException
95     {
96         byte[] data = new byte[ 512 ];
97         int offset = 0;
98
99         for (int j = 0; j < 128; j++)
100         {
101             int index = start_index + j;
102
103             if (index % 256 == 0)
104             {
105                 LittleEndian.putInt(data, offset, -1);
106             }
107             else if (index % 256 == 255)
108             {
109                 LittleEndian.putInt(data, offset, -2);
110             }
111             else
112             {
113                 LittleEndian.putInt(data, offset, index + 1);
114             }
115             offset += LittleEndianConsts.INT_SIZE;
116         }
117         add(new RawDataBlock(new ByteArrayInputStream(data)));
118     }
119
120     /**
121      * fill the list with dummy blocks
122      *
123      * @param count of blocks
124      *
125      * @exception IOException
126      */

127
128     public void fill(final int count)
129         throws IOException
130     {
131         int limit = 128 * count;
132
133         for (int j = _list.size(); j < limit; j++)
134         {
135             add(new RawDataBlock(new ByteArrayInputStream(new byte[ 0 ])));
136         }
137     }
138
139     /**
140      * add a new block
141      *
142      * @param block new block to add
143      */

144
145     public void add(RawDataBlock block)
146     {
147         _list.add(block);
148     }
149
150     /**
151      * override of remove method
152      *
153      * @param index of block to be removed
154      *
155      * @return desired block
156      *
157      * @exception IOException
158      */

159
160     public ListManagedBlock remove(final int index)
161         throws IOException
162     {
163         ensureArrayExists();
164         RawDataBlock rvalue = null;
165
166         try
167         {
168             rvalue = _array[ index ];
169             if (rvalue == null)
170             {
171                 throw new IOException("index " + index + " is null");
172             }
173             _array[ index ] = null;
174         }
175         catch (ArrayIndexOutOfBoundsException JavaDoc ignored)
176         {
177             throw new IOException("Cannot remove block[ " + index
178                                   + " ]; out of range");
179         }
180         return rvalue;
181     }
182
183     /**
184      * remove the specified block from the list
185      *
186      * @param index the index of the specified block; if the index is
187      * out of range, that's ok
188      */

189
190     public void zap(final int index)
191     {
192         ensureArrayExists();
193         if ((index >= 0) && (index < _array.length))
194         {
195             _array[ index ] = null;
196         }
197     }
198
199     private void ensureArrayExists()
200     {
201         if (_array == null)
202         {
203             _array = ( RawDataBlock [] ) _list.toArray(new RawDataBlock[ 0 ]);
204         }
205     }
206 }
207
Popular Tags