01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.store.fs;
07:
08: import java.io.IOException;
09: import java.io.InputStream;
10:
11: /**
12: * Allows to read from a file object like an input stream.
13: */
14: public class FileObjectInputStream extends InputStream {
15:
16: private FileObject file;
17: private byte[] buffer = new byte[1];
18:
19: FileObjectInputStream(FileObject file) {
20: this .file = file;
21: }
22:
23: public int read() throws IOException {
24: if (file.getFilePointer() >= file.length()) {
25: return -1;
26: }
27: file.readFully(buffer, 0, 1);
28: return buffer[0] & 0xff;
29: }
30:
31: public int read(byte[] b) throws IOException {
32: return read(b, 0, b.length);
33: }
34:
35: public int read(byte[] b, int off, int len) throws IOException {
36: if (file.getFilePointer() + len < file.length()) {
37: file.readFully(b, off, len);
38: return len;
39: } else {
40: return super .read(b, off, len);
41: }
42: }
43:
44: public void close() throws IOException {
45: file.close();
46: }
47:
48: }
|