01: // You can redistribute this software and/or modify it under the terms of
02: // the Ozone Library License version 1 published by ozone-db.org.
03: //
04: // The original code and portions created by SMB are
05: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
06: //
07: // $Id: BLOBInputStream.java,v 1.1 2001/12/18 10:31:30 per_nyfelt Exp $
08:
09: package org.ozoneDB.blob;
10:
11: import org.ozoneDB.*;
12: import java.io.*;
13:
14: /**
15: * The output stream of ozone BLOBs. Every read operations will call
16: * the underlaying BLOBContainer. Therefore BLOBStreams should never be used
17: * without buffering/caching. This can be done by using the BLOBStream together
18: * with a BufferedStream.
19: *
20: *
21: * @author <a href="http://www.softwarebuero.de/">SMB</a>
22: * @version $Revision: 1.1 $Date: 2001/12/18 10:31:30 $
23: */
24: public class BLOBInputStream extends InputStream implements
25: Serializable {
26:
27: BLOBContainer container;
28: int index = 0;
29:
30: public BLOBInputStream(BLOBContainer _container) {
31: container = _container;
32: }
33:
34: public int available() throws IOException {
35: try {
36: return container.available(index);
37: } catch (Exception e) {
38: throw new IOException(e.getMessage());
39: }
40: }
41:
42: public boolean markSupported() {
43: return false;
44: }
45:
46: public int read(byte[] b) throws IOException {
47: return read(b, 0, b.length);
48: }
49:
50: public int read(byte[] b, int off, int len) throws IOException {
51: try {
52: if (b == null) {
53: skip(len);
54: return len;
55: // but why ?!
56: }
57:
58: byte[] bb = container.read(index, len);
59: len = bb.length;
60: System.arraycopy(bb, 0, b, off, len);
61: index += len;
62:
63: return len == 0 ? -1 : len;
64: } catch (Exception e) {
65: throw new IOException(e.getMessage());
66: }
67: }
68:
69: public int read() throws IOException {
70: try {
71: byte[] bb = container.read(index, 1);
72: index += bb.length;
73: return bb.length == 1 ? bb[0] : -1;
74: } catch (Exception e) {
75: throw new IOException(e.getMessage());
76: }
77: }
78:
79: public long skip(long n) throws IOException {
80: int avail = available();
81: n = Math.min(n, avail);
82: index += n;
83: return n;
84: }
85:
86: public void close() throws IOException {
87: }
88: }
|