Example usage for java.nio ByteBuffer getShort

List of usage examples for java.nio ByteBuffer getShort

Introduction

In this page you can find the example usage for java.nio ByteBuffer getShort.

Prototype

public abstract short getShort();

Source Link

Document

Returns the short at the current position and increases the position by 2.

Usage

From source file:us.ihmc.idl.CDR.java

public static void readEncapsulation(SerializedPayload payload) {
    ByteBuffer buf = payload.getData();
    /* int dummy = */ buf.get();
    short encapsulation = buf.get();
    payload.setEncapsulation(encapsulation);
    /* this.options = */ buf.getShort();
}

From source file:org.midonet.netlink.rtnetlink.Link.java

public static Link buildFrom(ByteBuffer buf) {
    try {/* ww  w .j a  v a  2 s. c o  m*/
        Link link = new Link();
        link.ifi.family = buf.get();
        buf.get(); // pad
        link.ifi.type = buf.getShort();
        link.ifi.index = buf.getInt();
        link.ifi.flags = buf.getInt();
        link.ifi.change = buf.getInt();
        NetlinkMessage.scanAttributes(buf, link);
        return link;
    } catch (BufferUnderflowException ex) {
        return null;
    }
}

From source file:com.healthmarketscience.jackcess.impl.OleUtil.java

/**
 * creates the appropriate ContentImpl for the given blob.
 *//*from ww w . j  a v  a2s .  c  om*/
private static ContentImpl parseContent(OleBlobImpl blob) throws IOException {
    ByteBuffer bb = PageChannel.wrap(blob.getBytes());

    if ((bb.remaining() < 2) || (bb.getShort() != PACKAGE_SIGNATURE)) {
        return new UnknownContentImpl(blob);
    }

    // read outer package header
    int headerSize = bb.getShort();
    int objType = bb.getInt();
    int prettyNameLen = bb.getShort();
    int classNameLen = bb.getShort();
    int prettyNameOff = bb.getShort();
    int classNameOff = bb.getShort();
    int objSize = bb.getInt();
    String prettyName = readStr(bb, prettyNameOff, prettyNameLen);
    String className = readStr(bb, classNameOff, classNameLen);
    bb.position(headerSize);

    // read ole header
    int oleVer = bb.getInt();
    int format = bb.getInt();

    if (oleVer != OLE_VERSION) {
        return new UnknownContentImpl(blob);
    }

    int typeNameLen = bb.getInt();
    String typeName = readStr(bb, bb.position(), typeNameLen);
    bb.getLong(); // unused
    int dataBlockLen = bb.getInt();
    int dataBlockPos = bb.position();

    if (SIMPLE_PACKAGE_TYPE.equalsIgnoreCase(typeName)) {
        return createSimplePackageContent(blob, prettyName, className, typeName, bb, dataBlockLen);
    }

    // if COMPOUND_FACTORY is null, the poi library isn't available, so just
    // load compound data as "other"
    if ((COMPOUND_FACTORY != null) && (bb.remaining() >= COMPOUND_STORAGE_SIGNATURE.length)
            && ByteUtil.matchesRange(bb, bb.position(), COMPOUND_STORAGE_SIGNATURE)) {
        return COMPOUND_FACTORY.createCompoundPackageContent(blob, prettyName, className, typeName, bb,
                dataBlockLen);
    }

    // this is either some other "special" (as yet unhandled) format, or it is
    // simply an embedded file (or it is compound data and poi isn't available)
    return new OtherContentImpl(blob, prettyName, className, typeName, dataBlockPos, dataBlockLen);
}

From source file:edu.umass.cs.nio.MessageNIOTransport.java

/**
 * @param bytes//from ww w  .  java  2  s  .  c  o  m
 * @return Sender InetSocketAddress from bytes
 * @throws UnknownHostException
 */
