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:org.apache.druid.indexer.IndexGeneratorJobTest.java

private void writeDataToLocalSequenceFile(File outputFile, List<String> data) throws IOException {
    Configuration conf = new Configuration();
    LocalFileSystem fs = FileSystem.getLocal(conf);
    Writer fileWriter = SequenceFile.createWriter(fs, conf, new Path(outputFile.getAbsolutePath()),
            BytesWritable.class, BytesWritable.class, SequenceFile.CompressionType.NONE,
            (CompressionCodec) null);/*from  w  ww .  j av  a  2s  .  c o  m*/

    int keyCount = 10;
    for (String line : data) {
        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.putInt(keyCount);
        BytesWritable key = new BytesWritable(buf.array());
        BytesWritable value = new BytesWritable(StringUtils.toUtf8(line));
        fileWriter.append(key, value);
        keyCount += 1;
    }

    fileWriter.close();
}

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/*w w w.  j a v a 2 s .co  m*/
 * @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.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();// ww w  .  j  a  va  2 s  .co  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 av a  2s .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.yarn.server.nodemanager.containermanager.TestAuxServices.java

@Test
public void testAuxEventDispatch() {
    Configuration conf = new Configuration();
    conf.setStrings(YarnConfiguration.NM_AUX_SERVICES, new String[] { "Asrv", "Bsrv" });
    conf.setClass(String.format(YarnConfiguration.NM_AUX_SERVICE_FMT, "Asrv"), ServiceA.class, Service.class);
    conf.setClass(String.format(YarnConfiguration.NM_AUX_SERVICE_FMT, "Bsrv"), ServiceB.class, Service.class);
    conf.setInt("A.expected.init", 1);
    conf.setInt("B.expected.stop", 1);
    final AuxServices aux = new AuxServices();
    aux.init(conf);/*  w ww .  j  av  a2 s  . co  m*/
    aux.start();

    ApplicationId appId1 = ApplicationId.newInstance(0, 65);
    ByteBuffer buf = ByteBuffer.allocate(6);
    buf.putChar('A');
    buf.putInt(65);
    buf.flip();
    AuxServicesEvent event = new AuxServicesEvent(AuxServicesEventType.APPLICATION_INIT, "user0", appId1,
            "Asrv", buf, "user0Folder");
    aux.handle(event);
    ApplicationId appId2 = ApplicationId.newInstance(0, 66);
    event = new AuxServicesEvent(AuxServicesEventType.APPLICATION_STOP, "user0", appId2, "Bsrv", null,
            "user0Folder");
    // verify all services got the stop event 
    aux.handle(event);
    Collection<AuxiliaryService> servs = aux.getServices();
    for (AuxiliaryService serv : servs) {
        ArrayList<Integer> appIds = ((LightService) serv).getAppIdsStopped();
        assertEquals("app not properly stopped", 1, appIds.size());
        assertTrue("wrong app stopped", appIds.contains((Integer) 66));
    }

    for (AuxiliaryService serv : servs) {
        assertNull(((LightService) serv).containerId);
        assertNull(((LightService) serv).resource);
    }

    ApplicationAttemptId attemptId = ApplicationAttemptId.newInstance(appId1, 1);
    ContainerTokenIdentifier cti = new ContainerTokenIdentifier(ContainerId.newContainerId(attemptId, 1), "",
            "", Resource.newInstance(1, 1), 0, 0, 0, Priority.newInstance(0), 0, "");
    Context context = mock(Context.class);
    Container container = new ContainerImpl(null, null, null, null, null, cti, context);
    ContainerId containerId = container.getContainerId();
    Resource resource = container.getResource();
    event = new AuxServicesEvent(AuxServicesEventType.CONTAINER_INIT, container);
    aux.handle(event);
    for (AuxiliaryService serv : servs) {
        assertEquals(containerId, ((LightService) serv).containerId);
        assertEquals(resource, ((LightService) serv).resource);
        ((LightService) serv).containerId = null;
        ((LightService) serv).resource = null;
    }

    event = new AuxServicesEvent(AuxServicesEventType.CONTAINER_STOP, container);
    aux.handle(event);
    for (AuxiliaryService serv : servs) {
        assertEquals(containerId, ((LightService) serv).containerId);
        assertEquals(resource, ((LightService) serv).resource);
    }
}

From source file:org.onlab.packet.IPv4Test.java

@Before
public void setUp() throws Exception {
    deserializer = IPv4.deserializer();/*  w  w w.j  a v a2  s.  c o m*/

    ByteBuffer bb = ByteBuffer.allocate(headerLength * 4);

    bb.put((byte) ((version & 0xf) << 4 | headerLength & 0xf));
    bb.put(diffServ);
    bb.putShort(totalLength);
    bb.putShort(identification);
    bb.putShort((short) ((flags & 0x7) << 13 | fragmentOffset & 0x1fff));
    bb.put(ttl);
    bb.put(protocol);
    bb.putShort(checksum);
    bb.putInt(sourceAddress);
    bb.putInt(destinationAddress);
    bb.put(options);

    headerBytes = bb.array();
}

