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:MainClass.java

public static void main(String[] args) {
    try {/*w w  w  .  j  av a  2  s.co  m*/
        File aFile = new File("test.txt");

        FileOutputStream outputFile = null;
        outputFile = new FileOutputStream(aFile, true);
        FileChannel outChannel = outputFile.getChannel();

        ByteBuffer buf = ByteBuffer.allocate(200);

        buf.putInt(10).asCharBuffer().put("www.java2s.com");

        buf.position(10).flip();
        outChannel.write(buf);
        buf.clear();

        outputFile.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(100);

    // Put values of different types
    buf.putInt(123);

    // Reset position for reading
    buf.flip();//from ww  w.  ja  v  a2 s .  c om

    // Retrieve the values
    int i = buf.getInt();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putInt(123);

    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    DatagramChannel channel = DatagramChannel.open();
    SocketAddress address = new InetSocketAddress(0);
    DatagramSocket socket = channel.socket();
    socket.bind(address);//from  ww  w .  ja  va 2  s  . c o m

    SocketAddress server = new InetSocketAddress("time-a.nist.gov", 37);
    channel.connect(server);

    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.order(ByteOrder.BIG_ENDIAN);
    // send a byte of data to the server
    buffer.put((byte) 0);
    buffer.flip();
    channel.write(buffer);

    // get the buffer ready to receive data
    buffer.clear();
    // fill the first four bytes with zeros
    buffer.putInt(0);
    channel.read(buffer);
    buffer.flip();

    // convert seconds since 1900 to a java.util.Date
    long secondsSince1900 = buffer.getLong();
    long differenceBetweenEpochs = 2208988800L;
    long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
    long msSince1970 = secondsSince1970 * 1000;
    Date time = new Date(msSince1970);

    System.out.println(time);
}

From source file:io.pcp.parfait.dxm.PcpMmvWriter.java

public static void main(String[] args) throws IOException {
    PcpMmvWriter bridge;/*  w ww. ja v a  2 s  .  c  o  m*/

    if (args.length == 0) {
        // use $PCP_PMDAS_DIR/mmv/mmvdump (no args) as diagnostic tool
        bridge = new PcpMmvWriter("test", IdentifierSourceSet.DEFAULT_SET);
    } else {
        bridge = new PcpMmvWriter(new File(args[0]), IdentifierSourceSet.DEFAULT_SET);
    }

    // Automatically uses default int handler
    bridge.addMetric(MetricName.parse("sheep[baabaablack].bagsfull.count"), Semantics.COUNTER,
            ONE.multiply(1000), 3);

    // Automatically uses default boolean-to-int handler
    bridge.addMetric(MetricName.parse("sheep[baabaablack].bagsfull.haveany"), Semantics.INSTANT, null,
            new AtomicBoolean(true));
    bridge.addMetric(MetricName.parse("sheep[limpy].bagsfull.haveany"), Semantics.INSTANT, null,
            new AtomicBoolean(false));

    // Automatically uses default long handler
    bridge.addMetric(MetricName.parse("sheep[insomniac].jumps"), Semantics.COUNTER, ONE, 12345678901234L);

    // Automatically uses default double handler
    bridge.addMetric(MetricName.parse("sheep[limpy].legs.available"), Semantics.DISCRETE, ONE, 0.75);

    // Uses this class' custom String handler
    bridge.addMetric(MetricName.parse("sheep[limpy].jumpitem"), Semantics.DISCRETE, null, "fence");

    // addMetric(GregorianCalendar) would fail, as there's no handler registered by default for
    // GregorianCalendars; use a custom one which puts the year as an int
    bridge.addMetric(MetricName.parse("sheep[insomniac].lastjumped"), Semantics.INSTANT, null,
            new GregorianCalendar(), new AbstractTypeHandler<GregorianCalendar>(MmvMetricType.I32, 4) {
                public void putBytes(ByteBuffer buffer, GregorianCalendar value) {
                    buffer.putInt(value.get(GregorianCalendar.YEAR));
                }
            });

    // addMetric(Date) would fail, as there's no handler registered; register one for all date
    // types from now on
    bridge.registerType(Date.class, new AbstractTypeHandler<Date>(MmvMetricType.I64, 8) {
        public void putBytes(ByteBuffer buffer, Date value) {
            buffer.putLong(value.getTime());
        }
    });
    // These will both use the handler we just registered
    bridge.addMetric(MetricName.parse("cow.how.now"), Semantics.INSTANT, null, new Date());
    bridge.addMetric(MetricName.parse("cow.how.then"), Semantics.INSTANT, null,
            new GregorianCalendar(1990, 1, 1, 12, 34, 56).getTime());

    // Uses units
    bridge.addMetric(MetricName.parse("cow.bytes.total"), Semantics.COUNTER, BYTE, 10000001);
    bridge.addMetric(MetricName.parse("cow.bytes.rate"), Semantics.INSTANT, BYTE.multiply(1024).divide(SECOND),
            new Date());
    bridge.addMetric(MetricName.parse("cow.bytes.chewtime"), Semantics.INSTANT, HOUR.divide(BYTE), 7);
    bridge.addMetric(MetricName.parse("cow.bytes.jawmotion"), Semantics.INSTANT, KILO(HERTZ), 0.5);

    // Set up some help text
    bridge.setInstanceDomainHelpText("sheep", "sheep in the paddock",
            "List of all the sheep in the paddock. Includes 'baabaablack', 'insomniac' (who likes to jump fences), and 'limpy' the three-legged wonder sheep.");
    bridge.setMetricHelpText("sheep.jumps", "# of jumps done",
            "Number of times the sheep has jumped over its jumpitem");

    // All the metrics are added; write the file
    bridge.start();
    // Metrics are visible to the agent from this point on

    // Sold a bag! Better update the count
    bridge.updateMetric(MetricName.parse("sheep[baabaablack].bagsfull.count"), 2);
    // The fence broke! Need something new to jump over
    bridge.updateMetric(MetricName.parse("sheep[limpy].jumpitem"), "Honda Civic");
    // Values will be reflected in the agent immediately
}

From source file:Main.java

public static void putUnsignedInt(ByteBuffer bb, long value) {
    bb.putInt((int) (value & 0xffffffffL));
}

From source file:Main.java

public static byte[] convertintToBytes(final int i) {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.putInt(i);
    return bb.array();
}

From source file:Main.java

public static void putUnsignedInt(final ByteBuffer pByteBuffer, final long pValue) {
    pByteBuffer.putInt((int) (pValue & 0xFFFFFFFFL));
}

From source file:Main.java

public static byte[] toArray(int value) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putInt(value);
    return buffer.array();
}

From source file:Main.java

/**
 * Converts an int value into an array of 4 bytes.
 *
 * @param x The int./*from ww  w  . j ava  2s . com*/
 * @return The bytes.
 */
public static byte[] intToBytes(final int x) {
    final ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.putInt(x);
    return buffer.array();
}