Example usage for java.nio ByteBuffer allocate

List of usage examples for java.nio ByteBuffer allocate

Introduction

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

Prototype

public static ByteBuffer allocate(int capacity) 

Source Link

Document

Creates a byte buffer based on a newly allocated byte array.

Usage

From source file:Test.java

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

    try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        sbc.position(4);//  w  w w .  j  a  v a 2  s.co m
        sbc.read(buffer);
        for (int i = 0; i < 5; i++) {
            System.out.print((char) buffer.get(i));
        }

        buffer.clear();
        sbc.position(0);
        sbc.read(buffer);
        for (int i = 0; i < 4; i++) {
            System.out.print((char) buffer.get(i));
        }

    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramChannel server = null;
    server = DatagramChannel.open();
    InetSocketAddress sAddr = new InetSocketAddress("localhost", 8989);
    server.bind(sAddr);/*w w  w  .j  a v  a  2 s  .  c  o  m*/
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    while (true) {
        System.out.println("Waiting for a  message  from" + "  a  remote  host at " + sAddr);
        SocketAddress remoteAddr = server.receive(buffer);
        buffer.flip();
        int limits = buffer.limit();
        byte bytes[] = new byte[limits];
        buffer.get(bytes, 0, limits);
        String msg = new String(bytes);

        System.out.println("Client at " + remoteAddr + "  says: " + msg);
        buffer.rewind();
        server.send(buffer, remoteAddr);
        buffer.clear();
    }
    //server.close();
}

From source file:UDPTimeServer.java

public static void main(String[] args) throws IOException {

    int port = 37;

    ByteBuffer in = ByteBuffer.allocate(8192);
    ByteBuffer out = ByteBuffer.allocate(8);
    out.order(ByteOrder.BIG_ENDIAN);
    SocketAddress address = new InetSocketAddress(port);
    DatagramChannel channel = DatagramChannel.open();
    DatagramSocket socket = channel.socket();
    socket.bind(address);//from   w w w. j  a  v a 2s.c  om
    System.err.println("bound to " + address);
    while (true) {
        try {
            in.clear();
            SocketAddress client = channel.receive(in);
            System.err.println(client);
            long secondsSince1970 = System.currentTimeMillis();
            out.clear();
            out.putLong(secondsSince1970);
            out.flip();

            out.position(4);
            channel.send(out, client);
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }
}

From source file:MainClass.java

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

    SocketAddress address = new InetSocketAddress("127.0.0.1", port);
    SocketChannel client = SocketChannel.open(address);
    ByteBuffer buffer = ByteBuffer.allocate(4);
    IntBuffer view = buffer.asIntBuffer();

    for (int expected = 0;; expected++) {
        client.read(buffer);//from w w w . java 2s .c o  m
        int actual = view.get();
        buffer.clear();
        view.rewind();

        if (actual != expected) {
            System.err.println("Expected " + expected + "; was " + actual);
            break;
        }
        System.out.println(actual);
    }
}

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();// ww w  . j  av a2s .  com
        outc.write(bb);
        bb.clear();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    String fromFileName = args[0];
    String toFileName = args[1];// ww w. j av a2  s .  c  o m
    FileChannel in = new FileInputStream(fromFileName).getChannel();
    FileChannel out = new FileOutputStream(toFileName).getChannel();

    ByteBuffer buff = ByteBuffer.allocate(32 * 1024);

    while (in.read(buff) > 0) {
        buff.flip();
        out.write(buff);
        buff.clear();
    }

    in.close();
    out.close();
}

From source file:CopyChannels.java

public static void main(String[] args) throws Exception {
    String fromFileName = "from.txt";
    String toFileName = "to.txt";
    FileChannel in = new FileInputStream(fromFileName).getChannel();
    FileChannel out = new FileOutputStream(toFileName).getChannel();

    ByteBuffer buff = ByteBuffer.allocate(32 * 1024);

    while (in.read(buff) > 0) {
        buff.flip();/*from  w  ww  . ja v a2 s  . c om*/
        out.write(buff);
        buff.clear();
    }

    in.close();
    out.close();
}

From source file:ExplicitChannelRead.java

public static void main(String args[]) {
    FileInputStream fIn;/*from   w  w  w  .java 2 s. c o  m*/
    FileChannel fChan;
    long fSize;
    ByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");
        fChan = fIn.getChannel();
        fSize = fChan.size();
        mBuf = ByteBuffer.allocate((int) fSize);
        fChan.read(mBuf);
        mBuf.rewind();
        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());
        fChan.close();
        fIn.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    FileInputStream fIn;/*from   www  .jav a2s  .c o  m*/
    FileChannel fChan;
    long fSize;
    ByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");

        fChan = fIn.getChannel();

        fSize = fChan.size();

        mBuf = ByteBuffer.allocate((int) fSize);

        fChan.read(mBuf);

        mBuf.rewind();

        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());
        fChan.close();
        fIn.close();
    } catch (IOException exc) {
        System.out.println(exc);
    }
}

From source file:Main.java

public static void main(String[] args) {
    char[] data = "UsingBuffers".toCharArray();
    ByteBuffer bb = ByteBuffer.allocate(data.length * 2);
    CharBuffer cb = bb.asCharBuffer();
    cb.put(data);//  ww w.  j  a va2s  .  c o m
    System.out.println(cb.rewind());
    symmetricScramble(cb);
    System.out.println(cb.rewind());
    symmetricScramble(cb);
    System.out.println(cb.rewind());

}