-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
LocalInputFile.java
192 lines (163 loc) · 6.22 KB
/
LocalInputFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (c) 2010-2021 Haifeng Li. All rights reserved.
*
* Smile is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Smile is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Smile. If not, see <https://www.gnu.org/licenses/>.
*/
package smile.io;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import org.apache.parquet.io.InputFile;
import org.apache.parquet.io.SeekableInputStream;
/**
* Parquet InputFile with a local java.nio.Path.
* Adapted from <a href="https://github.com/tideworks/arvo2parquet">arvo2parquet</a>.
*
* @author Haifeng Li
*/
class LocalInputFile implements InputFile {
/** Local file object. */
private final RandomAccessFile input;
/**
* Constructor.
* @param path the input file path.
* @throws FileNotFoundException when file cannot be found.
*/
public LocalInputFile(Path path) throws FileNotFoundException {
input = new RandomAccessFile(path.toFile(), "r");
}
@Override
public long getLength() throws IOException {
return input.length();
}
@Override
public SeekableInputStream newStream() {
return new SeekableInputStream() {
private final byte[] page = new byte[8192];
private long markPos = 0;
@Override
public int read() throws IOException {
return input.read();
}
@Override
public int read(byte[] b) throws IOException {
return input.read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return input.read(b, off, len);
}
@Override
public long skip(long n) throws IOException {
final long savPos = input.getFilePointer();
final long amtLeft = input.length() - savPos;
n = Math.min(n, amtLeft);
final long newPos = savPos + n;
input.seek(newPos);
final long curPos = input.getFilePointer();
return curPos - savPos;
}
@Override
public int available() {
return 0;
}
@Override
public void close() throws IOException {
input.close();
}
@SuppressWarnings({"unchecked", "unused", "UnusedReturnValue"})
private <T extends Throwable, R> R uncheckedExceptionThrow(Throwable t) throws T {
throw (T) t;
}
@Override
public synchronized void mark(int readlimit) {
try {
markPos = input.getFilePointer();
} catch (IOException e) {
uncheckedExceptionThrow(e);
}
}
@Override
public synchronized void reset() throws IOException {
input.seek(markPos);
}
@Override
public boolean markSupported() {
return true;
}
@Override
public long getPos() throws IOException {
return input.getFilePointer();
}
@Override
public void seek(long l) throws IOException {
input.seek(l);
}
@Override
public void readFully(byte[] bytes) throws IOException {
input.readFully(bytes);
}
@Override
public void readFully(byte[] bytes, int i, int i1) throws IOException {
input.readFully(bytes, i, i1);
}
@Override
public int read(ByteBuffer byteBuffer) throws IOException {
return readDirectBuffer(byteBuffer, page, input::read);
}
@Override
public void readFully(ByteBuffer byteBuffer) throws IOException {
readFullyDirectBuffer(byteBuffer, page, input::read);
}
};
}
private interface ByteBufferReader {
int read(byte[] b, int off, int len) throws IOException;
}
private int readDirectBuffer(ByteBuffer byteBuffer, byte[] page, ByteBufferReader reader) throws IOException {
// copy all the bytes that return immediately, stopping at the first
// read that doesn't return a full buffer.
int nextReadLength = Math.min(byteBuffer.remaining(), page.length);
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = reader.read(page, 0, nextReadLength)) == page.length) {
byteBuffer.put(page);
totalBytesRead += bytesRead;
nextReadLength = Math.min(byteBuffer.remaining(), page.length);
}
if (bytesRead < 0) {
// return -1 if nothing was read
return totalBytesRead == 0 ? -1 : totalBytesRead;
} else {
// copy the last partial buffer
byteBuffer.put(page, 0, bytesRead);
totalBytesRead += bytesRead;
return totalBytesRead;
}
}
private static void readFullyDirectBuffer(ByteBuffer byteBuffer, byte[] page, ByteBufferReader reader) throws IOException {
int nextReadLength = Math.min(byteBuffer.remaining(), page.length);
int bytesRead = 0;
while (nextReadLength > 0 && (bytesRead = reader.read(page, 0, nextReadLength)) >= 0) {
byteBuffer.put(page, 0, bytesRead);
nextReadLength = Math.min(byteBuffer.remaining(), page.length);
}
if (bytesRead < 0 && byteBuffer.remaining() > 0) {
throw new EOFException("Reached the end of stream with " + byteBuffer.remaining() + " bytes left to read");
}
}
}