Example usage for java.nio ByteBuffer putChar

List of usage examples for java.nio ByteBuffer putChar

Introduction

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

Prototype

public abstract ByteBuffer putChar(char value);

Source Link

Document

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

Usage

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 ww.  jav  a2s.  c  o m*/

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

}

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.putChar('a');

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

From source file:MainClass.java

public static void main(String[] args) {
    String phrase = new String("text \n");
    String dirname = "C:/test";
    String filename = "charData.txt";
    File dir = new File(dirname);
    File aFile = new File(dir, filename);
    FileOutputStream outputFile = null;
    try {//from  w  w w .ja v  a  2  s .c om
        outputFile = new FileOutputStream(aFile, true);
        System.out.println("File stream created successfully.");
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = outputFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("New buffer:           position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    for (char ch : phrase.toCharArray()) {
        buf.putChar(ch);
    }
    System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    buf.flip();
    System.out.println("Buffer after flip:   position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    try {
        outChannel.write(buf);
        outputFile.close();
        System.out.println("Buffer contents written to file.");
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String phrase = new String("www.java2s.com\n");

    File aFile = new File("test.txt");
    FileOutputStream outputFile = null;
    outputFile = new FileOutputStream(aFile, true);
    System.out.println("File stream created successfully.");

    FileChannel outChannel = outputFile.getChannel();

    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("New buffer:           position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    // Load the data into the buffer
    for (char ch : phrase.toCharArray()) {
        buf.putChar(ch);
    }/*  w ww.  j  a  va 2 s . c  o  m*/
    System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    buf.flip();
    System.out.println("Buffer after flip:    position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());

    outChannel.write(buf);
    outputFile.close();
    System.out.println("Buffer contents written to file.");
}

From source file:MainClass.java

public static void main(String[] args) {
    String phrase = new String("www.java2s.com\n");

    File aFile = new File("test.txt");
    FileOutputStream outputFile = null;
    try {//from  w ww  . j  a  va 2 s . com
        outputFile = new FileOutputStream(aFile, true);
        System.out.println("File stream created successfully.");
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }

    FileChannel outChannel = outputFile.getChannel();

    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("New buffer:           position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());

    // Load the data into the buffer
    for (char ch : phrase.toCharArray()) {
        buf.putChar(ch);
    }
    System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    buf.flip();
    System.out.println("Buffer after flip:    position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());

    try {
        outChannel.write(buf);
        outputFile.close();
        System.out.println("Buffer contents written to file.");
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static void putString(ByteBuffer buf, String str) {
    int len = str.length();
    for (int i = 0; i < len; i++) {
        buf.putChar(str.charAt(i));
    }/*  w ww  .j a va 2  s .co  m*/
}

From source file:ConversionUtil.java

public static byte[] convertToByteArray(char value) {
    byte[] bytes = new byte[2];
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    buffer.putChar(value);
    return buffer.array();
    // buffer.get(bytes);
    /*//w  ww .ja va  2s  .com
    for (int i =0;i<bytes.length; ++i) {
    int offset = (bytes.length -i-1) *8;
    bytes[i] = (byte)((value & (0xff << offset)) >>> offset);
    }
     **/
    // return bytes;
}

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

/**
 * Stores the <i>String</i> parameter value into the
 * <code>ByteBuffer</code> object.
 *
 * @param aBuffer Packet byte buffer object.
 * @param aString A non-null string./*from  w  w w  .  j  a  v  a 2 s.co  m*/
 */
public static void putString(ByteBuffer aBuffer, String aString) {
    int strLength;

    if ((aBuffer != null) && (aString != null)) {
        strLength = aString.length();
        aBuffer.putInt(strLength);
        for (int i = 0; i < strLength; i++)
            aBuffer.putChar(aString.charAt(i));
    }
}

From source file:com.googlecode.mp4parser.boxes.microsoft.XtraBox.java

private static void writeUtf16String(ByteBuffer dest, String s) {
    char ar[] = s.toCharArray();
    for (int i = 0; i < ar.length; i++) { //Probably not the best way to do this but it preserves the byte order
        dest.putChar(ar[i]);
    }//from  www. ja v a 2  s  .  com
    dest.putChar((char) 0); //Terminating null
}

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

@Override
protected void putBody(ByteBuffer buf) {
    buf.putLong(0x0100000000000000L);//from  www  .j  a v  a 2 s  .  co m
    buf.putInt(0);
    buf.putChar((char) user.getFileAgentToken().length);
    buf.put(user.getFileAgentToken());

    buf.put((byte) 0x04);
    buf.put((byte) 0x4C);
    buf.putInt(clusterId);
    buf.putInt(imageLength);
    buf.put(md5);
    buf.put(md5(fileName.getBytes()));
    buf.putChar((char) 0);
}