forked from xerial/snappy-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalgaryTest.java
More file actions
executable file
·228 lines (192 loc) · 7.89 KB
/
Copy pathCalgaryTest.java
File metadata and controls
executable file
·228 lines (192 loc) · 7.89 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*--------------------------------------------------------------------------
* Copyright 2011 Taro L. Saito
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*--------------------------------------------------------------------------*/
//--------------------------------------
// XerialJ
//
// CalgaryTest.java
// Since: 2011/04/04 12:10:36
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.snappy;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.xerial.util.FileResource;
import org.xerial.util.log.Logger;
/**
* Benchmark using Calgary data set
*
* @author leo
*
*/
public class CalgaryTest
{
private static Logger _logger = Logger.getLogger(CalgaryTest.class);
@Rule
public final TemporaryFolder tempFolder = new TemporaryFolder();
static byte[] readFile(String file) throws IOException {
InputStream in = FileResource.find(CalgaryTest.class, file).openStream();
if (in == null)
throw new IOException("file " + file + " is not found");
try {
return SnappyInputStreamTest.readFully(in);
}
finally {
if (in != null)
in.close();
}
}
public static final String[] files = { "bib", "book1", "book2", "geo", "news", "obj1", "obj2", "paper1", "paper2",
"paper3", "paper4", "paper5", "paper6", "pic", "progc", "progl", "progp", "trans" };
@Test
public void block() throws Exception {
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
byte[] compressed = Snappy.compress(orig);
byte[] uncompressed = Snappy.uncompress(compressed);
assertArrayEquals(orig, uncompressed);
}
}
@Test
public void stream() throws Exception {
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
SnappyOutputStream out = new SnappyOutputStream(compressedBuf);
out.write(orig);
out.close();
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
byte[] uncompressed = new byte[orig.length];
int readBytes = in.read(uncompressed);
assertEquals(orig.length, readBytes);
assertArrayEquals(orig, uncompressed);
}
}
@Test
public void streamFramed() throws Exception {
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedBuf);
out.write(orig);
out.close();
SnappyFramedInputStream in = new SnappyFramedInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
byte[] uncompressed = new byte[orig.length];
int readBytes = readBytes(in, uncompressed, 0, orig.length);
assertEquals(orig.length, readBytes);
assertArrayEquals(orig, uncompressed);
}
}
@Test
public void streamFramedToFile() throws Exception {
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
final File tempFile = tempFolder.newFile(f);
final FileOutputStream compressedFOS = new FileOutputStream(tempFile);
try
{
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedFOS);
out.write(orig);
out.close();
}
finally
{
compressedFOS.close();
}
byte[] uncompressed = new byte[orig.length];
final FileInputStream compressedFIS = new FileInputStream(tempFile);
try
{
SnappyFramedInputStream in = new SnappyFramedInputStream(compressedFIS.getChannel());
int readBytes = readBytes(in, uncompressed, 0, orig.length);
assertEquals(orig.length, readBytes);
}
finally
{
compressedFIS.close();
}
assertArrayEquals(orig, uncompressed);
}
}
@Test
public void streamFramedNoCRCVerify() throws Exception {
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
SnappyFramedOutputStream out = new SnappyFramedOutputStream(compressedBuf);
out.write(orig);
out.close();
SnappyFramedInputStream in = new SnappyFramedInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()), false);
byte[] uncompressed = new byte[orig.length];
int readBytes = readBytes(in, uncompressed, 0, orig.length);
assertEquals(orig.length, readBytes);
assertArrayEquals(orig, uncompressed);
}
}
@Test
public void byteWiseRead() throws Exception {
for (String f : files) {
byte[] orig = readFile("testdata/calgary/" + f);
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
SnappyOutputStream out = new SnappyOutputStream(compressedBuf);
out.write(orig);
out.close();
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressedBuf.toByteArray()));
byte[] uncompressed = new byte[orig.length];
int cursor = 0;
for (;;) {
int b = in.read();
if (b == -1)
break;
uncompressed[cursor++] = (byte) b;
}
assertEquals(orig.length, cursor);
assertArrayEquals(orig, uncompressed);
}
}
static final int readBytes(InputStream source, byte[] dest, int offset, int length) throws IOException
{
// how many bytes were read.
int lastRead = source.read(dest, offset, length);
int totalRead = lastRead;
// if we did not read as many bytes as we had hoped, try reading again.
if (lastRead < length)
{
// as long the buffer is not full (remaining() == 0) and we have not reached EOF (lastRead == -1) keep reading.
while (totalRead < length && lastRead != -1)
{
lastRead = source.read(dest, offset + totalRead, length - totalRead);
// if we got EOF, do not add to total read.
if (lastRead != -1)
{
totalRead += lastRead;
}
}
}
return totalRead;
}
}