KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > blob > BLOBInputStream


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: BLOBInputStream.java,v 1.3 2002/12/29 11:15:55 per_nyfelt Exp $
8

9 package org.ozoneDB.blob;
10
11 import java.io.*;
12
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.3 $Date: 2002/12/29 11:15:55 $
23  */

24 public class BLOBInputStream extends InputStream implements Serializable {
25     
26     BLOBContainer container;
27     int index = 0;
28     
29     
30     public BLOBInputStream( BLOBContainer _container ) {
31         container = _container;
32     }
33     
34     
35     public int available() throws IOException {
36         try {
37             return container.available( index );
38         } catch (Exception JavaDoc e) {
39             throw new IOException( e.getMessage() );
40         }
41     }
42     
43     
44     public boolean markSupported() {
45         return false;
46     }
47     
48     
49     public int read( byte[] b ) throws IOException {
50         return read( b, 0, b.length );
51     }
52     
53     
54     public int read( byte[] b, int off, int len ) throws IOException {
55         try {
56             if (b == null) {
57                 skip( len );
58                 return len;
59             // but why ?!
60
}
61             
62             byte[] bb = container.read( index, len );
63             len = bb.length;
64             System.arraycopy( bb, 0, b, off, len );
65             index += len;
66             
67             return len == 0 ? -1 : len;
68         } catch (Exception JavaDoc e) {
69             throw new IOException( e.getMessage() );
70         }
71     }
72     
73     
74     public int read() throws IOException {
75         try {
76             byte[] bb = container.read( index, 1 );
77             index += bb.length;
78             return bb.length == 1 ? bb[0] : -1;
79         } catch (Exception JavaDoc e) {
80             throw new IOException( e.getMessage() );
81         }
82     }
83     
84     
85     public long skip( long n ) throws IOException {
86         int avail = available();
87         n = Math.min( n, avail );
88         index += n;
89         return n;
90     }
91     
92     
93     public void close() throws IOException {
94     }
95 }
96
Popular Tags