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.jmangos.sniffer.handler.PKTLogHandler.java

@Override
public void init() {
    final String date = String.format("%X", System.currentTimeMillis());
    final ByteBuffer buffer = ByteBuffer.allocate(66);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put("PKT".getBytes());
    buffer.put((byte) 1);
    buffer.put((byte) 3);
    buffer.put((byte) 12);
    buffer.putInt(this.build);
    buffer.put("xxXX".getBytes());
    buffer.put(this.keyReader.getKey());
    buffer.putInt((int) (System.currentTimeMillis() / 1000L));
    buffer.putInt(0);//from   w  ww . jav a  2 s .  c om
    buffer.putInt(0);
    try {
        this.fous = new DataOutputStream(new FileOutputStream(new File(this.build + "_" + date + ".pkt")));
        this.fous.write(buffer.array());
        setInit(true);
    } catch (final FileNotFoundException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

From source file:com.bigdata.dastor.db.RowMutation.java

public void delete(QueryPath path, long timestamp) {
    assert path.columnFamilyName != null;
    String cfName = path.columnFamilyName;

    int localDeleteTime = (int) (System.currentTimeMillis() / 1000);

    ColumnFamily columnFamily = modifications_.get(cfName);
    if (columnFamily == null)
        columnFamily = ColumnFamily.create(table_, cfName);

    if (path.superColumnName == null && path.columnName == null) {
        columnFamily.delete(localDeleteTime, timestamp);
    } else if (path.columnName == null) {
        SuperColumn sc = new SuperColumn(path.superColumnName,
                DatabaseDescriptor.getSubComparator(table_, cfName));
        sc.markForDeleteAt(localDeleteTime, timestamp);
        columnFamily.addColumn(sc);//www  .j a va2  s  . com
    } else {
        ByteBuffer bytes = ByteBuffer.allocate(4);
        bytes.putInt(localDeleteTime);
        columnFamily.addColumn(path, bytes.array(), timestamp, true);
    }

    modifications_.put(cfName, columnFamily);
}

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

public ByteBuffer toBytes(ByteBuffer bbuf) throws UnsupportedEncodingException {
    super.toBytes(bbuf);
    assert (bbuf.position() == SIZEOF_PAXOSPACKET_FIXED + this.getPaxosID().getBytes(CHARSET).length);
    bbuf.putInt(this.acceptor).putInt(this.ballot.ballotNumber).putInt(this.ballot.coordinatorID)
            .putInt(this.slotNumber).putInt(this.maxCheckpointedSlot).putLong(this.requestID)
            .put(this.isUndigestRequest() ? (byte) 1 : (byte) 0);
    return bbuf;//from   ww  w .  jav  a 2  s  .  c o  m
}

From source file:xbird.storage.io.RemoteVarSegments.java

private void sendRequest(final ByteChannel channel, final long... idxs) throws IOException {
    byte[] bFilePath = StringUtils.getBytes(_filePath);
    final int size = idxs.length;
    int buflen = bFilePath.length + 16 + (Primitives.LONG_BYTES * size);//+ 4 + 4 + 4 + 4 + 8n;
    if (buflen > RemotePagingService.MAX_COMMAND_BUFLEN) {
        throw new IllegalStateException("command size exceeds limit in MAX_COMMAND_BUFLEN("
                + RemotePagingService.MAX_COMMAND_BUFLEN + "): " + buflen + " bytes");
    }/*from   w  ww  .j  a  va 2  s  . c o  m*/
    ByteBuffer oprBuf = ByteBuffer.allocate(buflen);
    oprBuf.putInt(buflen - 4);
    oprBuf.putInt(RemotePagingService.COMMAND_TRACK_READ);
    oprBuf.putInt(bFilePath.length); // #1
    oprBuf.put(bFilePath); // #2
    oprBuf.putInt(size); // #3
    for (int i = 0; i < size; i++) {
        oprBuf.putLong(idxs[i]); // #4
    }
    oprBuf.flip();
    NIOUtils.writeFully(channel, oprBuf);
}

From source file:xbird.util.nio.RemoteMemoryMappedFile.java

private void sendRequest(final ByteChannel channel, final long startOffset, final long endOffset,
        final int aryLength) throws IOException {
    byte[] bFilePath = StringUtils.getBytes(_filePath);
    int buflen = bFilePath.length + 29;//+ 4 + 4 + 4 + 8 + 8 + 1;
    if (buflen > RemotePagingService.MAX_COMMAND_BUFLEN) {
        throw new IllegalStateException("command size exceeds limit in MAX_COMMAND_BUFLEN("
                + RemotePagingService.MAX_COMMAND_BUFLEN + "): " + buflen + " bytes");
    }/* w w w  .  j a v  a 2 s  .  c o  m*/
    ByteBuffer oprBuf = ByteBuffer.allocate(buflen);
    oprBuf.putInt(buflen - 4);
    oprBuf.putInt(RemotePagingService.COMMAND_READ);
    oprBuf.putInt(bFilePath.length); // #1
    oprBuf.put(bFilePath); // #2
    oprBuf.putLong(startOffset); // #3
    oprBuf.putLong(endOffset); // #4
    oprBuf.put((byte) (_bigEndian ? 1 : 0)); // # 5 is big endian?
    oprBuf.flip();
    NIOUtils.writeFully(channel, oprBuf);
}

From source file:org.hobbit.core.components.AbstractCommandReceivingComponent.java

/**
 * Sends the given command to the command queue with the given data appended
 * and using the given properties.//  w  w w. j  a v a2  s  .co  m
 *
 * @param command
 *            the command that should be sent
 * @param data
 *            data that should be appended to the command
 * @param props
 *            properties that should be used for the message
 * @throws IOException
 *             if a communication problem occurs
 */
protected void sendToCmdQueue(byte command, byte data[], BasicProperties props) throws IOException {
    byte sessionIdBytes[] = getHobbitSessionId().getBytes(Charsets.UTF_8);
    // + 5 because 4 bytes for the session ID length and 1 byte for the
    // command
    int dataLength = sessionIdBytes.length + 5;
    boolean attachData = (data != null) && (data.length > 0);
    if (attachData) {
        dataLength += data.length;
    }
    ByteBuffer buffer = ByteBuffer.allocate(dataLength);
    buffer.putInt(sessionIdBytes.length);
    buffer.put(sessionIdBytes);
    buffer.put(command);
    if (attachData) {
        buffer.put(data);
    }
    cmdChannel.basicPublish(Constants.HOBBIT_COMMAND_EXCHANGE_NAME, "", props, buffer.array());
}

From source file:io.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);// ww w  .ja v a  2s  .c om

    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(line.getBytes(Charsets.UTF_8));
        fileWriter.append(key, value);
        keyCount += 1;
    }

    fileWriter.close();
}

