Example usage for java.nio ByteBuffer asCharBuffer

List of usage examples for java.nio ByteBuffer asCharBuffer

Introduction

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

Prototype

public abstract CharBuffer asCharBuffer();

Source Link

Document

Returns a char buffer which is based on the remaining content of this byte buffer.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    String[] phrases = { "A", "B 1", "C 1.3" };
    String dirname = "C:/test";
    String filename = "Phrases.txt";
    File dir = new File(dirname);
    File aFile = new File(dir, filename);
    FileOutputStream outputFile = null;
    try {/* w  ww  . j  ava 2s . co  m*/
        outputFile = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = outputFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println(buf.position());
    System.out.println(buf.limit());
    System.out.println(buf.capacity());
    CharBuffer charBuf = buf.asCharBuffer();
    System.out.println(charBuf.position());
    System.out.println(charBuf.limit());
    System.out.println(charBuf.capacity());
    Formatter formatter = new Formatter(charBuf);
    int number = 0;
    for (String phrase : phrases) {
        formatter.format("%n %s", ++number, phrase);
        System.out.println(charBuf.position());
        System.out.println(charBuf.limit());
        System.out.println(charBuf.capacity());
        charBuf.flip();
        System.out.println(charBuf.position());
        System.out.println(charBuf.limit());
        System.out.println(charBuf.length());
        buf.limit(2 * charBuf.length()); // Set byte buffer limit
        System.out.println(buf.position());
        System.out.println(buf.limit());
        System.out.println(buf.remaining());
        try {
            outChannel.write(buf);
            buf.clear();
            charBuf.clear();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
    try {
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from  ww  w .j a v a  2  s  . c  o  m*/
    Charset csets = Charset.forName("UTF-8");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from  w  ww. j  a  v a  2 s .c o m*/
    Charset csets = Charset.forName("UTF-16BE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from ww  w. ja  v  a2s .c o m*/
    Charset csets = Charset.forName("ISO-8859-1");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from  w ww.  ja va  2  s .c o  m*/
    Charset csets = Charset.forName("US-ASCII");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);//from   ww  w .  j av  a2 s  .  co  m
    Charset csets = Charset.forName("UTF-16LE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer
            .wrap(new byte[] { (byte) 'j', (byte) 'a', (byte) 'v', (byte) 'a', (byte) '2', (byte) 's' });
    print(bb);//from w w w . ja va 2  s . c  o  m
    Charset csets = Charset.forName("UTF-16LE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { (byte) 'j', (byte) 'a', (byte) 'v', (byte) 'a', (byte) '2' });
    System.out.println("Initial Byte Buffer");
    print(bb);/* w  w w  .  ja v  a2s .co m*/
    Charset csets = Charset.forName("UTF-8");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("afile.txt");
    FileOutputStream outputFile = null;
    try {/*from w  w w .ja  v  a2 s  .  co  m*/
        outputFile = new FileOutputStream(aFile, true);
        System.out.println("File stream created successfully.");
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("\nByte buffer:");
    System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", buf.position(), buf.limit(),
            buf.capacity());

    // Create a view buffer
    CharBuffer charBuf = buf.asCharBuffer();
    System.out.println("Char view buffer:");
    System.out.printf("position = %2d  Limit = %4d  capacity = %4d%n", charBuf.position(), charBuf.limit(),
            charBuf.capacity());
    try {
        outputFile.close(); // Close the O/P stream & the channel
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:GetData.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    // Allocation automatically zeroes the ByteBuffer:
    int i = 0;/*from w w  w.  ja  va2 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();

}