Example usage for java.nio ByteBuffer wrap

List of usage examples for java.nio ByteBuffer wrap

Introduction

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

Prototype

public static ByteBuffer wrap(byte[] array) 

Source Link

Document

Creates a new byte buffer by wrapping the given byte array.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));
    fc.close();/*from  www.  jav  a 2 s. c  om*/
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    // Now try reading again:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes()));
    fc.close();/*from   w ww  .j av  a  2 s.  com*/
    fc = new FileInputStream("data2.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();

    System.out.println(buff.asCharBuffer());
    // Decode using this system's default Charset:
    buff.rewind();
    String encoding = System.getProperty("file.encoding");
    System.out.println("Decoded using " + encoding + ": " + Charset.forName(encoding).decode(buff));
    // Or, we could encode with something that will print:
    fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));
    fc.close();

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new RandomAccessFile("data.txt", "rw").getChannel();
    fc.position(fc.size());/*from  ww w . j  a  va2  s  .  co m*/
    fc.write(ByteBuffer.wrap("Some more".getBytes()));
    fc.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] invalidBytes = " ".getBytes();
    byte[] validBytes = "(c)".getBytes();

    CharsetDecoder decoder = Charset.forName("US-ASCII").newDecoder();
    CharBuffer buffer = decoder.decode(ByteBuffer.wrap(validBytes));
    System.out.println(Arrays.toString(buffer.array()));

    buffer = decoder.decode(ByteBuffer.wrap(invalidBytes));
    System.out.println(Arrays.toString(buffer.array()));
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    int port = 1234; // default

    ByteBuffer buffer = ByteBuffer.wrap(GREETING.getBytes());
    ServerSocketChannel ssc = ServerSocketChannel.open();

    ssc.socket().bind(new InetSocketAddress(port));
    ssc.configureBlocking(false);//from w w w.j  a  v  a 2s . c  o m

    while (true) {
        System.out.println("Waiting for connections");

        SocketChannel sc = ssc.accept();

        if (sc == null) {
            Thread.sleep(2000);
        } else {
            System.out.println("Incoming connection from: " + sc.socket().getRemoteSocketAddress());

            buffer.rewind();
            sc.write(buffer);
            sc.close();
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    File outputFile = new File("test.txt");

    try (FileChannel fileChannel = new FileOutputStream(outputFile).getChannel()) {
        String text = getText();/*from  w w  w  . j  ava  2s .c  o m*/
        byte[] byteData = text.toString().getBytes("UTF-8");
        ByteBuffer buffer = ByteBuffer.wrap(byteData);
        fileChannel.write(buffer);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("/asynchronous.txt"),
            StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

    Future<Integer> writeFuture1 = fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0);
    Future<Integer> writeFuture2 = fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0);

    int result;//ww w .ja  va  2s.  c om
    result = writeFuture1.get();
    System.out.println("Sample write completed with " + result + " bytes written");
    result = writeFuture2.get();
    System.out.println("Box write completed with " + result + " bytes written");

}

From source file:Test.java

License:asdf

public static void main(String[] args) throws IOException {
    Path path = Paths.get("/users.txt");

    final String newLine = System.getProperty("line.separator");
    try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.APPEND)) {
        String output = newLine + "asdf" + newLine;
        ByteBuffer buffer = ByteBuffer.wrap(output.getBytes());
        sbc.write(buffer);/*  w  w w.jav  a2  s. c  o m*/
    }

}

From source file:GetChannel.java

public static void main(String[] args) throws Exception {
    // Write a file:
    FileChannel fc = new FileOutputStream("data.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text ".getBytes()));
    fc.close();/*  w  w  w. j  av  a 2 s .  c o m*/
    // Add to the end of the file:
    fc = new RandomAccessFile("data.txt", "rw").getChannel();
    fc.position(fc.size()); // Move to the end
    fc.write(ByteBuffer.wrap("Some more".getBytes()));
    fc.close();
    // Read the file:
    fc = new FileInputStream("data.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();
    while (buff.hasRemaining())
        System.out.print((char) buff.get());
}

From source file:BufferToText.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes()));
    fc.close();//from   w w  w  . java 2 s .c  om
    fc = new FileInputStream("data2.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();
    // Doesn't work:
    System.out.println(buff.asCharBuffer());
    // Decode using this system's default Charset:
    buff.rewind();
    String encoding = System.getProperty("file.encoding");
    System.out.println("Decoded using " + encoding + ": " + Charset.forName(encoding).decode(buff));
    // Or, we could encode with something that will print:
    fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));
    fc.close();
    // Now try reading again:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
    // Use a CharBuffer to write through:
    fc = new FileOutputStream("data2.txt").getChannel();
    buff = ByteBuffer.allocate(24); // More than needed
    buff.asCharBuffer().put("Some text");
    fc.write(buff);
    fc.close();
    // Read and display:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
}