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
|
/*
* Copyright (C) 2023, SAP SE and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.util.io;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.eclipse.jgit.internal.JGitText;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ByteBufferInputStreamTest {
private static final byte data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
private ByteBuffer buf;
private ByteBufferInputStream is;
@Before
public void setup() {
buf = ByteBuffer.wrap(data);
is = new ByteBufferInputStream(buf);
}
@After
public void tearDown() {
is.close();
}
@Test
public void testRead() throws IOException {
assertEquals(0x00, is.read());
assertEquals(0x01, is.read());
assertEquals(0x02, is.read());
assertEquals(0x03, is.read());
assertEquals(0x04, is.read());
assertEquals(0x05, is.read());
assertEquals(0x06, is.read());
assertEquals(0x07, is.read());
assertEquals(0x08, is.read());
assertEquals(0x09, is.read());
assertEquals(0x0A, is.read());
assertEquals(0x0B, is.read());
assertEquals(0x0C, is.read());
assertEquals(0x0D, is.read());
assertEquals(0x0E, is.read());
assertEquals(0x0F, is.read());
assertEquals(-1, is.read());
}
@Test
public void testReadMultiple() throws IOException {
byte[] x = new byte[5];
int n = is.read(x);
assertEquals(5, n);
assertArrayEquals(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 }, x);
}
@Test
public void testReadMultipleOffset() throws IOException {
byte[] x = new byte[7];
int n = is.read(x, 4, 3);
assertEquals(3, n);
assertArrayEquals(
new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02 },
x);
}
@Test
public void testReadAll() throws IOException {
byte[] x = is.readAllBytes();
assertEquals(16, x.length);
assertArrayEquals(data, x);
}
@Test
public void testMarkReset() throws IOException {
byte[] x = new byte[5];
int n = is.read(x);
assertEquals(11, is.available());
assertTrue(is.markSupported());
is.mark(is.available());
is.reset();
byte[] y = new byte[5];
int m = is.read(y);
assertEquals(n, m);
assertArrayEquals(new byte[] { 0x05, 0x06, 0x07, 0x08, 0x09 }, y);
}
@Test
public void testClosed() {
is.close();
Exception e = assertThrows(IOException.class, () -> is.read());
assertEquals(JGitText.get().inputStreamClosed, e.getMessage());
}
@Test
public void testReadNBytes() throws IOException {
byte[] x = is.readNBytes(4);
assertArrayEquals(new byte[] { 0x00, 0x01, 0x02, 0x03 }, x);
}
@Test
public void testReadNBytesOffset() throws IOException {
byte[] x = new byte[10];
Arrays.fill(x, (byte) 0x0F);
is.readNBytes(x, 3, 4);
assertArrayEquals(new byte[] { 0x0F, 0x0F, 0x0F, 0x00, 0x01, 0x02, 0x03,
0x0F, 0x0F, 0x0F }, x);
}
@Test
public void testRead0() throws IOException {
byte[] x = new byte[7];
int n = is.read(x, 4, 0);
assertEquals(0, n);
is.readAllBytes();
n = is.read(x, 4, 3);
assertEquals(-1, n);
}
@Test
public void testSkip() throws IOException {
assertEquals(15, is.skip(15));
assertEquals(0x0F, is.read());
assertEquals(-1, is.read());
}
@Test
public void testSkip0() throws IOException {
assertEquals(0, is.skip(0));
assertEquals(0x00, is.read());
}
}
|