50113
|
1 |
/*
|
57360
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
50113
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jfr.internal.consumer;
|
|
27 |
|
|
28 |
import java.io.DataInput;
|
|
29 |
import java.io.EOFException;
|
|
30 |
import java.io.File;
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.RandomAccessFile;
|
|
33 |
|
|
34 |
public final class RecordingInput implements DataInput, AutoCloseable {
|
|
35 |
|
|
36 |
|
|
37 |
private final static int DEFAULT_BLOCK_SIZE = 16 * 1024 * 1024;
|
|
38 |
|
|
39 |
private static final class Block {
|
|
40 |
private byte[] bytes = new byte[0];
|
|
41 |
private long blockPosition;
|
57360
|
42 |
private int size;
|
50113
|
43 |
boolean contains(long position) {
|
57360
|
44 |
return position >= blockPosition && position < blockPosition + size;
|
50113
|
45 |
}
|
|
46 |
|
|
47 |
public void read(RandomAccessFile file, int amount) throws IOException {
|
|
48 |
blockPosition = file.getFilePointer();
|
|
49 |
// reuse byte array, if possible
|
57360
|
50 |
if (amount > bytes.length) {
|
50113
|
51 |
bytes = new byte[amount];
|
|
52 |
}
|
57360
|
53 |
this.size = amount;
|
|
54 |
file.readFully(bytes, 0 , amount);
|
50113
|
55 |
}
|
|
56 |
|
|
57 |
public byte get(long position) {
|
|
58 |
return bytes[(int) (position - blockPosition)];
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
private final RandomAccessFile file;
|
57360
|
63 |
private final String filename;
|
50113
|
64 |
private Block currentBlock = new Block();
|
|
65 |
private Block previousBlock = new Block();
|
|
66 |
private long position;
|
|
67 |
private final int blockSize;
|
57360
|
68 |
private long size = -1; // Fail fast if setSize(...) has not been called before parsing
|
50113
|
69 |
|
57360
|
70 |
public RecordingInput(File f, int blockSize) throws IOException {
|
50113
|
71 |
this.blockSize = blockSize;
|
57360
|
72 |
this.filename = f.getAbsolutePath().toString();
|
50113
|
73 |
this.file = new RandomAccessFile(f, "r");
|
57360
|
74 |
if (f.length() < 8) {
|
|
75 |
throw new IOException("Not a valid Flight Recorder file. File length is only " + f.length() + " bytes.");
|
50113
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
public RecordingInput(File f) throws IOException {
|
|
80 |
this(f, DEFAULT_BLOCK_SIZE);
|
|
81 |
}
|
57360
|
82 |
public void positionPhysical(long position) throws IOException {
|
|
83 |
file.seek(position);
|
|
84 |
}
|
|
85 |
public final byte readPhysicalByte() throws IOException {
|
|
86 |
return file.readByte();
|
|
87 |
}
|
|
88 |
public long readPhysicalLong() throws IOException {
|
|
89 |
return file.readLong();
|
|
90 |
}
|
50113
|
91 |
|
|
92 |
@Override
|
|
93 |
public final byte readByte() throws IOException {
|
|
94 |
if (!currentBlock.contains(position)) {
|
|
95 |
position(position);
|
|
96 |
}
|
|
97 |
return currentBlock.get(position++);
|
|
98 |
}
|
|
99 |
|
|
100 |
@Override
|
|
101 |
public final void readFully(byte[] dest, int offset, int length) throws IOException {
|
|
102 |
// TODO: Optimize, use Arrays.copy if all bytes are in current block
|
|
103 |
// array
|
|
104 |
for (int i = 0; i < length; i++) {
|
|
105 |
dest[i + offset] = readByte();
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
@Override
|
|
110 |
public final void readFully(byte[] dst) throws IOException {
|
|
111 |
readFully(dst, 0, dst.length);
|
|
112 |
}
|
|
113 |
|
|
114 |
public final short readRawShort() throws IOException {
|
|
115 |
// copied from java.io.Bits
|
|
116 |
byte b0 = readByte();
|
|
117 |
byte b1 = readByte();
|
|
118 |
return (short) ((b1 & 0xFF) + (b0 << 8));
|
|
119 |
}
|
|
120 |
|
|
121 |
@Override
|
|
122 |
public final double readDouble() throws IOException {
|
|
123 |
// copied from java.io.Bits
|
|
124 |
return Double.longBitsToDouble(readRawLong());
|
|
125 |
}
|
|
126 |
|
|
127 |
@Override
|
|
128 |
public final float readFloat() throws IOException {
|
|
129 |
// copied from java.io.Bits
|
|
130 |
return Float.intBitsToFloat(readRawInt());
|
|
131 |
}
|
|
132 |
|
|
133 |
public final int readRawInt() throws IOException {
|
|
134 |
// copied from java.io.Bits
|
|
135 |
byte b0 = readByte();
|
|
136 |
byte b1 = readByte();
|
|
137 |
byte b2 = readByte();
|
|
138 |
byte b3 = readByte();
|
|
139 |
return ((b3 & 0xFF)) + ((b2 & 0xFF) << 8) + ((b1 & 0xFF) << 16) + ((b0) << 24);
|
|
140 |
}
|
|
141 |
|
|
142 |
public final long readRawLong() throws IOException {
|
|
143 |
// copied from java.io.Bits
|
|
144 |
byte b0 = readByte();
|
|
145 |
byte b1 = readByte();
|
|
146 |
byte b2 = readByte();
|
|
147 |
byte b3 = readByte();
|
|
148 |
byte b4 = readByte();
|
|
149 |
byte b5 = readByte();
|
|
150 |
byte b6 = readByte();
|
|
151 |
byte b7 = readByte();
|
|
152 |
return ((b7 & 0xFFL)) + ((b6 & 0xFFL) << 8) + ((b5 & 0xFFL) << 16) + ((b4 & 0xFFL) << 24) + ((b3 & 0xFFL) << 32) + ((b2 & 0xFFL) << 40) + ((b1 & 0xFFL) << 48) + (((long) b0) << 56);
|
|
153 |
}
|
|
154 |
|
57360
|
155 |
public final long position() {
|
50113
|
156 |
return position;
|
|
157 |
}
|
|
158 |
|
|
159 |
public final void position(long newPosition) throws IOException {
|
|
160 |
if (!currentBlock.contains(newPosition)) {
|
|
161 |
if (!previousBlock.contains(newPosition)) {
|
57360
|
162 |
if (newPosition > size) {
|
|
163 |
throw new EOFException("Trying to read at " + newPosition + ", but file is only " + size + " bytes.");
|
50113
|
164 |
}
|
|
165 |
long blockStart = trimToFileSize(calculateBlockStart(newPosition));
|
|
166 |
file.seek(blockStart);
|
|
167 |
// trim amount to file size
|
57360
|
168 |
long amount = Math.min(size - blockStart, blockSize);
|
50113
|
169 |
previousBlock.read(file, (int) amount);
|
|
170 |
}
|
|
171 |
// swap previous and current
|
|
172 |
Block tmp = currentBlock;
|
|
173 |
currentBlock = previousBlock;
|
|
174 |
previousBlock = tmp;
|
|
175 |
}
|
|
176 |
position = newPosition;
|
|
177 |
}
|
|
178 |
|
|
179 |
private final long trimToFileSize(long position) throws IOException {
|
|
180 |
return Math.min(size(), Math.max(0, position));
|
|
181 |
}
|
|
182 |
|
|
183 |
private final long calculateBlockStart(long newPosition) {
|
|
184 |
// align to end of current block
|
|
185 |
if (currentBlock.contains(newPosition - blockSize)) {
|
|
186 |
return currentBlock.blockPosition + currentBlock.bytes.length;
|
|
187 |
}
|
|
188 |
// align before current block
|
|
189 |
if (currentBlock.contains(newPosition + blockSize)) {
|
|
190 |
return currentBlock.blockPosition - blockSize;
|
|
191 |
}
|
|
192 |
// not near current block, pick middle
|
|
193 |
return newPosition - blockSize / 2;
|
|
194 |
}
|
|
195 |
|
57360
|
196 |
public final long size() {
|
50113
|
197 |
return size;
|
|
198 |
}
|
|
199 |
|
|
200 |
public final void close() throws IOException {
|
|
201 |
file.close();
|
|
202 |
}
|
|
203 |
|
|
204 |
@Override
|
|
205 |
public final int skipBytes(int n) throws IOException {
|
|
206 |
long position = position();
|
|
207 |
position(position + n);
|
|
208 |
return (int) (position() - position);
|
|
209 |
}
|
|
210 |
|
|
211 |
@Override
|
|
212 |
public final boolean readBoolean() throws IOException {
|
|
213 |
return readByte() != 0;
|
|
214 |
}
|
|
215 |
|
|
216 |
@Override
|
|
217 |
public int readUnsignedByte() throws IOException {
|
|
218 |
return readByte() & 0x00FF;
|
|
219 |
}
|
|
220 |
|
|
221 |
@Override
|
|
222 |
public int readUnsignedShort() throws IOException {
|
|
223 |
return readShort() & 0xFFFF;
|
|
224 |
}
|
|
225 |
|
|
226 |
@Override
|
|
227 |
public final String readLine() throws IOException {
|
|
228 |
throw new UnsupportedOperationException();
|
|
229 |
}
|
|
230 |
|
|
231 |
// NOTE, this method should really be called readString
|
|
232 |
// but can't be renamed without making RecordingInput a
|
|
233 |
// public class.
|
|
234 |
//
|
|
235 |
// This method DOES Not read as expected (s2 + utf8 encoded character)
|
|
236 |
// instead it read:
|
|
237 |
// byte encoding
|
|
238 |
// int size
|
|
239 |
// data (byte or char)
|
|
240 |
//
|
|
241 |
// where encoding
|
|
242 |
//
|
|
243 |
// 0, means null
|
|
244 |
// 1, means UTF8 encoded byte array
|
|
245 |
// 2, means char array
|
|
246 |
// 3, means latin-1 (ISO-8859-1) encoded byte array
|
|
247 |
// 4, means ""
|
|
248 |
@Override
|
|
249 |
public String readUTF() throws IOException {
|
57360
|
250 |
throw new UnsupportedOperationException("Use StringParser");
|
50113
|
251 |
}
|
|
252 |
|
|
253 |
@Override
|
|
254 |
public char readChar() throws IOException {
|
|
255 |
return (char) readLong();
|
|
256 |
}
|
|
257 |
|
|
258 |
@Override
|
|
259 |
public short readShort() throws IOException {
|
|
260 |
return (short) readLong();
|
|
261 |
}
|
|
262 |
|
|
263 |
@Override
|
|
264 |
public int readInt() throws IOException {
|
|
265 |
return (int) readLong();
|
|
266 |
}
|
|
267 |
|
|
268 |
@Override
|
|
269 |
public long readLong() throws IOException {
|
|
270 |
// can be optimized by branching checks, but will do for now
|
|
271 |
byte b0 = readByte();
|
|
272 |
long ret = (b0 & 0x7FL);
|
|
273 |
if (b0 >= 0) {
|
|
274 |
return ret;
|
|
275 |
}
|
|
276 |
int b1 = readByte();
|
|
277 |
ret += (b1 & 0x7FL) << 7;
|
|
278 |
if (b1 >= 0) {
|
|
279 |
return ret;
|
|
280 |
}
|
|
281 |
int b2 = readByte();
|
|
282 |
ret += (b2 & 0x7FL) << 14;
|
|
283 |
if (b2 >= 0) {
|
|
284 |
return ret;
|
|
285 |
}
|
|
286 |
int b3 = readByte();
|
|
287 |
ret += (b3 & 0x7FL) << 21;
|
|
288 |
if (b3 >= 0) {
|
|
289 |
return ret;
|
|
290 |
}
|
|
291 |
int b4 = readByte();
|
|
292 |
ret += (b4 & 0x7FL) << 28;
|
|
293 |
if (b4 >= 0) {
|
|
294 |
return ret;
|
|
295 |
}
|
|
296 |
int b5 = readByte();
|
|
297 |
ret += (b5 & 0x7FL) << 35;
|
|
298 |
if (b5 >= 0) {
|
|
299 |
return ret;
|
|
300 |
}
|
|
301 |
int b6 = readByte();
|
|
302 |
ret += (b6 & 0x7FL) << 42;
|
|
303 |
if (b6 >= 0) {
|
|
304 |
return ret;
|
|
305 |
}
|
|
306 |
int b7 = readByte();
|
|
307 |
ret += (b7 & 0x7FL) << 49;
|
|
308 |
if (b7 >= 0) {
|
|
309 |
return ret;
|
|
310 |
}
|
|
311 |
int b8 = readByte(); // read last byte raw
|
|
312 |
return ret + (((long) (b8 & 0XFF)) << 56);
|
|
313 |
}
|
57360
|
314 |
|
|
315 |
public void setValidSize(long size) {
|
|
316 |
if (size > this.size) {
|
|
317 |
this.size = size;
|
|
318 |
}
|
|
319 |
}
|
|
320 |
|
|
321 |
public long getFileSize() throws IOException {
|
|
322 |
return file.length();
|
|
323 |
}
|
|
324 |
|
|
325 |
public String getFilename() {
|
|
326 |
return filename;
|
|
327 |
}
|
|
328 |
|
50113
|
329 |
}
|