1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.binary;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Random;
23
24
25
26
27
28
29
30
31
32 public class Base32TestData {
33
34 static final String STRING_FIXTURE = "Hello World";
35
36 static final String BASE32_FIXTURE = "JBSWY3DPEBLW64TMMQ======\r\n";
37
38
39
40
41 private final static int SIZE_KEY = 0;
42 private final static int LAST_READ_KEY = 1;
43
44 static byte[] streamToBytes(final InputStream in) throws IOException {
45
46 return streamToBytes(in, new byte[7]);
47 }
48
49 static byte[] streamToBytes(final InputStream in, byte[] buf) throws IOException {
50 try {
51 int[] status = fill(buf, 0, in);
52 int size = status[SIZE_KEY];
53 int lastRead = status[LAST_READ_KEY];
54 while (lastRead != -1) {
55 buf = resizeArray(buf);
56 status = fill(buf, size, in);
57 size = status[SIZE_KEY];
58 lastRead = status[LAST_READ_KEY];
59 }
60 if (buf.length != size) {
61 final byte[] smallerBuf = new byte[size];
62 System.arraycopy(buf, 0, smallerBuf, 0, size);
63 buf = smallerBuf;
64 }
65 }
66 finally {
67 in.close();
68 }
69 return buf;
70 }
71
72 private static int[] fill(final byte[] buf, final int offset, final InputStream in)
73 throws IOException {
74 int read = in.read(buf, offset, buf.length - offset);
75 int lastRead = read;
76 if (read == -1) {
77 read = 0;
78 }
79 while (lastRead != -1 && read + offset < buf.length) {
80 lastRead = in.read(buf, offset + read, buf.length - read - offset);
81 if (lastRead != -1) {
82 read += lastRead;
83 }
84 }
85 return new int[]{offset + read, lastRead};
86 }
87
88 private static byte[] resizeArray(final byte[] bytes) {
89 final byte[] biggerBytes = new byte[bytes.length * 2];
90 System.arraycopy(bytes, 0, biggerBytes, 0, bytes.length);
91 return biggerBytes;
92 }
93
94
95
96
97
98
99
100
101
102 static byte[][] randomData(final BaseNCodec codec, final int size) {
103 final Random r = new Random();
104 final byte[] decoded = new byte[size];
105 r.nextBytes(decoded);
106 final byte[] encoded = codec.encode(decoded);
107 return new byte[][] {decoded, encoded};
108 }
109
110
111
112
113
114
115
116
117 static boolean bytesContain(final byte[] bytes, final byte c) {
118 for (final byte b : bytes) {
119 if (b == c) { return true; }
120 }
121 return false;
122 }
123
124 }