Debian Codegraph @

src/main/org/h2/store/FileStoreInputStream.java

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