/** * Delegates to {@link EndianUtils#readSwappedUnsignedShort(InputStream)}. * @return the read long * @throws EOFException if an end of file is reached unexpectedly * @throws IOException if an I/O error occurs */ @Override public int readUnsignedShort() throws IOException, EOFException { return EndianUtils.readSwappedUnsignedShort( in ); }
/** * Converts a "float" value between endian systems. * @param value value to convert * @return the converted value */ public static float swapFloat(final float value) { return Float.intBitsToFloat( swapInteger( Float.floatToIntBits( value ) ) ); }
/** * Writes a "float" value to a byte array at a given offset. The value is * converted to the opposed endian system while writing. * @param data target byte array * @param offset starting offset in the byte array * @param value value to write */ public static void writeSwappedFloat(final byte[] data, final int offset, final float value) { writeSwappedInteger( data, offset, Float.floatToIntBits( value ) ); }
/** * Reads a "long" value from an InputStream. The value is * converted to the opposed endian system while reading. * @param input source InputStream * @return the value just read * @throws IOException in case of an I/O problem */ public static long readSwappedLong(final InputStream input) throws IOException { final byte[] bytes = new byte[8]; for ( int i=0; i<8; i++ ) { bytes[i] = (byte) read( input ); } return readSwappedLong( bytes, 0 ); }
@Test public void testSymmetryOfLong() { final double[] tests = new double[] {34.345, -345.5645, 545.12, 10.043, 7.123456789123}; for (final double test : tests) { // testing the real problem byte[] buffer = new byte[8]; final long ln1 = Double.doubleToLongBits( test ); EndianUtils.writeSwappedLong(buffer, 0, ln1); final long ln2 = EndianUtils.readSwappedLong(buffer, 0); assertEquals( ln1, ln2 ); // testing the bug report buffer = new byte[8]; EndianUtils.writeSwappedDouble(buffer, 0, test); final double val = EndianUtils.readSwappedDouble(buffer, 0); assertEquals( test, val, 0 ); } }
/** * Reads a "long" value from a byte array at a given offset. The value is * converted to the opposed endian system while reading. * @param data source byte array * @param offset starting offset in the byte array * @return the value read */ public static long readSwappedLong(final byte[] data, final int offset) { final long low = readSwappedInteger(data, offset); final long high = readSwappedInteger(data, offset + 4); return (high << 32) + (0xffffffffL & low); }
/** * Tests all swapXxxx methods for symmetry when going from one endian * to another and back again. */ @Test public void testSymmetry() { assertEquals( (short) 0x0102, EndianUtils.swapShort( EndianUtils.swapShort( (short) 0x0102 ) ) ); assertEquals( 0x01020304, EndianUtils.swapInteger( EndianUtils.swapInteger( 0x01020304 ) ) ); assertEquals( 0x0102030405060708L, EndianUtils.swapLong( EndianUtils.swapLong( 0x0102030405060708L ) ) ); final float f1 = Float.intBitsToFloat( 0x01020304 ); assertEquals( f1, EndianUtils.swapFloat( EndianUtils.swapFloat( f1 ) ), 0.0 ); final double d1 = Double.longBitsToDouble( 0x0102030405060708L ); assertEquals( d1, EndianUtils.swapDouble( EndianUtils.swapDouble( d1 ) ), 0.0 ); }
/** * Delegates to {@link EndianUtils#readSwappedShort(InputStream)}. * @return the read long * @throws EOFException if an end of file is reached unexpectedly * @throws IOException if an I/O error occurs */ @Override public short readShort() throws IOException, EOFException { return EndianUtils.readSwappedShort( in ); }
/** * Writes a "double" value to a byte array at a given offset. The value is * converted to the opposed endian system while writing. * @param data target byte array * @param offset starting offset in the byte array * @param value value to write */ public static void writeSwappedDouble(final byte[] data, final int offset, final double value) { writeSwappedLong( data, offset, Double.doubleToLongBits( value ) ); }
/** * Delegates to {@link EndianUtils#readSwappedDouble(InputStream)}. * @return the read long * @throws IOException if an I/O error occurs * @throws EOFException if an end of file is reached unexpectedly */ @Override public double readDouble() throws IOException, EOFException { return EndianUtils.readSwappedDouble( in ); }
/** * Converts a "double" value between endian systems. * @param value value to convert * @return the converted value */ public static double swapDouble(final double value) { return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) ); }
/** * Delegates to {@link EndianUtils#readSwappedFloat(InputStream)}. * @return the read long * @throws IOException if an I/O error occurs * @throws EOFException if an end of file is reached unexpectedly */ @Override public float readFloat() throws IOException, EOFException { return EndianUtils.readSwappedFloat( in ); }
/** * Delegates to {@link EndianUtils#readSwappedLong(InputStream)}. * @return the read long * @throws EOFException if an end of file is reached unexpectedly * @throws IOException if an I/O error occurs */ @Override public long readLong() throws IOException, EOFException { return EndianUtils.readSwappedLong( in ); }
@Override public void write(Writable r) throws IOException { BytesWritable bw = (BytesWritable) r; int recordLength = bw.getLength(); //Based on the row length to decide if the length is int or short String rowLength = tableProperties .getProperty(TeradataBinaryRecordReader.TD_ROW_LENGTH, TeradataBinaryRecordReader.DEFAULT_TD_ROW_LENGTH) .toLowerCase(); LOG.debug(format("The table property %s is: %s", TeradataBinaryRecordReader.TD_ROW_LENGTH, rowLength)); if (TeradataBinaryRecordReader.TD_ROW_LENGTH_TO_BYTE_NUM.containsKey(rowLength)) { if (rowLength.equals(TeradataBinaryRecordReader.DEFAULT_TD_ROW_LENGTH)) { EndianUtils.writeSwappedShort(outStream, (short) recordLength); // write the length using little endian } else if (rowLength.equals(TeradataBinaryRecordReader.TD_ROW_LENGTH_1MB)) { EndianUtils.writeSwappedInteger(outStream, recordLength); // write the length using little endian } } else { throw new IllegalArgumentException(format("%s doesn't support the value %s, the supported values are %s", TeradataBinaryRecordReader.TD_ROW_LENGTH, rowLength, TeradataBinaryRecordReader.TD_ROW_LENGTH_TO_BYTE_NUM.keySet())); } outStream.write(bw.getBytes(), 0, bw.getLength()); // write the content (the content is in little endian) outStream.write(RECORD_END_BYTE); //write the record ending }
/** * Delegates to {@link EndianUtils#readSwappedInteger(InputStream)}. * @return the read long * @throws EOFException if an end of file is reached unexpectedly * @throws IOException if an I/O error occurs */ @Override public int readInt() throws IOException, EOFException { return EndianUtils.readSwappedInteger( in ); }
/** @see java.io.DataInput#readShort() */ public short readShort() throws IOException, EOFException { return EndianUtils.readSwappedShort( in ); }
/** * Writes a "double" value to an OutputStream. The value is * converted to the opposed endian system while writing. * @param output target OutputStream * @param value value to write * @throws IOException in case of an I/O problem */ public static void writeSwappedDouble(final OutputStream output, final double value) throws IOException { writeSwappedLong( output, Double.doubleToLongBits( value ) ); }
/** @see java.io.DataInput#readDouble() */ public double readDouble() throws IOException, EOFException { return EndianUtils.readSwappedDouble( in ); }
/** * Converts a "double" value between endian systems. * @param value value to convert * @return the converted value */ public static double swapDouble(double value) { return Double.longBitsToDouble( swapLong( Double.doubleToLongBits( value ) ) ); }
/** @see java.io.DataInput#readFloat() */ public float readFloat() throws IOException, EOFException { return EndianUtils.readSwappedFloat( in ); }