Example usage for java.nio ByteBuffer putInt

List of usage examples for java.nio ByteBuffer putInt

Introduction

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

Prototype

public abstract ByteBuffer putInt(int value);

Source Link

Document

Writes the given int to the current position and increases the position by 4.

Usage

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * put a file, its meta-data, and retrieval id on the server
 * //from ww w. ja  v a  2 s.co m
 * @throws MessageNotFoundException
 */
@Deprecated
public byte[] postFileAppleUA(byte[] msgHashBytes, byte[] msgData, byte[] fileData, String recipientToken)
        throws ExchangeException, MessageNotFoundException {

    handleSizeRestictions(fileData);

    int capacity = mVersionLen //
            + 4 + msgHashBytes.length //
            + 4 + recipientToken.length() //
            + 4 + msgData.length //
            + 4 + fileData.length;
    ByteBuffer msg = ByteBuffer.allocate(capacity);
    msg.putInt(mVersion);
    msg.putInt(msgHashBytes.length);
    msg.put(msgHashBytes);
    msg.putInt(recipientToken.length());
    msg.put(recipientToken.getBytes());
    msg.putInt(msgData.length);
    msg.put(msgData);
    msg.putInt(fileData.length);
    msg.put(fileData);

    mNotRegistered = false;

    byte[] resp = doPost(mUrlPrefix + mHost + "/postFile2" + mUrlSuffix, msg.array());

    mNotRegistered = isNotRegisteredErrorCodes(resp);

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * put a message, file, its meta-data, and retrieval id on the server
 * //from   ww  w .  j a  v  a  2s  .  c  o  m
 * @throws MessageNotFoundException
 */
public byte[] postMessage(byte[] msgHashBytes, byte[] msgData, byte[] fileData, String recipientToken,
        int notifyType) throws ExchangeException, MessageNotFoundException {

    handleSizeRestictions(fileData);

    int capacity = mVersionLen //
            + 4 + msgHashBytes.length //
            + 4 + recipientToken.length() //
            + 4 + msgData.length //
            + 4 + fileData.length //
            + 4;
    ByteBuffer msg = ByteBuffer.allocate(capacity);
    msg.putInt(mVersion);
    msg.putInt(msgHashBytes.length);
    msg.put(msgHashBytes);
    msg.putInt(recipientToken.length());
    msg.put(recipientToken.getBytes());
    msg.putInt(msgData.length);
    msg.put(msgData);
    msg.putInt(fileData.length);
    msg.put(fileData);
    msg.putInt(notifyType);

    mNotRegistered = false;

    byte[] resp = doPost(mUrlPrefix + mHost + "/postMessage" + mUrlSuffix, msg.array());

    mNotRegistered = isNotRegisteredErrorCodes(resp);

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:com.openteach.diamond.network.waverider.network.Packet.java

/**
 * ByteBuffer//from w w w .java  2 s  . co  m
 * @return
 */
public ByteBuffer marshall() {
    int size = getSize();
    ByteBuffer buffer = ByteBuffer.allocate(size);
    buffer.put(magic.getBytes());
    buffer.putLong(sequence);
    buffer.putLong(type);
    buffer.putInt(size);
    buffer.put(payLoad);
    buffer.flip();
    return buffer;
}

From source file:gridool.memcached.gateway.MemcachedProxyHandler.java

@Override
public short handleSet(byte[] key, byte[] value, int flags, int expiry) {
    final ByteBuffer reqPacket = ByteBuffer
            .allocate(HEADER_LENGTH + SET_EXTRA_LENGTH + key.length + value.length);
    // request header
    Header header = new Header(MAGIC_BYTE_REQUEST, OPCODE_SET);
    header.setBodyLength(SET_EXTRA_LENGTH, key.length, value.length);
    header.encode(reqPacket);//from w w  w .  j  av a  2 s .c o  m
    // request body (flags, expiration, key, value)
    reqPacket.putInt(flags);
    reqPacket.putInt(expiry);
    reqPacket.put(key);
    reqPacket.put(value);
    reqPacket.flip();

    final ByteBuffer resPacket = ByteBuffer.allocate(HEADER_LENGTH);
    final SocketAddress sockAddr = getSocket(key);
    final ByteChannel channel = sockPool.borrowObject(sockAddr);
    try {
        NIOUtils.writeFully(channel, reqPacket);
        NIOUtils.readFully(channel, resPacket);
    } catch (IOException e) {
        LOG.error(e);
        return ResponseStatus.UNKNOWN.status;
    } finally {
        sockPool.returnObject(sockAddr, channel);
    }
    resPacket.flip();
    short status = resPacket.getShort(6);
    return status;
}

From source file:org.apache.kylin.storage.hbase.cube.v1.filter.TestFuzzyRowFilterV2EndToEnd.java

private void runTest1(HTable hTable) throws IOException {
    // [0, 2, ?, ?, ?, ?, 0, 0, 0, 1]

    byte[] mask = new byte[] { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 };

    List<Pair<byte[], byte[]>> list = new ArrayList<Pair<byte[], byte[]>>();
    for (int i = 0; i < totalFuzzyKeys; i++) {
        byte[] fuzzyKey = new byte[10];
        ByteBuffer buf = ByteBuffer.wrap(fuzzyKey);
        buf.clear();//from  w  ww. j  a  va 2s.  c  om
        buf.putShort((short) 2);
        for (int j = 0; j < 4; j++) {
            buf.put(fuzzyValue);
        }
        buf.putInt(i);

        Pair<byte[], byte[]> pair = Pair.newPair(fuzzyKey, mask);
        list.add(pair);
    }

    int expectedSize = secondPartCardinality * totalFuzzyKeys * colQualifiersTotal;
    FuzzyRowFilterV2 fuzzyRowFilter0 = new FuzzyRowFilterV2(list);
    // Filters are not stateless - we can't reuse them
    FuzzyRowFilterV2 fuzzyRowFilter1 = new FuzzyRowFilterV2(list);

    // regular test
    runScanner(hTable, expectedSize, fuzzyRowFilter0);
    // optimized from block cache
    runScanner(hTable, expectedSize, fuzzyRowFilter1);

}

From source file:org.apache.hadoop.hbase.filter.TestFuzzyRowFilterEndToEnd.java

private void runTest1(Table hTable) throws IOException {
    // [0, 2, ?, ?, ?, ?, 0, 0, 0, 1]

    byte[] mask = new byte[] { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 };

    List<Pair<byte[], byte[]>> list = new ArrayList<Pair<byte[], byte[]>>();
    for (int i = 0; i < totalFuzzyKeys; i++) {
        byte[] fuzzyKey = new byte[10];
        ByteBuffer buf = ByteBuffer.wrap(fuzzyKey);
        buf.clear();//from  w w  w  .j  a v a  2s  .c o  m
        buf.putShort((short) 2);
        for (int j = 0; j < 4; j++) {
            buf.put(fuzzyValue);
        }
        buf.putInt(i);

        Pair<byte[], byte[]> pair = new Pair<byte[], byte[]>(fuzzyKey, mask);
        list.add(pair);
    }

    int expectedSize = secondPartCardinality * totalFuzzyKeys * colQualifiersTotal;
    FuzzyRowFilter fuzzyRowFilter0 = new FuzzyRowFilter(list);
    // Filters are not stateless - we can't reuse them
    FuzzyRowFilter fuzzyRowFilter1 = new FuzzyRowFilter(list);

    // regular test
    runScanner(hTable, expectedSize, fuzzyRowFilter0);
    // optimized from block cache
    runScanner(hTable, expectedSize, fuzzyRowFilter1);

}

From source file:org.apache.kylin.storage.hbase.cube.v1.filter.TestFuzzyRowFilterV2EndToEnd.java

private void runTest2(HTable hTable) throws IOException {
    // [0, 0, ?, ?, ?, ?, 0, 0, 0, 0] , [0, 1, ?, ?, ?, ?, 0, 0, 0, 1]...

    byte[] mask = new byte[] { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 };

    List<Pair<byte[], byte[]>> list = new ArrayList<Pair<byte[], byte[]>>();

    for (int i = 0; i < totalFuzzyKeys; i++) {
        byte[] fuzzyKey = new byte[10];
        ByteBuffer buf = ByteBuffer.wrap(fuzzyKey);
        buf.clear();//from  w  w w . j a  va2  s.co m
        buf.putShort((short) (i * 2));
        for (int j = 0; j < 4; j++) {
            buf.put(fuzzyValue);
        }
        buf.putInt(i * 2);

        Pair<byte[], byte[]> pair = Pair.newPair(fuzzyKey, mask);
        list.add(pair);
    }

    int expectedSize = totalFuzzyKeys * secondPartCardinality * colQualifiersTotal;

    FuzzyRowFilterV2 fuzzyRowFilter0 = new FuzzyRowFilterV2(list);
    // Filters are not stateless - we can't reuse them
    FuzzyRowFilterV2 fuzzyRowFilter1 = new FuzzyRowFilterV2(list);

    // regular test
    runScanner(hTable, expectedSize, fuzzyRowFilter0);
    // optimized from block cache
    runScanner(hTable, expectedSize, fuzzyRowFilter1);

}

From source file:org.apache.hadoop.hbase.filter.TestFuzzyRowFilterEndToEnd.java

private void runTest2(Table hTable) throws IOException {
    // [0, 0, ?, ?, ?, ?, 0, 0, 0, 0] , [0, 1, ?, ?, ?, ?, 0, 0, 0, 1]...

    byte[] mask = new byte[] { 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 };

    List<Pair<byte[], byte[]>> list = new ArrayList<Pair<byte[], byte[]>>();

    for (int i = 0; i < totalFuzzyKeys; i++) {
        byte[] fuzzyKey = new byte[10];
        ByteBuffer buf = ByteBuffer.wrap(fuzzyKey);
        buf.clear();/*from   w  w w. ja  v a2s .co m*/
        buf.putShort((short) (i * 2));
        for (int j = 0; j < 4; j++) {
            buf.put(fuzzyValue);
        }
        buf.putInt(i * 2);

        Pair<byte[], byte[]> pair = new Pair<byte[], byte[]>(fuzzyKey, mask);
        list.add(pair);
    }

    int expectedSize = totalFuzzyKeys * secondPartCardinality * colQualifiersTotal;

    FuzzyRowFilter fuzzyRowFilter0 = new FuzzyRowFilter(list);
    // Filters are not stateless - we can't reuse them
    FuzzyRowFilter fuzzyRowFilter1 = new FuzzyRowFilter(list);

    // regular test
    runScanner(hTable, expectedSize, fuzzyRowFilter0);
    // optimized from block cache
    runScanner(hTable, expectedSize, fuzzyRowFilter1);

}

From source file:org.carbondata.processing.restructure.SchemaRestructurer.java

private static ByteBuffer getMemberByteBufferWithoutDefaultValue(String defaultValue) {
    int minValue = 1;
    int rowLength = 8;
    boolean enableEncoding = Boolean
            .valueOf(CarbonProperties.getInstance().getProperty(CarbonCommonConstants.ENABLE_BASE64_ENCODING,
                    CarbonCommonConstants.ENABLE_BASE64_ENCODING_DEFAULT));
    ByteBuffer buffer = null;
    byte[] data = null;
    if (enableEncoding) {
        try {//from  ww w  . j a  v  a2s  .c o m
            data = Base64.encodeBase64(CarbonCommonConstants.MEMBER_DEFAULT_VAL.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            data = Base64.encodeBase64(CarbonCommonConstants.MEMBER_DEFAULT_VAL.getBytes());
        }
    } else {
        try {
            data = CarbonCommonConstants.MEMBER_DEFAULT_VAL.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            data = CarbonCommonConstants.MEMBER_DEFAULT_VAL.getBytes();
        }

    }
    rowLength += 4;
    rowLength += data.length;
    if (null == defaultValue) {
        buffer = ByteBuffer.allocate(rowLength);
        buffer.putInt(minValue);
        buffer.putInt(data.length);
        buffer.put(data);
        buffer.putInt(minValue);
    } else {
        byte[] data1 = null;
        if (enableEncoding) {
            try {
                data1 = Base64.encodeBase64(defaultValue.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                data1 = Base64.encodeBase64(defaultValue.getBytes());
            }
        } else {
            try {
                data1 = defaultValue.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                data1 = defaultValue.getBytes();
            }

        }
        rowLength += 4;
        rowLength += data1.length;
        buffer = ByteBuffer.allocate(rowLength);
        buffer.putInt(minValue);
        buffer.putInt(data.length);
        buffer.put(data);
        buffer.putInt(data1.length);
        buffer.put(data1);
        buffer.putInt(2);
    }
    buffer.flip();
    return buffer;
}

From source file:de.hpi.fgis.hdrs.Triple.java

public static void writeTripleHeader(ByteBuffer out, Triple t) {
    out.putShort(t.getSubjectLength());//from  w w  w.ja  v a  2  s. c  o m
    out.putShort(t.getPredicateLength());
    out.putInt(t.getObjectLength());
    out.putInt(t.getMultiplicity());
}