Example usage for org.apache.commons.io EndianUtils readSwappedUnsignedShort

List of usage examples for org.apache.commons.io EndianUtils readSwappedUnsignedShort

Introduction

In this page you can find the example usage for org.apache.commons.io EndianUtils readSwappedUnsignedShort.

Prototype

public static int readSwappedUnsignedShort(byte[] data, int offset) 

Source Link

Document

Reads an unsigned short (16-bit) value from a byte array at a given offset.

Usage

From source file:org.apache.hadoop.hive.ql.io.TeradataBinaryRecordReader.java

/**
 * Reads the next key/value pair from the input for processing.
 *
 * @param key the key to read data into//from  www.ja va 2s  .com
 * @param value the value to read data into
 * @return true iff a key/value was read, false if at EOF
 */
@Override
public synchronized boolean next(NullWritable key, BytesWritable value) throws IOException {

    /* read the record length */
    int lengthExpected = recordLengthBytes.length;
    int hasConsumed = readExpectedBytes(recordLengthBytes, lengthExpected);
    if (hasConsumed == 0) {
        LOG.info("Reach the End of File. No more record");
        return false;
    } else if (hasConsumed < lengthExpected) {
        LOG.error(format("We expect %s bytes for the record length but read %d byte and reach the End of File.",
                lengthExpected, hasConsumed));
        LOG.error(format("The current position in the file : %s", getFilePosition()));
        LOG.error(format("The current consumed bytes: %s", pos));
        LOG.error(format("The bytes for the current record is: %s", Hex.encodeHexString(recordLengthBytes)));
        throw new EOFException("When reading the record length, reach the unexpected end of file.");
    }
    /* get the record contend length to prepare to read the content */
    recordLength = EndianUtils.readSwappedUnsignedShort(recordLengthBytes, 0);
    pos += lengthExpected;

    /* read the record content */
    lengthExpected = recordLength;
    hasConsumed = readExpectedBytes(valueByteArray, lengthExpected);
    if (hasConsumed < lengthExpected) {
        LOG.error(
                format("We expect %s bytes for the record content but read %d byte and reach the End of File.",
                        lengthExpected, hasConsumed));
        LOG.error(format("The current position in the file : %s", getFilePosition()));
        LOG.error(format("The current consumed bytes: %s", pos));
        LOG.error(format("The bytes for the current record is: %s",
                Hex.encodeHexString(recordLengthBytes) + Hex.encodeHexString(valueByteArray)));
        throw new EOFException("When reading the contend of the record, reach the unexpected end of file.");
    }
    value.set(valueByteArray, 0, recordLength);
    pos += lengthExpected;

    /* read the record end */
    lengthExpected = endOfRecord.length;
    hasConsumed = readExpectedBytes(endOfRecord, lengthExpected);
    if (hasConsumed < lengthExpected) {
        LOG.error(format(
                "We expect %s bytes for the record end symbol but read %d byte and reach the End of File.",
                lengthExpected, hasConsumed));
        LOG.error(format("The current position in the file : %s", getFilePosition()));
        LOG.error(format("The current consumed bytes: %s", pos));
        LOG.error(format("The bytes for the current record is: %s", Hex.encodeHexString(recordLengthBytes)
                + Hex.encodeHexString(valueByteArray) + Hex.encodeHexString(endOfRecord)));
        throw new EOFException("When reading the end of record, reach the unexpected end of file.");
    }

    if (endOfRecord[0] != TeradataBinaryFileOutputFormat.RECORD_END_BYTE) {
        throw new IOException(
                format("We expect 0x0a as the record end but get %s.", Hex.encodeHexString(endOfRecord)));
    }
    pos += lengthExpected;

    return true;
}