Example usage for org.apache.hadoop.io WritableComparator readUnsignedShort

List of usage examples for org.apache.hadoop.io WritableComparator readUnsignedShort

Introduction

In this page you can find the example usage for org.apache.hadoop.io WritableComparator readUnsignedShort.

Prototype

public static int readUnsignedShort(byte[] bytes, int start) 

Source Link

Document

Parse an unsigned short from a byte array.

Usage

From source file:com.asakusafw.runtime.value.ByteArrayUtil.java

License:Apache License

static short readShort(byte[] bytes, int offset) {
    return (short) WritableComparator.readUnsignedShort(bytes, offset);
}

From source file:ilps.hadoop.StringLongPair.java

License:Apache License

public static String readUTF(byte[] bytes, int s) {
    try {//from  w  ww . java  2s. com
        int utflen = WritableComparator.readUnsignedShort(bytes, s);

        byte[] bytearr = new byte[utflen];
        char[] chararr = new char[utflen];

        int c, char2, char3;
        int count = 0;
        int chararr_count = 0;

        System.arraycopy(bytes, s + 2, bytearr, 0, utflen);
        // in.readFully(bytearr, 0, utflen);

        while (count < utflen) {
            c = (int) bytearr[count] & 0xff;
            if (c > 127)
                break;
            count++;
            chararr[chararr_count++] = (char) c;
        }

        while (count < utflen) {
            c = (int) bytearr[count] & 0xff;
            switch (c >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                /* 0xxxxxxx */
                count++;
                chararr[chararr_count++] = (char) c;
                break;
            case 12:
            case 13:
                /* 110x xxxx 10xx xxxx */
                count += 2;
                if (count > utflen)
                    throw new UTFDataFormatException("malformed input: partial character at end");
                char2 = (int) bytearr[count - 1];
                if ((char2 & 0xC0) != 0x80)
                    throw new UTFDataFormatException("malformed input around byte " + count);
                chararr[chararr_count++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
            case 14:
                /* 1110 xxxx 10xx xxxx 10xx xxxx */
                count += 3;
                if (count > utflen)
                    throw new UTFDataFormatException("malformed input: partial character at end");
                char2 = (int) bytearr[count - 2];
                char3 = (int) bytearr[count - 1];
                if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
                    throw new UTFDataFormatException("malformed input around byte " + (count - 1));
                chararr[chararr_count++] = (char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6)
                        | ((char3 & 0x3F) << 0));
                break;
            default:
                /* 10xx xxxx, 1111 xxxx */
                throw new UTFDataFormatException("malformed input around byte " + count);
            }
        }
        // The number of chars produced may be less than utflen
        return new String(chararr, 0, chararr_count);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.apache.mrql.MR_short.java

License:Apache License

final public static int compare(byte[] x, int xs, int xl, byte[] y, int ys, int yl, int[] size) {
    size[0] = (Short.SIZE >> 3) + 1;
    return WritableComparator.readUnsignedShort(x, xs) - WritableComparator.readUnsignedShort(y, ys);
}