diff --git ql/src/java/org/apache/hadoop/hive/ql/io/orc/SerializationUtils.java ql/src/java/org/apache/hadoop/hive/ql/io/orc/SerializationUtils.java index b14fa7b..54b9aae 100644 --- ql/src/java/org/apache/hadoop/hive/ql/io/orc/SerializationUtils.java +++ ql/src/java/org/apache/hadoop/hive/ql/io/orc/SerializationUtils.java @@ -73,17 +73,23 @@ long readVslong(InputStream in) throws IOException { } float readFloat(InputStream in) throws IOException { - int ser = in.read() | (in.read() << 8) | (in.read() << 16) | - (in.read() << 24); + if (4 != in.read(readBuffer, 0, 4)) { + throw new EOFException("Reading float past EOF"); + } + int ser = ((readBuffer[0] & 0xff) << 0) + | ((readBuffer[1] & 0xff) << 8) + | ((readBuffer[2] & 0xff) << 16) + | ((readBuffer[3] & 0xff) << 24); return Float.intBitsToFloat(ser); } void writeFloat(OutputStream output, float value) throws IOException { int ser = Float.floatToIntBits(value); - output.write(ser & 0xff); - output.write((ser >> 8) & 0xff); - output.write((ser >> 16) & 0xff); - output.write((ser >> 24) & 0xff); + writeBuffer[0] = (byte) (ser & 0xff); + writeBuffer[1] = (byte) ((ser >> 8) & 0xff); + writeBuffer[2] = (byte) ((ser >> 16) & 0xff); + writeBuffer[3] = (byte) ((ser >> 24) & 0xff); + output.write(writeBuffer, 0, 4); } double readDouble(InputStream in) throws IOException { @@ -91,7 +97,9 @@ void writeFloat(OutputStream output, float value) throws IOException { } long readLongLE(InputStream in) throws IOException { - in.read(readBuffer, 0, 8); + if (8 != in.read(readBuffer, 0, 8)) { + throw new EOFException("Reading Double past EOF"); + } return (((readBuffer[0] & 0xff) << 0) + ((readBuffer[1] & 0xff) << 8) + ((readBuffer[2] & 0xff) << 16) diff --git ql/src/test/org/apache/hadoop/hive/ql/io/orc/TestSerializationUtils.java ql/src/test/org/apache/hadoop/hive/ql/io/orc/TestSerializationUtils.java index b3f9cf1..6bba934 100644 --- ql/src/test/org/apache/hadoop/hive/ql/io/orc/TestSerializationUtils.java +++ ql/src/test/org/apache/hadoop/hive/ql/io/orc/TestSerializationUtils.java @@ -50,6 +50,19 @@ public void testDoubles() throws Exception { } @Test + public void testFloats() throws Exception { + double tolerance = 0.0000000000000001; + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + SerializationUtils utils = new SerializationUtils(); + utils.writeFloat(buffer, 1343822337.759f); + assertEquals(1343822337.759f, utils.readFloat(fromBuffer(buffer)), tolerance); + buffer = new ByteArrayOutputStream(); + utils.writeFloat(buffer, 0.8f); + float got = utils.readFloat(fromBuffer(buffer)); + assertEquals(0.8f, got, tolerance); + } + + @Test public void testBigIntegers() throws Exception { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); SerializationUtils.writeBigInteger(buffer, BigInteger.valueOf(0));