KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > BlockingInputStream


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.util;
20
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * Implementation of a BlockingInputStream to provide data to
26  * RawDataBlock that expects data in 512 byte chunks. Useful to read
27  * data from slow (ie, non FileInputStream) sources, for example when
28  * reading an OLE2 Document over a network.
29  *
30  * Possible extentions: add a timeout. Curently a call to read(byte[]) on this
31  * class is blocking, so use at your own peril if your underlying stream blocks.
32  *
33  * @author Jens Gerhard
34  * @author aviks - documentation cleanups.
35  */

36 public class BlockingInputStream
37       extends InputStream JavaDoc
38 {
39       protected InputStream JavaDoc is;
40
41       public BlockingInputStream(InputStream JavaDoc is)
42       {
43           this.is = is;
44       }
45
46       public int available()
47         throws IOException JavaDoc
48       {
49           return is.available();
50       }
51
52       public void close()
53         throws IOException JavaDoc
54       {
55           is.close();
56       }
57
58       public void mark(int readLimit)
59       {
60           is.mark(readLimit);
61       }
62
63       public boolean markSupported()
64       {
65           return is.markSupported();
66       }
67
68       public int read()
69         throws IOException JavaDoc
70       {
71           return is.read();
72       }
73       
74       /**
75        * We had to revert to byte per byte reading to keep
76        * with slow network connections on one hand, without
77        * missing the end-of-file.
78        * This is the only method that does its own thing in this class
79        * everything else is delegated to aggregated stream.
80        * THIS IS A BLOCKING BLOCK READ!!!
81        */

82       public int read(byte[] bf)
83         throws IOException JavaDoc
84       {
85           
86           int i = 0;
87           int b = 4611;
88           while ( i < bf.length )
89           {
90               b = is.read();
91               if ( b == -1 )
92                   break;
93               bf[i++] = (byte) b;
94           }
95           if ( i == 0 && b == -1 )
96               return -1;
97           return i;
98       }
99
100       public int read(byte[] bf, int s, int l)
101         throws IOException JavaDoc
102       {
103           return is.read(bf, s, l);
104       }
105
106       public void reset()
107         throws IOException JavaDoc
108       {
109           is.reset();
110       }
111
112       public long skip(long n)
113         throws IOException JavaDoc
114       {
115           return is.skip(n);
116       }
117 }
118
119
Popular Tags