From source file:com.yobidrive.diskmap.needles.Needle.java

public void putNeedleInBuffer(ByteBuffer result) throws Exception {
    int startPosition = result.position();
    result.limit(result.capacity());/*w  w w  . ja v a 2 s. c  o  m*/
    result.putInt(MAGICSTART);
    result.putLong(needleNumber);
    result.put(flags);
    result.putInt(keyBytes.length);
    result.put(keyBytes);
    result.putInt(version == null ? 0 : version.toBytes().length);
    if (version != null)
        result.put(version.toBytes());
    result.putInt(previousNeedle == null ? -1 : previousNeedle.getNeedleFileNumber()); // Chaining
    result.putLong(previousNeedle == null ? -1L : previousNeedle.getNeedleOffset()); // Chaining
    result.putInt(originalFileNumber); // Original needle location (for cleaning)
    result.putInt(originalSize); // Original needle size (for cleaning)
    result.putInt(data == null ? 0 : data.length);
    if (data != null)
        result.put(data);
    result.putInt(MAGICEND);
    result.put(hashMD5());
    while (((result.position() - startPosition) % 256) > 0) {
        result.put(PADDING);
    }
    result.flip();
}

From source file:edu.tsinghua.lumaqq.qq.packets.out._05.TransferPacket.java

@Override
protected void putBody(ByteBuffer buf) {
    if (!requestSend) {
        // 2. 8/*from w ww  .ja v  a2 s  .co m*/
        buf.putLong(0x0100000000000000L);
        // 3. session id, 4           
        buf.putInt(sessionId);
        // 4
        buf.putInt(0);
        // ??
        if (dataReply) {
            buf.putChar((char) 0x0001);
            buf.put((byte) 0x02);
        } else {
            buf.putChar((char) 0x04);
            buf.putInt(0);
        }
    } else if (data) {
        // 2. 8??0x1000000000000001?
        if (last)
            buf.putLong(0x0100000000000000L);
        else
            buf.putLong(0x0100000000000001L);
        // 3. session id, 4
        buf.putInt(sessionId);
        // 4. 4
        buf.putInt(0);
        // 5. ?2
        buf.putChar((char) fragment.length);
        // 6. ?
        buf.put(fragment);
    } else {
        // 2. 8
        buf.putLong(0x0100000000000000L);
        // 3. session id, 4
        buf.putInt(sessionId);
        // 4. 4
        buf.putInt(0);
        // 5. ???2
        buf.putChar((char) 0);
        // 6. 25?
        int pos = buf.position();
        buf.putChar((char) 0);
        // 7. md5
        buf.put(md5);
        // 8. ??md5
        byte[] fileNameBytes = fileName.getBytes();
        buf.put(md5(fileNameBytes));
        // 9. 4
        buf.putInt(imageLength);
        // 10. ??2
        buf.putChar((char) fileName.length());
        // 11. ??
        buf.put(fileNameBytes);
        // 12. 8
        buf.putLong(0);

        char len = (char) (buf.position() - pos);
        buf.putChar(pos - 2, len);
        buf.putChar(pos, len);
    }
}

From source file:org.kalypso.grid.BinaryGeoGrid.java

private void saveStatistically() throws GeoGridException {
    final BigDecimal min = getMin();
    final BigDecimal max = getMax();

    try {//www .j a v  a 2s  .c o m
        m_unscaledMin = unscaleValue(min.doubleValue());
        m_unscaledMax = unscaleValue(max.doubleValue());

        /* directly write into buffer */
        final long pos = BinaryGeoGridHeader.HEADER_SIZE + getSizeX() * getSizeY() * 4;

        final ByteBuffer buffer = ByteBuffer.allocate(8);

        buffer.putInt(m_unscaledMin);
        buffer.putInt(m_unscaledMax);

        m_channel.position(pos);
        buffer.rewind();
        m_channel.write(buffer);
    } catch (final IOException e) {
        throw new GeoGridException("Failed to set statistical data", e);
    }
}

From source file:org.getspout.spoutapi.packet.PacketBlockData.java

public PacketBlockData(Set<Block> modifiedData) {
    if (modifiedData.size() > 0) {
        ByteBuffer rawData = ByteBuffer.allocate(modifiedData.size() * (15));

        Iterator<Block> i = modifiedData.iterator();
        while (i.hasNext()) {
            Block next = i.next();//w ww .  jav  a  2 s  .com

            rawData.put((byte) next.getRawId());
            rawData.put((byte) next.getRawData());
            rawData.putFloat(next.getHardness());
            rawData.putInt(next.getLightLevel());
            rawData.putFloat(next.getFriction());
            rawData.put((byte) (next.isOpaque() ? 1 : 0));
        }

        data = rawData.array();
    }
}