File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\PageInputStream.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.EOFException;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import org.h2.message.DbException;
13 import org.h2.message.Trace;
14 import org.h2.util.BitField;
15
16 /**
17 * An input stream that reads from a page store.
18 */
19 public class PageInputStream extends InputStream {
20
21 private final PageStore store;
22 private final Trace trace;
23 private final int firstTrunkPage;
24 private final PageStreamTrunk.Iterator trunkIterator;
25 private int dataPage;
26 private PageStreamTrunk trunk;
27 private int trunkIndex;
28 private PageStreamData data;
29 private int dataPos;
30 private boolean endOfFile;
31 private int remaining;
32 private final byte[] buffer = { 0 };
33 private int logKey;
34
35 PageInputStream(PageStore store, int logKey, int firstTrunkPage, int dataPage) {
36 this.store = store;
37 this.trace = store.getTrace();
38 // minus one because we increment before comparing
39 this.logKey = logKey - 1;
40 this.firstTrunkPage = firstTrunkPage;
41 trunkIterator = new PageStreamTrunk.Iterator(store, firstTrunkPage);
42 this.dataPage = dataPage;
43 }
44
45 public int read() throws IOException {
46 int len = read(buffer);
47 return len < 0 ? -1 : (buffer[0] & 255);
48 }
49
50 public int read(byte[] b) throws IOException {
51 return read(b, 0, b.length);
52 }
53
54 public int read(byte[] b, int off, int len) throws IOException {
55 if (len == 0) {
56 return 0;
57 }
58 int read = 0;
59 while (len > 0) {
60 int r = readBlock(b, off, len);
61 if (r < 0) {
62 break;
63 }
64 read += r;
65 off += r;
66 len -= r;
67 }
68 return read == 0 ? -1 : read;
69 }
70
71 private int readBlock(byte[] buff, int off, int len) throws IOException {
72 try {
73 fillBuffer();
74 if (endOfFile) {
75 return -1;
76 }
77 int l = Math.min(remaining, len);
78 data.read(dataPos, buff, off, l);
79 remaining -= l;
80 dataPos += l;
81 return l;
82 } catch (DbException e) {
83 throw new EOFException();
84 }
85 }
86
87 private void fillBuffer() {
88 if (remaining > 0 || endOfFile) {
89 return;
90 }
91 int next;
92 while (true) {
93 if (trunk == null) {
94 trunk = trunkIterator.next();
95 trunkIndex = 0;
96 logKey++;
97 if (trunk == null || trunk.getLogKey() != logKey) {
98 endOfFile = true;
99 return;
100 }
101 }
102 if (trunk != null) {
103 next = trunk.getPageData(trunkIndex++);
104 if (next == -1) {
105 trunk = null;
106 } else if (dataPage == -1 || dataPage == next) {
107 break;
108 }
109 }
110 }
111 if (trace.isDebugEnabled()) {
112 trace.debug("pageIn.readPage " + next);
113 }
114 dataPage = -1;
115 data = null;
116 Page p = store.getPage(next);
117 if (p instanceof PageStreamData) {
118 data = (PageStreamData) p;
119 }
120 if (data == null || data.getLogKey() != logKey) {
121 endOfFile = true;
122 return;
123 }
124 dataPos = PageStreamData.getReadStart();
125 remaining = store.getPageSize() - dataPos;
126 }
127
128 /**
129 * Set all pages as 'allocated' in the page store.
130 *
131 * @return the bit set
132 */
133 BitField allocateAllPages() {
134 BitField pages = new BitField();
135 int key = logKey;
136 PageStreamTrunk.Iterator it = new PageStreamTrunk.Iterator(store, firstTrunkPage);
137 while (true) {
138 PageStreamTrunk t = it.next();
139 key++;
140 if (it.canDelete()) {
141 store.allocatePage(it.getCurrentPageId());
142 }
143 if (t == null || t.getLogKey() != key) {
144 break;
145 }
146 pages.set(t.getPos());
147 for (int i = 0;; i++) {
148 int n = t.getPageData(i);
149 if (n == -1) {
150 break;
151 }
152 pages.set(n);
153 store.allocatePage(n);
154 }
155 }
156 return pages;
157 }
158
159 int getDataPage() {
160 return data.getPos();
161 }
162
163 public void close() {
164 // nothing to do
165 }
166
167 }
168