Example usage for java.nio ByteBuffer putShort

List of usage examples for java.nio ByteBuffer putShort

Introduction

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

Prototype

public abstract ByteBuffer putShort(short value);

Source Link

Document

Writes the given short to the current position and increases the position by 2.

Usage

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  va2  s  .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 ww w.  j  a v a2 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();/*w ww .jav  a  2 s. c om*/
        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.onlab.packet.DhcpTest.java

@Before
public void setUp() {
    hostNameOption.setCode((byte) 55);
    hostNameOption.setLength((byte) hostName.length());
    hostNameOption.setData(hostName.getBytes(Charsets.US_ASCII));

    // Packet length is the fixed DHCP header plus option length plus an
    // extra byte to indicate 'end of options'.
    ByteBuffer bb = ByteBuffer.allocate(DHCP.MIN_HEADER_LENGTH + 2 + hostNameOption.getLength() + 1);

    bb.put(opCode);//from   w  ww . j a va 2s  .  co m
    bb.put(hardwareType);
    bb.put(hardwareAddressLength);
    bb.put(hops);
    bb.putInt(transactionId);
    bb.putShort(seconds);
    bb.putShort(flags);
    bb.putInt(clientIpAddress);
    bb.putInt(yourIpAddress);
    bb.putInt(serverIpAddress);
    bb.putInt(gatewayIpAddress);
    bb.put(clientHardwareAddress);

    // need 16 bytes of zeros to pad out the client hardware address field
    bb.put(new byte[16 - hardwareAddressLength]);

    // Put server name and pad out to 64 bytes
    bb.put(serverName.getBytes(Charsets.US_ASCII));
    bb.put(new byte[64 - serverName.length()]);

    // Put boot file name and pad out to 128 bytes
    bb.put(bootFileName.getBytes(Charsets.US_ASCII));
    bb.put(new byte[128 - bootFileName.length()]);

    // Magic cookie
    bb.put("DHCP".getBytes(Charsets.US_ASCII));

    bb.put(hostNameOption.getCode());
    bb.put(hostNameOption.getLength());
    bb.put(hostNameOption.getData());

    // End of options marker
    bb.put((byte) (0xff & 255));

    byteHeader = bb.array();
}

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

@SuppressWarnings("deprecation")
@Test//from w ww.j  a  v  a2  s . c o m
public void testFilterList() throws Exception {
    String cf = "f";
    String table = "TestFuzzyRowFiltersInFilterList";
    HTable ht = TEST_UTIL.createTable(TableName.valueOf(table), Bytes.toBytes(cf), Integer.MAX_VALUE);

    // 10 byte row key - (2 bytes 4 bytes 4 bytes)
    // 4 byte qualifier
    // 4 byte value

    for (int i1 = 0; i1 < 5; i1++) {
        for (int i2 = 0; i2 < 5; i2++) {
            byte[] rk = new byte[10];

            ByteBuffer buf = ByteBuffer.wrap(rk);
            buf.clear();
            buf.putShort((short) 2);
            buf.putInt(i1);
            buf.putInt(i2);

            // Each row contains 5 columns
            for (int c = 0; c < 5; c++) {
                byte[] cq = new byte[4];
                Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);

                Put p = new Put(rk);
                p.setDurability(Durability.SKIP_WAL);
                p.add(cf.getBytes(), cq, Bytes.toBytes(c));
                ht.put(p);
                LOG.info("Inserting: rk: " + Bytes.toStringBinary(rk) + " cq: " + Bytes.toStringBinary(cq));
            }
        }
    }

    TEST_UTIL.flush();

    // test passes if we get back 5 KV's (1 row)
    runTest(ht, 5);

}

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

@SuppressWarnings("deprecation")
@Test// ww w  .j  a  v a2s.c o m
public void testFilterList() throws Exception {
    String cf = "f";
    String table = "TestFuzzyRowFiltersInFilterList";
    Table ht = TEST_UTIL.createTable(TableName.valueOf(table), Bytes.toBytes(cf), Integer.MAX_VALUE);

    // 10 byte row key - (2 bytes 4 bytes 4 bytes)
    // 4 byte qualifier
    // 4 byte value

    for (int i1 = 0; i1 < 5; i1++) {
        for (int i2 = 0; i2 < 5; i2++) {
            byte[] rk = new byte[10];

            ByteBuffer buf = ByteBuffer.wrap(rk);
            buf.clear();
            buf.putShort((short) 2);
            buf.putInt(i1);
            buf.putInt(i2);

            // Each row contains 5 columns
            for (int c = 0; c < 5; c++) {
                byte[] cq = new byte[4];
                Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);

                Put p = new Put(rk);
                p.setDurability(Durability.SKIP_WAL);
                p.add(cf.getBytes(), cq, Bytes.toBytes(c));
                ht.put(p);
                LOG.info("Inserting: rk: " + Bytes.toStringBinary(rk) + " cq: " + Bytes.toStringBinary(cq));
            }
        }
    }

    TEST_UTIL.flush();

    // test passes if we get back 5 KV's (1 row)
    runTest(ht, 5);

}

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

