Example usage for com.google.common.primitives Shorts fromBytes

List of usage examples for com.google.common.primitives Shorts fromBytes

Introduction

In this page you can find the example usage for com.google.common.primitives Shorts fromBytes.

Prototype

@GwtIncompatible("doesn't work")
public static short fromBytes(byte b1, byte b2) 

Source Link

Document

Returns the short value whose byte representation is the given 2 bytes, in big-endian order; equivalent to Shorts.fromByteArray(new byte[] {b1, b2})}.

Usage

From source file:org.apache.drill.exec.store.pcap.PcapFormatUtils.java

/**
 *
 * @param byteOrder true for forward file order, false fore revers file order
 * @param buf byte buffer/*from   www.j a v  a 2s .c om*/
 * @param offset buffer offset
 * @return short value as int of specific bytes from buffer
 */
public static int getShortFileOrder(boolean byteOrder, final byte[] buf, final int offset) {
    if (byteOrder) {
        return Shorts.fromBytes(buf[offset], buf[offset + 1]);
    } else {
        return Shorts.fromBytes(buf[offset + 1], buf[offset]);
    }
}

From source file:org.apache.drill.exec.store.pcap.PcapFormatUtils.java

public static int getShort(final byte[] buf, final int offset) {
    return 0xffff & Shorts.fromBytes(buf[offset], buf[offset + 1]);
}

From source file:org.kegbot.kegboard.KegboardMessageFactory.java

public synchronized KegboardMessage getMessage() {
    // Consume from mBuffer until done
    while (mAppendPosition > 0) {
        final int available = mAppendPosition;
        if (available < KBSP_MIN_PACKET_SIZE) {
            return null;
        }//www  .j  a  v  a 2s.com

        boolean framingError = false;
        for (int i = 0; i < KBSP_PREFIX.length; i++) {
            if (mBuffer[i] != KBSP_PREFIX[i]) {
                compact(i + 1);
                framingError = true;
                break;
            }
        }

        if (framingError) {
            //debug("Framing error: " + HexDump.dumpHexString(mBuffer, 0, 12));
            continue;
        }

        int payloadLength = Shorts.fromBytes(mBuffer[11], mBuffer[10]);
        if (payloadLength > 240) {
            compact(KBSP_HEADER_LENGTH);
            framingError("Illegal payload length");
            return null;
        }

        int totalLength = KBSP_HEADER_LENGTH + payloadLength + KBSP_TRAILER_LENGTH;
        if (available < totalLength) {
            return null;
        }

        final KegboardMessage message;
        try {
            message = KegboardMessage.fromBytes(Arrays.copyOf(mBuffer, totalLength));
            return message;
        } catch (KegboardMessageException e) {
            debug("Error building message: " + e);
            /* Don't return, keep trying. */
        } finally {
            compact(totalLength);
        }
    }
    return null;
}

From source file:org.kegbot.kegboard.KegboardMessage.java

