From 2f29ce3426d0c1cb852c73492af3e4b96e7dca74 Mon Sep 17 00:00:00 2001 From: "Taro L. Saito" Date: Thu, 31 Mar 2011 22:56:43 +0900 Subject: [PATCH] Fixes issue 3 --- .../xerial/snappy/SnappyInputStreamTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 src/test/java/org/xerial/snappy/SnappyInputStreamTest.java diff --git a/src/test/java/org/xerial/snappy/SnappyInputStreamTest.java b/src/test/java/org/xerial/snappy/SnappyInputStreamTest.java new file mode 100755 index 0000000..c245935 --- /dev/null +++ b/src/test/java/org/xerial/snappy/SnappyInputStreamTest.java @@ -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); + + } +}