public static InetSocketAddress getSenderAddress(byte[] bytes) throws UnknownHostException {
    ByteBuffer bbuf = ByteBuffer.wrap(bytes, 0, NIOHeader.BYTES);
    byte[] addressBytes = new byte[4];
    bbuf.get(addressBytes);
    InetAddress address = InetAddress.getByAddress(addressBytes);
    int port = (int) bbuf.getShort();
    if (port < 0)
        port += 2 * (Short.MAX_VALUE + 1);
    return new InetSocketAddress(address, port);
}

From source file:edu.umass.cs.nio.MessageNIOTransport.java

/**
 * @param bytes/*  w  w  w.  j  av a 2s .  c  o m*/
 * @return Sender InetSocketAddress from bytes
 * @throws UnknownHostException
 */
public static InetSocketAddress getReceiverAddress(byte[] bytes) throws UnknownHostException {
    ByteBuffer bbuf = ByteBuffer.wrap(bytes, 6, NIOHeader.BYTES);
    byte[] addressBytes = new byte[4];
    bbuf.get(addressBytes);
    InetAddress address = InetAddress.getByAddress(addressBytes);
    int port = (int) bbuf.getShort();
    if (port < 0)
        port += 2 * (Short.MAX_VALUE + 1);
    return new InetSocketAddress(address, port);
}

From source file:com.healthmarketscience.jackcess.impl.OleUtil.java

private static ContentImpl createSimplePackageContent(OleBlobImpl blob, String prettyName, String className,
        String typeName, ByteBuffer blobBb, int dataBlockLen) {

    int dataBlockPos = blobBb.position();
    ByteBuffer bb = PageChannel.narrowBuffer(blobBb, dataBlockPos, dataBlockPos + dataBlockLen);

    int packageSig = bb.getShort();
    if (packageSig != PACKAGE_STREAM_SIGNATURE) {
        return new OtherContentImpl(blob, prettyName, className, typeName, dataBlockPos, dataBlockLen);
    }/*from w w w  . j  a  v a 2  s .co m*/

    String fileName = readZeroTermStr(bb);
    String filePath = readZeroTermStr(bb);
    int packageType = bb.getInt();

    if (packageType == PS_EMBEDDED_FILE) {

        int localFilePathLen = bb.getInt();
        String localFilePath = readStr(bb, bb.position(), localFilePathLen);
        int dataLen = bb.getInt();
        int dataPos = bb.position();
        bb.position(dataLen + dataPos);

        // remaining strings are in "reverse" order (local file path, file name,
        // file path).  these string usee a real utf charset, and therefore can
        // "fix" problems with ascii based names (so we prefer these strings to
        // the original strings we found)
        int strNum = 0;
        while (true) {

            int rem = bb.remaining();
            if (rem < 4) {
                break;
            }

            int strLen = bb.getInt();
            String remStr = readStr(bb, bb.position(), strLen * 2, OLE_UTF_CHARSET);

            switch (strNum) {
            case 0:
                localFilePath = remStr;
                break;
            case 1:
                fileName = remStr;
                break;
            case 2:
                filePath = remStr;
                break;
            default:
                // ignore
            }

            ++strNum;
        }

        return new SimplePackageContentImpl(blob, prettyName, className, typeName, dataPos, dataLen, fileName,
                filePath, localFilePath);
    }

    if (packageType == PS_LINKED_FILE) {

        bb.getShort(); //unknown
        String linkStr = readZeroTermStr(bb);

        return new LinkContentImpl(blob, prettyName, className, typeName, fileName, linkStr, filePath);
    }

    return new OtherContentImpl(blob, prettyName, className, typeName, dataBlockPos, dataBlockLen);
}

From source file:org.zaproxy.zap.extension.ascanrulesBeta.HeartBleedActiveScanner.java

/**
 * reads an SSL message from the inputstream
 *
 * @param is//from  ww  w .j  a  va 2s.c o  m
 * @param timeoutMs the timeout in milliseconds
 * @return
 * @throws IOException
 */
