Example usage for java.nio ByteBuffer getChar

List of usage examples for java.nio ByteBuffer getChar

Introduction

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

Prototype

public abstract char getChar();

Source Link

Document

Returns the char at the current position and increases the position by 2.

Usage

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("Howdy!");
    char c;/*from  ww  w .j a  v  a  2 s. com*/
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("java2s.com");
    char c;/* w  ww. j  a  va2  s  .c om*/
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("Howdy!");
    char c;/*from   w  w  w. j a v  a  2s .c  o m*/
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();

    bb.rewind();
    // Store and read a short:
    bb.asShortBuffer().put((short) 471142);
    System.out.println(bb.getShort());

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("java2s.com");
    bb.rewind();//from   ww  w.ja v a  2 s .  c  om
    char c;
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();

}

From source file:Main.java

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

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

    // Reset position for reading
    buf.flip();/*from  w  w  w .ja va  2s .  c om*/

    // Retrieve the values
    char c = buf.getChar();

}

From source file:GetData.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    // Allocation automatically zeroes the ByteBuffer:
    int i = 0;/*w  w  w .  j  a  va  2 s  .co  m*/
    while (i++ < bb.limit())
        if (bb.get() != 0)
            System.out.println("nonzero");
    System.out.println("i = " + i);
    bb.rewind();
    // Store and read a char array:
    bb.asCharBuffer().put("Howdy!");
    char c;
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();
    bb.rewind();
    // Store and read a short:
    bb.asShortBuffer().put((short) 471142);
    System.out.println(bb.getShort());
    bb.rewind();
    // Store and read an int:
    bb.asIntBuffer().put(99471142);
    System.out.println(bb.getInt());
    bb.rewind();
    // Store and read a long:
    bb.asLongBuffer().put(99471142);
    System.out.println(bb.getLong());
    bb.rewind();
    // Store and read a float:
    bb.asFloatBuffer().put(99471142);
    System.out.println(bb.getFloat());
    bb.rewind();
    // Store and read a double:
    bb.asDoubleBuffer().put(99471142);
    System.out.println(bb.getDouble());
    bb.rewind();

}

From source file:Main.java

public static String getString(ByteBuffer buf, int len) {
    char[] data = new char[len];
    for (int i = 0; i < len; i++) {
        data[i] = buf.getChar();
    }//from   w w w .ja  va2 s .co  m
    return new String(data);
}

From source file:ConversionUtil.java

public static char convertToCharacter(byte[] array) {
    ByteBuffer buffer = ByteBuffer.wrap(array);
    return buffer.getChar();
    /*//from ww  w  .  j av  a  2 s. c  o m
    char value = 0;
    for (int i =0;i<array.length; ++i) {
    int offset = (array.length -i-1) *8;
    value += (array[i] << offset);
    // bytes[i] = (byte)((value & (0xff << offset)) >>> offset);
    }
    return value;
     */
}

From source file:com.nridge.core.base.std.BufUtl.java

/**
 * Retrieves a <i>String</i> value stored within the body of the
 * <code>ByteBuffer</code> object.
 *
 * @param aBuffer Packet byte buffer object.
 * @return A <i>String</i> object.
 *//*w w  w . j a v a  2 s .c  om*/
public static String getString(ByteBuffer aBuffer) {
    int strLength;
    StringBuilder strBuilder;

    strLength = aBuffer.getInt();
    if (strLength > 0)
        strBuilder = new StringBuilder(strLength + 10);
    else
        strBuilder = new StringBuilder();
    for (int i = 0; i < strLength; i++)
        strBuilder.append(aBuffer.getChar());
    return strBuilder.toString();
}

From source file:org.ihsp.data.common.ObjectId.java

private static int createMachineIdentifier() {
    // build a 2-byte machine piece based on NICs info
    int machinePiece;
    try {//ww w . j  av a 2 s .  c o m
        StringBuilder sb = new StringBuilder();
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface ni = e.nextElement();
            sb.append(ni.toString());
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                ByteBuffer bb = ByteBuffer.wrap(mac);
                try {
                    sb.append(bb.getChar());
                    sb.append(bb.getChar());
                    sb.append(bb.getChar());
                } catch (BufferUnderflowException shortHardwareAddressException) { //NOPMD
                    // mac with less than 6 bytes. continue
                }
            }
        }
        machinePiece = sb.toString().hashCode();
    } catch (Throwable t) {
        // exception sometimes happens with IBM JVM, use random
        machinePiece = (new SecureRandom().nextInt());
        log.info("Failed to get machine identifier from network interface, using random number instead", t);
    }
    machinePiece = machinePiece & LOW_ORDER_THREE_BYTES;
    return machinePiece;
}