/** * Write SHORT. * using little-endian to write short. * * @param s the s * @throws IOException the io exception */ public void writeShort(short s) throws IOException { EndianUtils.writeSwappedShort(this, s); }
/** * Write VARBYTE(N). * The representation of VARBYTE in Teradata binary format is: * the first two bytes represent the length N of this varchar field, * the next N bytes represent the content of this varchar field. * To pad the null varbyte, the length will be 0 and the content will be none. * * @param writable the writable * @throws IOException the io exception */ public void writeVarByte(BytesWritable writable) throws IOException { if (writable == null) { EndianUtils.writeSwappedShort(this, (short) 0); return; } int varbyteLength = writable.getLength(); EndianUtils.writeSwappedShort(this, (short) varbyteLength); // write the varbyte length write(writable.getBytes(), 0, varbyteLength); // write the varchar content } }
/** * Write VARCHAR(N). * The representation of Varchar in Teradata binary format is: * the first two bytes represent the length N of this varchar field, * the next N bytes represent the content of this varchar field. * To pad the null varchar, the length will be 0 and the content will be none. * * @param writable the writable * @throws IOException the io exception */ public void writeVarChar(HiveVarcharWritable writable) throws IOException { if (writable == null) { EndianUtils.writeSwappedShort(this, (short) 0); return; } Text t = writable.getTextValue(); int varcharLength = t.getLength(); EndianUtils.writeSwappedShort(this, (short) varcharLength); // write the varchar length write(t.getBytes(), 0, varcharLength); // write the varchar content }
@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 }
@Test public void testWriteSwappedShort() throws IOException { byte[] bytes = new byte[2]; EndianUtils.writeSwappedShort( bytes, 0, (short) 0x0102 ); assertEquals( 0x02, bytes[0] ); assertEquals( 0x01, bytes[1] ); final ByteArrayOutputStream baos = new ByteArrayOutputStream(2); EndianUtils.writeSwappedShort( baos, (short) 0x0102 ); bytes = baos.toByteArray(); assertEquals( 0x02, bytes[0] ); assertEquals( 0x01, bytes[1] ); }
public void writeUnsignedShort(int num) throws IOException { EndianUtils.writeSwappedShort(stream, (short) num); }
public void writeShort(int v) throws IOException { EndianUtils.writeSwappedShort(stream, (short) v); }