-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TestBlockInStream.java
100 lines (85 loc) · 2.61 KB
/
TestBlockInStream.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
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.client.block.stream;
import alluxio.wire.WorkerNetAddress;
import java.io.IOException;
/**
* A {@link BlockInStream} which reads from the given byte array. The stream is able to track how
* much bytes that have been read from the extended BlockInStream.
*/
public class TestBlockInStream extends BlockInStream {
/** A field tracks how much bytes read. */
private int mBytesRead;
private boolean mClosed;
public TestBlockInStream(byte[] data, long id, long length, boolean shortCircuit,
BlockInStreamSource source) {
super(new Factory(data, shortCircuit), new WorkerNetAddress(), source, id, length);
mBytesRead = 0;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int bytesRead = super.read(b, off, len);
if (bytesRead <= 0) {
return bytesRead;
}
mBytesRead += bytesRead;
return bytesRead;
}
@Override
public int positionedRead(long pos, byte[] b, int off, int len) throws IOException {
int bytesRead = super.positionedRead(pos, b, off, len);
if (bytesRead <= 0) {
return bytesRead;
}
mBytesRead += bytesRead;
return bytesRead;
}
public boolean isClosed() {
return mClosed;
}
@Override
public void close() throws IOException {
mClosed = true;
super.close();
}
/**
* @return how many bytes been read
*/
public int getBytesRead() {
return mBytesRead;
}
/**
* Factory class to create {@link TestPacketReader}s.
*/
public static class Factory implements PacketReader.Factory {
private final byte[] mData;
private final boolean mShortCircuit;
/**
* Creates an instance of {@link LocalFilePacketReader.Factory}.
*
* @param data the data to serve
*/
public Factory(byte[] data, boolean shortCircuit) {
mData = data;
mShortCircuit = shortCircuit;
}
@Override
public PacketReader create(long offset, long len) {
return new TestPacketReader(mData, offset, len);
}
@Override
public boolean isShortCircuit() {
return mShortCircuit;
}
@Override
public void close() {}
}
}