From source file:org.cryptomator.crypto.aes256.Aes256CryptorTest.java

License:asdf

@Test
public void testPartialDecryption()
        throws IOException, DecryptFailedException, WrongPasswordException, UnsupportedKeyLengthException {
    // our test plaintext data:
    final byte[] plaintextData = new byte[65536 * Integer.BYTES];
    final ByteBuffer bbIn = ByteBuffer.wrap(plaintextData);
    for (int i = 0; i < 65536; i++) {
        bbIn.putInt(i);
    }//from   w w  w. j a v  a  2s  . c o m
    final InputStream plaintextIn = new ByteArrayInputStream(plaintextData);

    // init cryptor:
    final Aes256Cryptor cryptor = new Aes256Cryptor(TEST_PRNG);

    // encrypt:
    final ByteBuffer encryptedData = ByteBuffer.allocate((int) (64 + plaintextData.length * 1.2));
    final SeekableByteChannel encryptedOut = new ByteBufferBackedSeekableChannel(encryptedData);
    cryptor.encryptFile(plaintextIn, encryptedOut);
    IOUtils.closeQuietly(plaintextIn);
    IOUtils.closeQuietly(encryptedOut);

    encryptedData.position(0);

    // decrypt:
    final SeekableByteChannel encryptedIn = new ByteBufferBackedSeekableChannel(encryptedData);
    final ByteArrayOutputStream plaintextOut = new ByteArrayOutputStream();
    final Long numDecryptedBytes = cryptor.decryptRange(encryptedIn, plaintextOut, 25000 * Integer.BYTES,
            30000 * Integer.BYTES);
    IOUtils.closeQuietly(encryptedIn);
    IOUtils.closeQuietly(plaintextOut);
    Assert.assertTrue(numDecryptedBytes > 0);

    // check decrypted data:
    final byte[] result = plaintextOut.toByteArray();
    final byte[] expected = Arrays.copyOfRange(plaintextData, 25000 * Integer.BYTES, 55000 * Integer.BYTES);
    Assert.assertArrayEquals(expected, result);
}

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);//w w  w.j ava  2 s .  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:de.micromata.genome.logging.spi.ifiles.IndexHeader.java

public void writLogEntryPosition(LogWriteEntry lwe, int position, OutputStream idxOut) throws IOException {
    ByteBuffer ibb = ByteBuffer.wrap(new byte[Integer.BYTES]);
    ByteBuffer lbb = ByteBuffer.wrap(new byte[Long.BYTES]);
    lbb.putLong(lwe.getTimestamp());/* w  ww .j a v  a2s .c  o  m*/
    idxOut.write(lbb.array());
    ibb.putInt(position);
    idxOut.write(ibb.array());
    idxOut.flush();
}