KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > store > FileStoreInputStream


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.store;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 import org.h2.engine.Constants;
12 import org.h2.message.Message;
13 import org.h2.tools.CompressTool;
14
15 public class FileStoreInputStream extends InputStream JavaDoc {
16
17     private FileStore store;
18     private DataPage page;
19     private int remaining;
20     private CompressTool compress;
21     
22     public FileStoreInputStream(FileStore store, DataHandler handler, boolean compression) throws SQLException JavaDoc {
23         this.store = store;
24         if(compression) {
25             compress = CompressTool.getInstance();
26         }
27         page = DataPage.create(handler, Constants.FILE_BLOCK_SIZE);
28         try {
29             if(store.length() <= FileStore.HEADER_LENGTH) {
30                 close();
31             } else {
32                 fillBuffer();
33             }
34         } catch(IOException JavaDoc e) {
35             throw Message.convert(e);
36         }
37     }
38
39     public int available() {
40         return remaining <= 0 ? 0 : remaining;
41     }
42     
43     public int read(byte[] buff) throws IOException JavaDoc {
44         return read(buff, 0, buff.length);
45     }
46     
47     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
48         if(len == 0) {
49             return 0;
50         }
51         int read = 0;
52         while(len > 0) {
53             int r = readBlock(b, off, len);
54             if(r < 0) {
55                 break;
56             }
57             read += r;
58             off += r;
59             len -= r;
60         }
61         return read == 0 ? -1 : read;
62     }
63     
64     public int readBlock(byte[] buff, int off, int len) throws IOException JavaDoc {
65         fillBuffer();
66         if(store == null) {
67             return -1;
68         }
69         int l = Math.min(remaining, len);
70         page.read(buff, off, l);
71         remaining -= l;
72         return l;
73     }
74     
75     private void fillBuffer() throws IOException JavaDoc {
76         if(remaining > 0 || store==null) {
77             return;
78         }
79         page.reset();
80         try {
81             if(store.length() == store.getFilePointer()) {
82                 close();
83                 return;
84             }
85             store.readFully(page.getBytes(), 0, Constants.FILE_BLOCK_SIZE);
86         } catch(SQLException JavaDoc e) {
87             throw Message.convertToIOException(e);
88         }
89         page.reset();
90         remaining = page.readInt();
91         if(remaining<0) {
92             close();
93             return;
94         }
95         page.checkCapacity(remaining);
96         // get the length to read
97
if(compress != null) {
98             page.checkCapacity(page.getIntLen());
99             page.readInt();
100         }
101         page.setPos(page.length() + remaining);
102         page.fillAligned();
103         int len = page.length() - Constants.FILE_BLOCK_SIZE;
104         page.reset();
105         page.readInt();
106         try {
107             store.readFully(page.getBytes(), Constants.FILE_BLOCK_SIZE, len);
108             page.reset();
109             page.readInt();
110             if(compress != null) {
111                 int uncompressed = page.readInt();
112                 byte[] buff = new byte[remaining];
113                 page.read(buff, 0, remaining);
114                 page.reset();
115                 page.checkCapacity(uncompressed);
116                 compress.expand(buff, page.getBytes(), 0);
117                 remaining = uncompressed;
118             }
119         } catch(SQLException JavaDoc e) {
120             throw Message.convertToIOException(e);
121         }
122     }
123     
124     public void close() throws IOException JavaDoc {
125         if(store != null) {
126             try {
127                 store.close();
128             } finally {
129                 store = null;
130             }
131         }
132     }
133     
134     protected void finalize() {
135         if (!Constants.RUN_FINALIZERS) {
136             return;
137         }
138         try {
139             close();
140         } catch(IOException JavaDoc e) {
141             // ignore
142
}
143     }
144     
145     public int read() throws IOException JavaDoc {
146         fillBuffer();
147         if(store == null) {
148             return -1;
149         }
150         int i = page.readByte() & 0xff;
151         remaining--;
152         return i;
153     }
154
155 }
156
Popular Tags