protected KegboardMessage(byte[] wholeMessage) throws KegboardMessageException {
    if (wholeMessage.length < KBSP_MIN_LENGTH) {
        throw new KegboardMessageException(
                "Raw message size too small: min=" + KBSP_MIN_LENGTH + ", actual=" + wholeMessage.length);
    } else if (wholeMessage.length > KBSP_MAX_LENGTH) {
        throw new KegboardMessageException(
                "Raw message size too large: max=" + KBSP_MAX_LENGTH + ", actual=" + wholeMessage.length);
    }/*from w  w w. j  a  v a 2s .  com*/

    final int payloadLength = Shorts.fromBytes(wholeMessage[11], wholeMessage[10]) & 0x0ffff;
    final int payloadEnd = KBSP_HEADER_LENGTH + payloadLength;
    if (payloadLength > KBSP_PAYLOAD_MAX_LENGTH) {
        throw new KegboardMessageException(
                "Illegal payload size: max=" + KBSP_PAYLOAD_MAX_LENGTH + ", actual=" + payloadLength);
    }

    final int totalMessageSize = KBSP_HEADER_LENGTH + payloadLength + KBSP_TRAILER_LENGTH;
    if (wholeMessage.length != totalMessageSize) {
        throw new KegboardMessageException("Input buffer size does not match computed size: " + "payloadLength="
                + payloadLength + ", needed=" + totalMessageSize + ", actual=" + wholeMessage.length);
    }

    final byte[] payload;
    if (payloadLength > 0) {
        payload = Arrays.copyOfRange(wholeMessage, KBSP_HEADER_LENGTH, payloadEnd);
    } else {
        payload = new byte[0];
    }

    final byte[] crcBytes = Arrays.copyOfRange(wholeMessage, payloadEnd, payloadEnd + 2);

    final byte[] trailerBytes = Arrays.copyOfRange(wholeMessage, payloadEnd + 2, payloadEnd + 4);
    if (!Arrays.equals(trailerBytes, KBSP_TRAILER_BYTES)) {
        throw new KegboardMessageException("Illegal trailer value.");
    }

    final int expectedCrc = KegboardCrc.crc16Ccitt(wholeMessage, wholeMessage.length - 4) & 0x0ffff;
    final int computedCrc = Shorts.fromBytes(crcBytes[1], crcBytes[0]) & 0x0ffff;

    if (expectedCrc != computedCrc) {
        throw new KegboardMessageException(
                "Bad CRC: " + "expected=" + String.format("0x%04x ", Integer.valueOf(expectedCrc)) + "computed="
                        + String.format("0x%04x ", Integer.valueOf(computedCrc)));
    }

    short messageType = Shorts.fromBytes(wholeMessage[9], wholeMessage[8]);
    if (messageType != getMessageType()) {
        throw new KegboardMessageException(
                "Message type mismatch: expected=" + getMessageType() + " got=" + messageType);
    }

    // System.out.println(HexDump.dumpHexString(wholeMessage));

    for (int i = 0; i <= (payload.length - 2);) {
        final int tagNum = payload[i] & 0x00ff;
        final int length = payload[i + 1] & 0x00ff;

        i += 2;

        if ((i + length) <= payload.length) {
            mTags.put(Integer.valueOf(tagNum), Arrays.copyOfRange(payload, i, i + length));
        }

        i += length;
    }
}

From source file:com.android.tools.idea.apk.viewer.ApkFileSystem.java

public static boolean isBinaryXml(String relativePath, byte[] bytes) {
    if (!relativePath.endsWith(SdkConstants.DOT_XML)) {
        return false;
    }//from  w ww.  j ava 2s .co  m

    boolean encodedXmlPath = relativePath.equals(SdkConstants.FN_ANDROID_MANIFEST_XML)
            || (relativePath.startsWith(SdkConstants.FD_RES)
                    && !relativePath.startsWith(SdkConstants.FD_RES + "/" + SdkConstants.FD_RES_RAW));
    if (!encodedXmlPath) {
        return false;
    }

    short code = Shorts.fromBytes(bytes[1], bytes[0]);
    return code == Chunk.Type.XML.code();
}

From source file:com.facebook.buck.cxx.platform.ObjectFileScrubbers.java

public static short getLittleEndianShort(ByteBuffer buffer) {
    byte b1 = buffer.get();
    byte b2 = buffer.get();
    return Shorts.fromBytes(b2, b1);
}

From source file:org.kegbot.kegboard.KegboardMessage.java

private static int extractType(final byte[] bytes) {
    return Shorts.fromBytes(bytes[9], bytes[8]);
}

From source file:com.liaison.javabasics.serialization.BytesUtil.java

/**
 * TODO/*from   w  w  w  . j ava 2s .  co m*/
 * @param bytes TODO
 * @param offset TODO
 * @return TODO
 */
public static Short toShort(final byte[] bytes, final int offset) {
    // TODO: determine constant values programmatically, or make them constant
    if (bytes != null && offset >= 0 && bytes.length >= offset + 2) {
        return Short.valueOf(Shorts.fromBytes(bytes[offset + 0], bytes[offset + 1]));
    }
    return null;
}

From source file:com.liaison.javabasics.serialization.BytesUtil.java

/**
 * TODO//ww w.  j a va  2s. c o  m
 * @param b1 TODO
 * @param b2 TODO
 * @return TODO
 */
public static short toShort(byte b1, byte b2) {
    return Shorts.fromBytes(b1, b2);
}

From source file:com.android.tools.idea.rendering.ClassConverter.java

/** Returns the major class file version */
public static short getMajorVersion(byte[] classData) {
    // See http://en.wikipedia.org/wiki/Java_class_file
    return Shorts.fromBytes(classData[6], classData[7]);
}