KickJava   Java API By Example, From Geeks To Geeks.

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


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.poifs.common.POIFSConstants;
22 import org.apache.poi.util.IOUtils;
23
24 import java.io.*;
25
26 /**
27  * A big block created from an InputStream, holding the raw data
28  *
29  * @author Marc Johnson (mjohnson at apache dot org
30  */

31
32 public class RawDataBlock
33     implements ListManagedBlock
34 {
35     private byte[] _data;
36     private boolean _eof;
37
38     /**
39      * Constructor RawDataBlock
40      *
41      * @param stream the InputStream from which the data will be read
42      *
43      * @exception IOException on I/O errors, and if an insufficient
44      * amount of data is read
45      */

46
47     public RawDataBlock(final InputStream stream)
48         throws IOException
49     {
50         _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ];
51         int count = IOUtils.readFully(stream, _data);
52
53         if (count == -1)
54         {
55             _eof = true;
56         }
57         else if (count != POIFSConstants.BIG_BLOCK_SIZE)
58         {
59             String JavaDoc type = " byte" + ((count == 1) ? ("")
60                                                   : ("s"));
61
62             throw new IOException("Unable to read entire block; " + count
63                                   + type + " read; expected "
64                                   + POIFSConstants.BIG_BLOCK_SIZE + " bytes");
65         }
66         else
67         {
68             _eof = false;
69         }
70     }
71
72     /**
73      * When we read the data, did we hit end of file?
74      *
75      * @return true if no data was read because we were at the end of
76      * the file, else false
77      *
78      * @exception IOException
79      */

80
81     public boolean eof()
82         throws IOException
83     {
84         return _eof;
85     }
86
87     /* ********** START implementation of ListManagedBlock ********** */
88
89     /**
90      * Get the data from the block
91      *
92      * @return the block's data as a byte array
93      *
94      * @exception IOException if there is no data
95      */

96
97     public byte [] getData()
98         throws IOException
99     {
100         if (eof())
101         {
102             throw new IOException("Cannot return empty data");
103         }
104         return _data;
105     }
106
107     /* ********** END implementation of ListManagedBlock ********** */
108 } // end public class RawDataBlock
109

110
Popular Tags