From c73d2e104bec45066cfe9a15a307d4eb2baa88bc Mon Sep 17 00:00:00 2001 From: "Taro L. Saito" Date: Tue, 2 Aug 2011 17:07:24 +0900 Subject: [PATCH] Fixes issue 24 --- .../org/xerial/snappy/SnappyInputStream.java | 132 +++++++++++++++++- .../org/xerial/snappy/SnappyOutputStream.java | 24 ++++ .../xerial/snappy/SnappyOutputStreamTest.java | 68 +++++++++ 3 files changed, 223 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/xerial/snappy/SnappyInputStream.java b/src/main/java/org/xerial/snappy/SnappyInputStream.java index c1a8e0a..2668cc5 100755 --- a/src/main/java/org/xerial/snappy/SnappyInputStream.java +++ b/src/main/java/org/xerial/snappy/SnappyInputStream.java @@ -34,6 +34,10 @@ import java.io.InputStream; * @author leo * */ +/** + * @author leo + * + */ public class SnappyInputStream extends InputStream { private boolean finishedReading = false; @@ -122,6 +126,15 @@ public class SnappyInputStream extends InputStream return rawRead(b, off, len); } + /** + * Read uncompressed data into the specified array + * + * @param array + * @param byteOffset + * @param byteLength + * @return + * @throws IOException + */ public int rawRead(Object array, int byteOffset, int byteLength) throws IOException { int writtenBytes = 0; for (; writtenBytes < byteLength;) { @@ -142,23 +155,140 @@ public class SnappyInputStream extends InputStream } /** + * Read long array from the stream + * * @param d * input * @param off * offset * @param len * the number of long elements to read - * @return written bytes + * @return the number of read bytes * @throws IOException */ public int read(long[] d, int off, int len) throws IOException { return rawRead(d, off * 8, len * 8); } + /** + * Read long array from the stream + * + * @param d + * @return the number of read bytes + * @throws IOException + */ public int read(long[] d) throws IOException { return read(d, 0, d.length); } + /** + * Read double array from the stream + * + * @param d + * input + * @param off + * offset + * @param len + * the number of double elements to read + * @return the number of read bytes + * @throws IOException + */ + public int read(double[] d, int off, int len) throws IOException { + return rawRead(d, off * 8, len * 8); + } + + /** + * Read double array from the stream + * + * @param d + * @return read bytes + * @throws IOException + */ + public int read(double[] d) throws IOException { + return read(d, 0, d.length); + } + + /** + * Read int array from the stream + * + * @param d + * @return read bytes + * @throws IOException + */ + public int read(int[] d) throws IOException { + return read(d, 0, d.length); + } + + /** + * Read int array from the stream + * + * @param d + * input + * @param off + * offset + * @param len + * the number of int elements to read + * @return the number of read bytes + * @throws IOException + */ + public int read(int[] d, int off, int len) throws IOException { + return rawRead(d, off * 4, len * 4); + } + + /** + * Read float array from the stream + * + * @param d + * input + * @param off + * offset + * @param len + * the number of float elements to read + * @return the number of read bytes + * @throws IOException + */ + public int read(float[] d, int off, int len) throws IOException { + return rawRead(d, off * 4, len * 4); + } + + /** + * Read float array from the stream + * + * @param d + * @return the number of read bytes + * @throws IOException + */ + public int read(float[] d) throws IOException { + return read(d, 0, d.length); + } + + /** + * Read short array from the stream + * + * @param d + * input + * @param off + * offset + * @param len + * the number of short elements to read + * @return the number of read bytes + * @throws IOException + */ + public int read(short[] d, int off, int len) throws IOException { + return rawRead(d, off * 2, len * 2); + } + + /** + * Read short array from the stream + * + * @param d + * @return the number of read bytes + * @throws IOException + */ + public int read(short[] d) throws IOException { + return read(d, 0, d.length); + } + protected boolean hasNextChunk() throws IOException { if (finishedReading) return false; diff --git a/src/main/java/org/xerial/snappy/SnappyOutputStream.java b/src/main/java/org/xerial/snappy/SnappyOutputStream.java index c93863f..ba4d541 100755 --- a/src/main/java/org/xerial/snappy/SnappyOutputStream.java +++ b/src/main/java/org/xerial/snappy/SnappyOutputStream.java @@ -97,18 +97,42 @@ public class SnappyOutputStream extends OutputStream rawWrite(d, off * 8, len * 8); } + public void write(double[] f, int off, int len) throws IOException { + rawWrite(f, off * 8, len * 8); + } + public void write(float[] f, int off, int len) throws IOException { rawWrite(f, off * 4, len * 4); } + public void write(int[] f, int off, int len) throws IOException { + rawWrite(f, off * 4, len * 4); + } + + public void write(short[] f, int off, int len) throws IOException { + rawWrite(f, off * 2, len * 2); + } + public void write(long[] d) throws IOException { write(d, 0, d.length); } + public void write(double[] f) throws IOException { + write(f, 0, f.length); + } + public void write(float[] f) throws IOException { write(f, 0, f.length); } + public void write(int[] f) throws IOException { + write(f, 0, f.length); + } + + public void write(short[] f) throws IOException { + write(f, 0, f.length); + } + /** * Compress the raw byte array data. * diff --git a/src/test/java/org/xerial/snappy/SnappyOutputStreamTest.java b/src/test/java/org/xerial/snappy/SnappyOutputStreamTest.java index 1d9ac85..84152f4 100755 --- a/src/test/java/org/xerial/snappy/SnappyOutputStreamTest.java +++ b/src/test/java/org/xerial/snappy/SnappyOutputStreamTest.java @@ -117,4 +117,72 @@ public class SnappyOutputStreamTest } + @Test + public void writeDoubleArray() throws Exception { + ByteArrayOutputStream b = new ByteArrayOutputStream(); + SnappyOutputStream os = new SnappyOutputStream(b); + + double[] orig = new double[] { 1.0, 2.0, 1.4, 0.00343430014, -4.4, 4e-20 }; + os.write(orig); + os.close(); + + SnappyInputStream is = new SnappyInputStream(new ByteArrayInputStream(b.toByteArray())); + double[] uncompressed = new double[orig.length]; + is.read(uncompressed); + is.close(); + + assertArrayEquals(orig, uncompressed, 0.0); + } + + @Test + public void writeFloatArray() throws Exception { + ByteArrayOutputStream b = new ByteArrayOutputStream(); + SnappyOutputStream os = new SnappyOutputStream(b); + + float[] orig = new float[] { 1.0f, 2.0f, 1.4f, 0.00343430014f, -4.4f, 4e-20f }; + os.write(orig); + os.close(); + + SnappyInputStream is = new SnappyInputStream(new ByteArrayInputStream(b.toByteArray())); + float[] uncompressed = new float[orig.length]; + is.read(uncompressed); + is.close(); + + assertArrayEquals(orig, uncompressed, 0.0f); + } + + @Test + public void writeIntArray() throws Exception { + ByteArrayOutputStream b = new ByteArrayOutputStream(); + SnappyOutputStream os = new SnappyOutputStream(b); + + int[] orig = new int[] { 0, -1, -34, 43, 234, 34324, -234 }; + os.write(orig); + os.close(); + + SnappyInputStream is = new SnappyInputStream(new ByteArrayInputStream(b.toByteArray())); + int[] uncompressed = new int[orig.length]; + is.read(uncompressed); + is.close(); + + assertArrayEquals(orig, uncompressed); + } + + @Test + public void writeShortArray() throws Exception { + ByteArrayOutputStream b = new ByteArrayOutputStream(); + SnappyOutputStream os = new SnappyOutputStream(b); + + short[] orig = new short[] { 0, -1, -34, 43, 234, 324, -234 }; + os.write(orig); + os.close(); + + SnappyInputStream is = new SnappyInputStream(new ByteArrayInputStream(b.toByteArray())); + short[] uncompressed = new short[orig.length]; + is.read(uncompressed); + is.close(); + + assertArrayEquals(orig, uncompressed); + } + }