Example usage for java.nio ByteBuffer getInt

List of usage examples for java.nio ByteBuffer getInt

Introduction

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

Prototype

public abstract int getInt();

Source Link

Document

Returns the int at the current position and increases the position by 4.

Usage

From source file:net.jenet.BandwidthLimit.java

@Override
public void fromBuffer(ByteBuffer buffer) {
    super.fromBuffer(buffer);
    incomingBandwidth = buffer.getInt();
    outgoingBandwidth = buffer.getInt();
}

From source file:edu.umass.cs.gigapaxos.paxospackets.ProposalPacket.java

protected ProposalPacket(ByteBuffer bbuf) throws UnsupportedEncodingException, UnknownHostException {
    super(bbuf);// w w  w. java  2  s .c om
    this.slot = bbuf.getInt();
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6StatsRequest.java

@Override
public void readFrom(ByteBuffer data) {
    this.vendor = data.getInt();
    this.msgsubtype = data.getInt();
    data.getInt();//pad 4 bytes
    this.outPort = data.getShort();
    this.match_len = data.getShort();
    this.tableId = data.get();
    for (int i = 0; i < 3; i++)
        data.get();//pad byte

}

From source file:us.ihmc.pubsub.types.ByteBufferPubSubType.java

@Override
public void deserialize(SerializedPayload serializedPayload, ByteBuffer data) throws IOException {
    CDR.readEncapsulation(serializedPayload);

    ByteBuffer src = serializedPayload.getData();

    int length = src.getInt();
    if (length > data.remaining()) {
        throw new IOException(
                "ByteBuffer data does not have enough space remaining to accept the incoming message.");
    }//from w  w w . j  a va2  s.  c  om
    for (int i = 0; i < length; i++) {
        data.put(src.get());
    }
}

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);
    }//  ww  w  .j a v a 2  s.  c o  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: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  .co 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:com.dreamworks.dsp.server.EmbeddedSftpServer.java

private PublicKey decodePublicKey() throws Exception {
    InputStream stream = new ClassPathResource("keys/sftp_rsa.pub").getInputStream();
    byte[] decodeBuffer = Base64.decodeBase64(StreamUtils.copyToByteArray(stream));
    ByteBuffer bb = ByteBuffer.wrap(decodeBuffer);
    int len = bb.getInt();
    byte[] type = new byte[len];
    bb.get(type);//w  w  w. ja va 2 s .c o m
    if ("ssh-rsa".equals(new String(type))) {
        BigInteger e = decodeBigInt(bb);
        BigInteger m = decodeBigInt(bb);
        RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);
        return KeyFactory.getInstance("RSA").generatePublic(spec);

    } else {
        throw new IllegalArgumentException("Only supports RSA");
    }
}

From source file:org.apache.hadoop.hbase.KeyValueUtil.java

/**
 * Creates a new KeyValue object positioned in the supplied ByteBuffer and sets the ByteBuffer's
 * position to the start of the next KeyValue. Does not allocate a new array or copy data.
 * @param bb//w  ww. j  a va  2  s.com
 * @param includesMvccVersion
 * @param includesTags
 */
public static KeyValue nextShallowCopy(final ByteBuffer bb, final boolean includesMvccVersion,
        boolean includesTags) {
    if (bb.isDirect()) {
        throw new IllegalArgumentException("only supports heap buffers");
    }
    if (bb.remaining() < 1) {
        return null;
    }
    KeyValue keyValue = null;
    int underlyingArrayOffset = bb.arrayOffset() + bb.position();
    int keyLength = bb.getInt();
    int valueLength = bb.getInt();
    ByteBufferUtils.skip(bb, keyLength + valueLength);
    int tagsLength = 0;
    if (includesTags) {
        // Read short as unsigned, high byte first
        tagsLength = ((bb.get() & 0xff) << 8) ^ (bb.get() & 0xff);
        ByteBufferUtils.skip(bb, tagsLength);
    }
    int kvLength = (int) KeyValue.getKeyValueDataStructureSize(keyLength, valueLength, tagsLength);
    keyValue = new KeyValue(bb.array(), underlyingArrayOffset, kvLength);
    if (includesMvccVersion) {
        long mvccVersion = ByteBufferUtils.readVLong(bb);
        keyValue.setSequenceId(mvccVersion);
    }
    return keyValue;
}

From source file:edu.umass.cs.gigapaxos.paxospackets.AcceptPacket.java

public AcceptPacket(ByteBuffer bbuf) throws UnsupportedEncodingException, UnknownHostException {
    super(bbuf);//from  ww w.java  2s.  c om
    this.sender = bbuf.getInt();
}

From source file:org.openhab.binding.ulux.internal.ump.messages.StateMessage.java

public StateMessage(final short actorId, final ByteBuffer data) {
    super((byte) 0x04, UluxMessageId.State, actorId, data);

    final BigInteger stateFlags = BigInteger.valueOf(data.getInt());

    this.i2cHumidityValid = stateFlags.testBit(25);
    this.i2cTemperatureValid = stateFlags.testBit(24);
    this.motionSensor = stateFlags.testBit(11);
    this.internalError = stateFlags.testBit(7);
    this.initRequest = stateFlags.testBit(6);
    this.timeRequest = stateFlags.testBit(5);
    this.introActive = stateFlags.testBit(4);
    this.audioActive = stateFlags.testBit(3);
    this.displayActive = stateFlags.testBit(2);
    this.proximitySensor = stateFlags.testBit(1);
    this.lightSensor = stateFlags.testBit(0);
}