1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.util.io;
11
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertTrue;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.util.Arrays;
21
22 import org.junit.Test;
23
24
25
26
27 public class BinaryHunkStreamTest {
28
29 @Test
30 public void testRoundtripWholeBuffer() throws IOException {
31 for (int length = 1; length < 520 + 52; length++) {
32 byte[] data = new byte[length];
33 for (int i = 0; i < data.length; i++) {
34 data[i] = (byte) (255 - (i % 256));
35 }
36 try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
37 BinaryHunkOutputStream out = new BinaryHunkOutputStream(
38 bos)) {
39 out.write(data);
40 out.flush();
41 byte[] encoded = bos.toByteArray();
42 assertFalse(Arrays.equals(data, encoded));
43 try (BinaryHunkInputStream in = new BinaryHunkInputStream(
44 new ByteArrayInputStream(encoded))) {
45 byte[] decoded = new byte[data.length];
46 int newLength = in.read(decoded);
47 assertEquals(newLength, decoded.length);
48 assertEquals(-1, in.read());
49 assertArrayEquals(data, decoded);
50 }
51 }
52 }
53 }
54
55 @Test
56 public void testRoundtripChunks() throws IOException {
57 for (int length = 1; length < 520 + 52; length++) {
58 byte[] data = new byte[length];
59 for (int i = 0; i < data.length; i++) {
60 data[i] = (byte) (255 - (i % 256));
61 }
62 try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
63 BinaryHunkOutputStream out = new BinaryHunkOutputStream(
64 bos)) {
65 out.write(data, 0, data.length / 2);
66 out.write(data, data.length / 2, data.length - data.length / 2);
67 out.flush();
68 byte[] encoded = bos.toByteArray();
69 assertFalse(Arrays.equals(data, encoded));
70 try (BinaryHunkInputStream in = new BinaryHunkInputStream(
71 new ByteArrayInputStream(encoded))) {
72 byte[] decoded = new byte[data.length];
73 int p = 0;
74 int n;
75 while ((n = in.read(decoded, p,
76 Math.min(decoded.length - p, 57))) >= 0) {
77 p += n;
78 if (p == decoded.length) {
79 break;
80 }
81 }
82 assertEquals(p, decoded.length);
83 assertEquals(-1, in.read());
84 assertArrayEquals(data, decoded);
85 }
86 }
87 }
88 }
89
90 @Test
91 public void testRoundtripBytes() throws IOException {
92 for (int length = 1; length < 520 + 52; length++) {
93 byte[] data = new byte[length];
94 for (int i = 0; i < data.length; i++) {
95 data[i] = (byte) (255 - (i % 256));
96 }
97 try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
98 BinaryHunkOutputStream out = new BinaryHunkOutputStream(
99 bos)) {
100 for (int i = 0; i < data.length; i++) {
101 out.write(data[i]);
102 }
103 out.flush();
104 byte[] encoded = bos.toByteArray();
105 assertFalse(Arrays.equals(data, encoded));
106 try (BinaryHunkInputStream in = new BinaryHunkInputStream(
107 new ByteArrayInputStream(encoded))) {
108 byte[] decoded = new byte[data.length];
109 for (int i = 0; i < decoded.length; i++) {
110 int val = in.read();
111 assertTrue(0 <= val && val <= 255);
112 decoded[i] = (byte) val;
113 }
114 assertEquals(-1, in.read());
115 assertArrayEquals(data, decoded);
116 }
117 }
118 }
119 }
120
121 @Test
122 public void testRoundtripWithClose() throws IOException {
123 for (int length = 1; length < 520 + 52; length++) {
124 byte[] data = new byte[length];
125 for (int i = 0; i < data.length; i++) {
126 data[i] = (byte) (255 - (i % 256));
127 }
128 try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
129 try (BinaryHunkOutputStream out = new BinaryHunkOutputStream(
130 bos)) {
131 out.write(data);
132 }
133 byte[] encoded = bos.toByteArray();
134 assertFalse(Arrays.equals(data, encoded));
135 try (BinaryHunkInputStream in = new BinaryHunkInputStream(
136 new ByteArrayInputStream(encoded))) {
137 byte[] decoded = new byte[data.length];
138 int newLength = in.read(decoded);
139 assertEquals(newLength, decoded.length);
140 assertEquals(-1, in.read());
141 assertArrayEquals(data, decoded);
142 }
143 }
144 }
145 }
146 }