File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\FileStoreInputStream.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.store;
8
9 import java.io.IOException;
10 import java.io.InputStream;
11 import org.h2.engine.Constants;
12 import org.h2.message.DbException;
13 import org.h2.tools.CompressTool;
14 import org.h2.util.Utils;
15
16 /**
17 * An input stream that is backed by a file store.
18 */
19 public class FileStoreInputStream extends InputStream {
20
21 private FileStore store;
22 private final Data page;
23 private int remainingInBuffer;
24 private final CompressTool compress;
25 private boolean endOfFile;
26 private final boolean alwaysClose;
27
28 public FileStoreInputStream(FileStore store, DataHandler handler, boolean compression, boolean alwaysClose) {
29 this.store = store;
30 this.alwaysClose = alwaysClose;
31 if (compression) {
32 compress = CompressTool.getInstance();
33 } else {
34 compress = null;
35 }
36 page = Data.create(handler, Constants.FILE_BLOCK_SIZE);
37 try {
38 if (store.length() <= FileStore.HEADER_LENGTH) {
39 close();
40 } else {
41 fillBuffer();
42 }
43 } catch (IOException e) {
44 throw DbException.convertIOException(e, store.name);
45 }
46 }
47
48 public int available() {
49 return remainingInBuffer <= 0 ? 0 : remainingInBuffer;
50 }
51
52 public int read(byte[] buff) throws IOException {
53 return read(buff, 0, buff.length);
54 }
55
56 public int read(byte[] b, int off, int len) throws IOException {
57 if (len == 0) {
58 return 0;
59 }
60 int read = 0;
61 while (len > 0) {
62 int r = readBlock(b, off, len);
63 if (r < 0) {
64 break;
65 }
66 read += r;
67 off += r;
68 len -= r;
69 }
70 return read == 0 ? -1 : read;
71 }
72
73 private int readBlock(byte[] buff, int off, int len) throws IOException {
74 fillBuffer();
75 if (endOfFile) {
76 return -1;
77 }
78 int l = Math.min(remainingInBuffer, len);
79 page.read(buff, off, l);
80 remainingInBuffer -= l;
81 return l;
82 }
83
84 private void fillBuffer() throws IOException {
85 if (remainingInBuffer > 0 || endOfFile) {
86 return;
87 }
88 page.reset();
89 store.openFile();
90 if (store.length() == store.getFilePointer()) {
91 close();
92 return;
93 }
94 store.readFully(page.getBytes(), 0, Constants.FILE_BLOCK_SIZE);
95 page.reset();
96 remainingInBuffer = page.readInt();
97 if (remainingInBuffer < 0) {
98 close();
99 return;
100 }
101 page.checkCapacity(remainingInBuffer);
102 // get the length to read
103 if (compress != null) {
104 page.checkCapacity(Data.LENGTH_INT);
105 page.readInt();
106 }
107 page.setPos(page.length() + remainingInBuffer);
108 page.fillAligned();
109 int len = page.length() - Constants.FILE_BLOCK_SIZE;
110 page.reset();
111 page.readInt();
112 store.readFully(page.getBytes(), Constants.FILE_BLOCK_SIZE, len);
113 page.reset();
114 page.readInt();
115 if (compress != null) {
116 int uncompressed = page.readInt();
117 byte[] buff = Utils.newBytes(remainingInBuffer);
118 page.read(buff, 0, remainingInBuffer);
119 page.reset();
120 page.checkCapacity(uncompressed);
121 CompressTool.expand(buff, page.getBytes(), 0);
122 remainingInBuffer = uncompressed;
123 }
124 if (alwaysClose) {
125 store.closeFile();
126 }
127 }
128
129 public void close() {
130 if (store != null) {
131 try {
132 store.close();
133 endOfFile = true;
134 } finally {
135 store = null;
136 }
137 }
138 }
139
140 protected void finalize() {
141 close();
142 }
143
144 public int read() throws IOException {
145 fillBuffer();
146 if (endOfFile) {
147 return -1;
148 }
149 int i = page.readByte() & 0xff;
150 remainingInBuffer--;
151 return i;
152 }
153
154 }
155