Example usage for java.nio CharBuffer flip

List of usage examples for java.nio CharBuffer flip

Introduction

In this page you can find the example usage for java.nio CharBuffer flip.

Prototype

public final Buffer flip() 

Source Link

Document

Flips this buffer.

Usage

From source file:MainClass.java

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

    cb.put("This is a test String");

    cb.flip();

    // This throws an IllegalArgumentException
    cb.put(cb);/*from  ww w. j a v a  2 s. c o  m*/

    System.out.println(cb);
}

From source file:Main.java

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

    cb.put("This is a test String");

    cb.flip();

    System.out.println("hasArray() = " + cb.hasArray());

    char[] carray = cb.array();

    System.out.print("array=");

    for (int i = 0; i < carray.length; i++) {
        System.out.print(carray[i]);
    }//w  w  w  .  j ava  2s .co m

}

From source file:MainClass.java

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

    cb.put("This is a test String");

    cb.flip();

    System.out.println("hasArray() = " + cb.hasArray());

    char[] carray = cb.array();

    System.out.print("array=");

    for (int i = 0; i < carray.length; i++) {
        System.out.print(carray[i]);
    }/*from w  ww  .  ja  va2s.  c  om*/

    System.out.println("");
    System.out.flush();
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    CharBuffer cb = CharBuffer.allocate(15);

    cb.put("Hello World");
    println(cb);//from   ww  w  .  j  ava2 s. co m

    cb.flip();
    println(cb);

    cb.flip();
    println(cb);
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    char[] chars = new char[60];

    CharBuffer cb = CharBuffer.wrap(chars, 12, 42);

    println(cb);//from w ww .ja va2s  .  c  o m

    cb.put("This is a test String");

    cb.flip();

    println(cb);

    cb.clear();

    cb.put("Foobar");

    println(cb);

    for (int i = 0; i < 20; i++) {
        System.out.println("[" + i + "] = " + chars[i]);
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    CharBuffer cb = CharBuffer.allocate(100);

    Reader reader = new StringReader(s);

    try {//from  w  w  w  . j  a v  a 2  s . c  o m
        reader.read(cb);

        cb.flip();

        // print the char buffer
        System.out.println(cb.toString());

        reader.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer bbuf = ByteBuffer.allocateDirect(1024);

    CharBuffer cbuf = CharBuffer.allocate(1024);

    encoder.encode(cbuf, bbuf, false);//w ww  . ja  va 2s  .  c om

    bbuf.flip();

    decoder.decode(bbuf, cbuf, false);

    cbuf.flip();

}

From source file:MainClass.java

License:asdf

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

    String string = "asdf";

    for (int i = 0; i < string.length(); i++) {
        buffer.put(string.charAt(i));/*from  w  w w  .ja  v  a  2s. com*/
    }

    buffer.flip();
    drainBuffer(buffer);
    buffer.clear();
}

From source file:GetWebPageDemo.java

public static void main(String args[]) throws Exception {
    String resource, host, file;/*w ww. j ava2 s  . c o m*/
    int slashPos;

    resource = "www.java2s.com/index.htm";
    slashPos = resource.indexOf('/'); // find host/file separator
    if (slashPos < 0) {
        resource = resource + "/";
        slashPos = resource.indexOf('/');
    }
    file = resource.substring(slashPos); // isolate host and file parts
    host = resource.substring(0, slashPos);
    System.out.println("Host to contact: '" + host + "'");
    System.out.println("File to fetch : '" + file + "'");

    SocketChannel channel = null;

    try {
        Charset charset = Charset.forName("ISO-8859-1");
        CharsetDecoder decoder = charset.newDecoder();
        CharsetEncoder encoder = charset.newEncoder();

        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        CharBuffer charBuffer = CharBuffer.allocate(1024);

        InetSocketAddress socketAddress = new InetSocketAddress(host, 80);
        channel = SocketChannel.open();
        channel.connect(socketAddress);

        String request = "GET " + file + " \r\n\r\n";
        channel.write(encoder.encode(CharBuffer.wrap(request)));

        while ((channel.read(buffer)) != -1) {
            buffer.flip();
            decoder.decode(buffer, charBuffer, false);
            charBuffer.flip();
            System.out.println(charBuffer);
            buffer.clear();
            charBuffer.clear();
        }
    } catch (UnknownHostException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException ignored) {
            }
        }
    }

    System.out.println("\nDone.");
}

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 {//from  w w w. j ava 2 s .com
        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);
    }
}