-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathByteBufTest.java
More file actions
41 lines (33 loc) · 1.46 KB
/
Copy pathByteBufTest.java
File metadata and controls
41 lines (33 loc) · 1.46 KB
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
package testSuite.classes.bytebuf_error;
// SO 48582520 — UnsupportedOperationException at ByteBuffer.array()
// https://stackoverflow.com/questions/48582520
// SO class is already self-contained pure java.nio; only added the package.
import java.nio.ByteBuffer;
public class ByteBufTest {
public static final int TEST_BUFFER_SIZE = 128;
// private ByteBuffer mDirectBuffer;
public ByteBufTest() {
// FIX (from accepted answer): mDirectBuffer = ByteBuffer.wrap(new byte[TEST_BUFFER_SIZE]);
// or guard with: if (mDirectBuffer.hasArray()) { ... }
ByteBuffer mDirectBuffer = ByteBuffer.allocateDirect(TEST_BUFFER_SIZE);
// VIOLATION: a direct buffer is not array-backed -> array() throws
// UnsupportedOperationException.
byte[] buf = mDirectBuffer.array(); // State Refinement Error
buf[1] = 100;
}
public void test(ByteBuffer mDirectBuffer) {
printBuffer("nativeInitDirectBuffer", mDirectBuffer.array()); // State Refinement Error
}
private void printBuffer(String tag, byte[] buffer) {
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < buffer.length; i++) {
sBuffer.append(buffer[i]);
sBuffer.append(" ");
}
}
public static void main(String[] args) throws Exception {
ByteBufTest item = new ByteBufTest();
ByteBuffer mDirectBuffer = ByteBuffer.allocateDirect(128);
item.test(mDirectBuffer);
}
}