01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: 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: package org.apache.poi.util;
19:
20: import java.io.InputStream;
21: import java.io.IOException;
22:
23: /**
24: * Implementation of a BlockingInputStream to provide data to
25: * RawDataBlock that expects data in 512 byte chunks. Useful to read
26: * data from slow (ie, non FileInputStream) sources, for example when
27: * reading an OLE2 Document over a network.
28: *
29: * Possible extentions: add a timeout. Curently a call to read(byte[]) on this
30: * class is blocking, so use at your own peril if your underlying stream blocks.
31: *
32: * @author Jens Gerhard
33: * @author aviks - documentation cleanups.
34: */
35: public class BlockingInputStream extends InputStream {
36: protected InputStream is;
37:
38: public BlockingInputStream(InputStream is) {
39: this .is = is;
40: }
41:
42: public int available() throws IOException {
43: return is.available();
44: }
45:
46: public void close() throws IOException {
47: is.close();
48: }
49:
50: public void mark(int readLimit) {
51: is.mark(readLimit);
52: }
53:
54: public boolean markSupported() {
55: return is.markSupported();
56: }
57:
58: public int read() throws IOException {
59: return is.read();
60: }
61:
62: /**
63: * We had to revert to byte per byte reading to keep
64: * with slow network connections on one hand, without
65: * missing the end-of-file.
66: * This is the only method that does its own thing in this class
67: * everything else is delegated to aggregated stream.
68: * THIS IS A BLOCKING BLOCK READ!!!
69: */
70: public int read(byte[] bf) throws IOException {
71:
72: int i = 0;
73: int b = 4611;
74: while (i < bf.length) {
75: b = is.read();
76: if (b == -1)
77: break;
78: bf[i++] = (byte) b;
79: }
80: if (i == 0 && b == -1)
81: return -1;
82: return i;
83: }
84:
85: public int read(byte[] bf, int s, int l) throws IOException {
86: return is.read(bf, s, l);
87: }
88:
89: public void reset() throws IOException {
90: is.reset();
91: }
92:
93: public long skip(long n) throws IOException {
94: return is.skip(n);
95: }
96: }
|