@Test
public void testEndToEnd() throws Exception {
    String cf = "f";

    HTable ht = TEST_UTIL.createTable(TableName.valueOf(table), Bytes.toBytes(cf), Integer.MAX_VALUE);

    // 10 byte row key - (2 bytes 4 bytes 4 bytes)
    // 4 byte qualifier
    // 4 byte value

    for (int i0 = 0; i0 < firstPartCardinality; i0++) {

        for (int i1 = 0; i1 < secondPartCardinality; i1++) {

            for (int i2 = 0; i2 < thirdPartCardinality; i2++) {
                byte[] rk = new byte[10];

                ByteBuffer buf = ByteBuffer.wrap(rk);
                buf.clear();/*  w w w .java2  s .c  o  m*/
                buf.putShort((short) i0);
                buf.putInt(i1);
                buf.putInt(i2);
                for (int c = 0; c < colQualifiersTotal; c++) {
                    byte[] cq = new byte[4];
                    Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);

                    Put p = new Put(rk);
                    p.setDurability(Durability.SKIP_WAL);
                    p.add(cf.getBytes(), cq, Bytes.toBytes(c));
                    ht.put(p);
                }
            }
        }
    }

    TEST_UTIL.flush();

    // test passes
    runTest1(ht);
    runTest2(ht);

}

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

@Test
public void testEndToEnd() throws Exception {
    String cf = "f";

    Table ht = TEST_UTIL.createTable(TableName.valueOf(table), Bytes.toBytes(cf), Integer.MAX_VALUE);

    // 10 byte row key - (2 bytes 4 bytes 4 bytes)
    // 4 byte qualifier
    // 4 byte value

    for (int i0 = 0; i0 < firstPartCardinality; i0++) {

        for (int i1 = 0; i1 < secondPartCardinality; i1++) {

            for (int i2 = 0; i2 < thirdPartCardinality; i2++) {
                byte[] rk = new byte[10];

                ByteBuffer buf = ByteBuffer.wrap(rk);
                buf.clear();//from w w w .  j a v a  2 s .com
                buf.putShort((short) i0);
                buf.putInt(i1);
                buf.putInt(i2);
                for (int c = 0; c < colQualifiersTotal; c++) {
                    byte[] cq = new byte[4];
                    Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);

                    Put p = new Put(rk);
                    p.setDurability(Durability.SKIP_WAL);
                    p.add(cf.getBytes(), cq, Bytes.toBytes(c));
                    ht.put(p);
                }
            }
        }
    }

    TEST_UTIL.flush();

    // test passes
    runTest1(ht);
    runTest2(ht);

}

From source file:com.offbynull.portmapper.pcp.PcpRequest.java

/**
 * Dump this PCP request in to a byte buffer.
 * @param dst byte buffer to dump to/*from www  . ja v a 2  s  . com*/
 * @param selfAddress IP address of this machine on the interface used to access the PCP server
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferOverflowException if {@code dst} doesn't have enough space to write this option
 * @throws ReadOnlyBufferException if {@code dst} is read-only
 */
public final void dump(ByteBuffer dst, InetAddress selfAddress) {
    Validate.notNull(dst);
    Validate.notNull(selfAddress);

    dst.put((byte) 2);
    dst.put((byte) op); // topmost bit should be 0, because op is between 0 to 127, which means r-flag = 0
    dst.putShort((short) 0);
    dst.putInt((int) lifetime);

    byte[] selfAddressArr = selfAddress.getAddress();
    switch (selfAddressArr.length) {
    case 4: {
        // convert ipv4 address to ipv4-mapped ipv6 address
        for (int i = 0; i < 10; i++) {
            dst.put((byte) 0);
        }

        for (int i = 0; i < 2; i++) {
            dst.put((byte) 0xff);
        }

        dst.put(selfAddressArr);
        break;
    }
    case 16: {
        dst.put(selfAddressArr);
        break;
    }
    default:
        throw new IllegalArgumentException(); // should never happen
    }

    dumpOpCodeSpecificInformation(dst);

    for (PcpOption option : options) {
        option.dump(dst);
    }
}

From source file:org.cosmo.common.util.Util.java

public static long shortsToLong2(short[] shorts) {
    ByteBuffer b = ByteBuffer.allocate(8);
    b.putShort(shorts[0]);
    b.putShort(shorts[1]);/*ww w.j  a  va  2s.  com*/
    b.putShort(shorts[2]);
    b.putShort(shorts[3]);
    b.rewind();
    return b.getLong();
}