static SSLRecord recvmsg(InputStream is, int timeoutMs) throws IOException {
    byte[] messageHeader = recvall(is, 5, timeoutMs);

    // convert the 5 bytes to (big endian) 1 unsigned byte type, 2 unsigned bytes ver, 2
    // unsigned bytes len
    ByteBuffer bb = ByteBuffer.wrap(messageHeader);
    byte type = bb.get();
    short ver = bb.getShort();
    short len = bb.getShort();

    // read the specified number of bytes from the inputstream
    byte[] messagePayload = recvall(is, len, timeoutMs);
    return new SSLRecord(type, ver, len, messagePayload);
}

From source file:org.zaproxy.zap.extension.ascanrulesAlpha.HeartBleedActiveScanner.java

/**
 * reads an SSL message from the inputstream 
 * @param is// w  w w  . j av  a 2 s.  c  o m
 * @param timeoutMs the timeout in milliseconds
 * @return
 * @throws IOException
 */
static SSLRecord recvmsg(InputStream is, int timeoutMs) throws IOException {
    byte[] messageHeader = recvall(is, 5, timeoutMs);

    //convert the 5 bytes to (big endian) 1 unsigned byte type, 2 unsigned bytes ver, 2 unsigned bytes len
    ByteBuffer bb = ByteBuffer.wrap(messageHeader);
    byte type = bb.get();
    short ver = bb.getShort();
    short len = bb.getShort();

    //read the specified number of bytes from the inputstream
    byte[] messagePayload = recvall(is, len, timeoutMs);
    return new SSLRecord(type, ver, len, messagePayload);
}

From source file:com.kactech.otj.Utils.java

public static String open(byte[] encryptedEnvelope, PrivateKey privateKey)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException {
    String str;//from   ww w. j  a  va 2s  . c o m
    byte[] by;
    ByteBuffer buff = ByteBuffer.wrap(encryptedEnvelope);
    buff.order(ByteOrder.BIG_ENDIAN);
    int envType = buff.getShort();// expected 1(asymmetric)
    if (envType != 1)
        throw new UnsupportedOperationException("unexpected envelope type " + envType);
    int arraySize = buff.getInt();// can result in negative integer but not expecting it here
    if (arraySize != 1)//TODO
        throw new UnsupportedOperationException("current code doesn't support multi-nym response");
    byte[] encKeyBytes = null;
    byte[] vectorBytes = null;
    for (int i = 0; i < arraySize; i++) {
        int nymIDLen = buff.getInt();
        by = new byte[nymIDLen];
        buff.get(by);
        String nymID;
        try {
            nymID = new String(by, 0, by.length - 1, Utils.US_ASCII);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } // take nymID W/O trailing \0
          //TODO nymID matching!
        int keyLength = buff.getInt();
        encKeyBytes = new byte[keyLength];
        buff.get(encKeyBytes);
        int vectorLength = buff.getInt();
        vectorBytes = new byte[vectorLength];
        buff.get(vectorBytes);

    }
    byte[] encryptedMsg = new byte[buff.remaining()];
    buff.get(encryptedMsg);

    Cipher cipher;
    try {
        cipher = Cipher.getInstance(WRAP_ALGO);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    cipher.init(Cipher.UNWRAP_MODE, privateKey);
    SecretKeySpec aesKey = (SecretKeySpec) cipher.unwrap(encKeyBytes, "AES", Cipher.SECRET_KEY);
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(vectorBytes));
    by = cipher.doFinal(encryptedMsg);
    try {
        str = new String(by, 0, by.length - 1, Utils.UTF8);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } // w/o trailing \0
    return str;
}

From source file:backtype.storm.messaging.TaskMessage.java

public void deserialize(ByteBuffer packet) {
    if (packet == null)
        return;//from  ww w  . jav a 2 s.  c o m
    _task = packet.getShort();
    remoteTuple = packet.getShort();
    _message = new byte[packet.limit() - 4];
    packet.get(_message);
}