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
|
/*
* Copyright (C) 2021 Thomas Wolf <thomas.wolf@paranor.ch> 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.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Test;
/**
* Tests for {@link BinaryHunkInputStream} and {@link BinaryHunkOutputStream}.
*/
public class BinaryHunkStreamTest {
@Test
public void testRoundtripWholeBuffer() throws IOException {
for (int length = 1; length < 520 + 52; length++) {
byte[] data = new byte[length];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (255 - (i % 256));
}
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
BinaryHunkOutputStream out = new BinaryHunkOutputStream(
bos)) {
out.write(data);
out.flush();
byte[] encoded = bos.toByteArray();
assertFalse(Arrays.equals(data, encoded));
try (BinaryHunkInputStream in = new BinaryHunkInputStream(
new ByteArrayInputStream(encoded))) {
byte[] decoded = new byte[data.length];
int newLength = in.read(decoded);
assertEquals(newLength, decoded.length);
assertEquals(-1, in.read());
assertArrayEquals(data, decoded);
}
}
}
}
@Test
public void testRoundtripChunks() throws IOException {
for (int length = 1; length < 520 + 52; length++) {
byte[] data = new byte[length];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (255 - (i % 256));
}
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
BinaryHunkOutputStream out = new BinaryHunkOutputStream(
bos)) {
out.write(data, 0, data.length / 2);
out.write(data, data.length / 2, data.length - data.length / 2);
out.flush();
byte[] encoded = bos.toByteArray();
assertFalse(Arrays.equals(data, encoded));
try (BinaryHunkInputStream in = new BinaryHunkInputStream(
new ByteArrayInputStream(encoded))) {
byte[] decoded = new byte[data.length];
int p = 0;
int n;
while ((n = in.read(decoded, p,
Math.min(decoded.length - p, 57))) >= 0) {
p += n;
if (p == decoded.length) {
break;
}
}
assertEquals(p, decoded.length);
assertEquals(-1, in.read());
assertArrayEquals(data, decoded);
}
}
}
}
@Test
public void testRoundtripBytes() throws IOException {
for (int length = 1; length < 520 + 52; length++) {
byte[] data = new byte[length];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (255 - (i % 256));
}
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
BinaryHunkOutputStream out = new BinaryHunkOutputStream(
bos)) {
for (int i = 0; i < data.length; i++) {
out.write(data[i]);
}
out.flush();
byte[] encoded = bos.toByteArray();
assertFalse(Arrays.equals(data, encoded));
try (BinaryHunkInputStream in = new BinaryHunkInputStream(
new ByteArrayInputStream(encoded))) {
byte[] decoded = new byte[data.length];
for (int i = 0; i < decoded.length; i++) {
int val = in.read();
assertTrue(0 <= val && val <= 255);
decoded[i] = (byte) val;
}
assertEquals(-1, in.read());
assertArrayEquals(data, decoded);
}
}
}
}
@Test
public void testRoundtripWithClose() throws IOException {
for (int length = 1; length < 520 + 52; length++) {
byte[] data = new byte[length];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (255 - (i % 256));
}
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
try (BinaryHunkOutputStream out = new BinaryHunkOutputStream(
bos)) {
out.write(data);
}
byte[] encoded = bos.toByteArray();
assertFalse(Arrays.equals(data, encoded));
try (BinaryHunkInputStream in = new BinaryHunkInputStream(
new ByteArrayInputStream(encoded))) {
byte[] decoded = new byte[data.length];
int newLength = in.read(decoded);
assertEquals(newLength, decoded.length);
assertEquals(-1, in.read());
assertArrayEquals(data, decoded);
}
}
}
}
}
|