Fixes issue 24
This commit is contained in:
parent
16ce3bc904
commit
c73d2e104b
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue