Fixes issue 3

This commit is contained in:
Taro L. Saito 2011-03-31 22:56:43 +09:00
parent 12069f4624
commit 2f29ce3426
1 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,76 @@
/*--------------------------------------------------------------------------
* 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
//
// SnappyInputStreamTest.java
// Since: 2011/03/31 22:31:51
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.snappy;
import static org.junit.Assert.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
import org.xerial.util.FileResource;
import org.xerial.util.log.Logger;
public class SnappyInputStreamTest
{
private static Logger _logger = Logger.getLogger(SnappyInputStreamTest.class);
public static byte[] readResourceFile(String fileName) throws IOException {
BufferedInputStream input = FileResource.openByteStream(SnappyOutputStreamTest.class, fileName);
assertNotNull(input);
return readFully(input);
}
public static byte[] readFully(InputStream input) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int readBytes = 0; (readBytes = input.read(buf)) != -1;) {
out.write(buf, 0, readBytes);
}
out.flush();
return out.toByteArray();
}
@Test
public void read() throws Exception {
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
SnappyOutputStream snappyOut = new SnappyOutputStream(compressedBuf);
byte[] orig = readResourceFile("alice29.txt");
snappyOut.write(orig);
snappyOut.close();
byte[] compressed = compressedBuf.toByteArray();
_logger.debug("compressed size: " + compressed.length);
SnappyInputStream in = new SnappyInputStream(new ByteArrayInputStream(compressed));
byte[] uncompressed = readFully(in);
assertEquals(orig.length, uncompressed.length);
assertArrayEquals(orig, uncompressed);
}
}