Example usage for java.nio ByteBuffer flip

List of usage examples for java.nio ByteBuffer flip

Introduction

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

Prototype

public final Buffer flip() 

Source Link

Document

Flips this buffer.

Usage

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(2);
    System.out.println("Default  Byte  Order: " + bb.order());
    bb.putShort((short) 300);
    bb.flip();
    showByteOrder(bb);// w  w w . j ava 2 s .c o m

    bb.clear();
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putShort((short) 300);
    bb.flip();
    showByteOrder(bb);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File aFile = new File("charData.xml");
    FileInputStream inFile = null;

    inFile = new FileInputStream(aFile);

    FileChannel inChannel = inFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(48);

    while (inChannel.read(buf) != -1) {
        System.out.println("String read: " + ((ByteBuffer) (buf.flip())).asCharBuffer().get(0));
        buf.clear();/*  ww w.  jav  a 2s .  c  om*/
    }
    inFile.close();
}

From source file:Main.java

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

    // Put values of different types
    buf.putLong(123L);//from w  ww  .j  a v  a2 s .  com

    // Reset position for reading
    buf.flip();

    // Retrieve the values
    long l = buf.getLong();

}

From source file:MainClass.java

public static void main(String[] args) {

    String phrase = new String("www.java2s.com API \n");
    File aFile = new File("test.txt");

    FileOutputStream file = null;
    try {/*  ww  w  .  j a  v a2 s.co m*/
        file = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = file.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(phrase.length());
    byte[] bytes = phrase.getBytes();
    buf.put(bytes);
    buf.flip();

    try {
        outChannel.write(buf);
        file.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

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

    // Put values of different types
    buf.putFloat(12.3F);/* w  w w. j a  v a  2  s .c o m*/

    // Reset position for reading
    buf.flip();

    // Retrieve the values
    float f = buf.getFloat();

}

From source file:Main.java

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

    // Put values of different types
    buf.putDouble(12.3D);/*  w ww  .  j  a v  a  2s  .c o  m*/

    // Reset position for reading
    buf.flip();

    // Retrieve the values
    double d = buf.getDouble();

}

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();

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

}

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);/*from w  ww .j av a2s .  co  m*/

    bbuf.flip();

    decoder.decode(bbuf, cbuf, false);

    cbuf.flip();

}

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocate(1024);

    while (true) {
        int ret = inc.read(bb);
        if (ret == -1)
            break;
        bb.flip();
        outc.write(bb);//from ww  w.  j a v  a2 s  .co m
        bb.clear();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int port = 19;

    SocketAddress address = new InetSocketAddress("127.0.0.1", port);
    SocketChannel client = SocketChannel.open(address);

    ByteBuffer buffer = ByteBuffer.allocate(74);
    WritableByteChannel out = Channels.newChannel(System.out);

    while (client.read(buffer) != -1) {
        buffer.flip();
        out.write(buffer);//  w ww  .  j  ava2  s  .c o  m
        buffer.clear();
    }
}