Example usage for java.nio ByteBuffer remaining

List of usage examples for java.nio ByteBuffer remaining

Introduction

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

Prototype

public final int remaining() 

Source Link

Document

Returns the number of remaining elements in this buffer, that is limit - position .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer buf = ByteBuffer.allocateDirect(10);
    int rem = buf.remaining();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer buf = ByteBuffer.allocateDirect(10);
    int rem = buf.remaining();
    System.out.println(rem);//w ww  .j a va 2s  .c om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes);

    bytes = new byte[buf.remaining()];
    buf.get(bytes, 0, bytes.length);//from w  w  w.  ja v a  2s. co m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create a ByteBuffer from a byte array
    byte[] bytes = new byte[10];
    ByteBuffer buffer = ByteBuffer.wrap(bytes);

    // Retrieve bytes between the position and limit
    bytes = new byte[buffer.remaining()];
    buffer.get(bytes, 0, bytes.length);/*from   ww  w . j  a va  2  s.  com*/

    // Retrieve all bytes in the buffer
    buffer.clear();
    bytes = new byte[buffer.capacity()];
    buffer.get(bytes, 0, bytes.length);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File aFile = new File("primes.txt");
    FileInputStream inFile = new FileInputStream(aFile);
    FileChannel inChannel = inFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
    buf.position(buf.limit());//from w  ww  .  ja  v  a2  s. c  om
    while (true) {
        if (buf.remaining() < 8) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        int strLength = (int) buf.getDouble();
        if (buf.remaining() < 2 * strLength) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        byte[] strChars = new byte[2 * strLength];
        buf.get(strChars);
        if (buf.remaining() < 8) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        System.out.println(strLength);
        System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
        System.out.println(buf.getLong());
    }
    inFile.close();
}

From source file:BufferConverter.java

public static void main(String[] arguments) {
    try {// ww  w .  j  av  a 2  s. c o m
        String data = "friends.dat";
        FileInputStream inData = new FileInputStream(data);
        FileChannel inChannel = inData.getChannel();
        long inSize = inChannel.size();
        ByteBuffer source = ByteBuffer.allocate((int) inSize);
        inChannel.read(source, 0);
        source.position(0);
        for (int i = 0; source.remaining() > 0; i++)
            System.out.print(source.get() + " ");

        source.position(0);
        Charset ascii = Charset.forName("US-ASCII");
        CharsetDecoder toAscii = ascii.newDecoder();
        CharBuffer destination = toAscii.decode(source);
        destination.position(0);
        System.out.println("\n\nNew character data:");
        for (int i = 0; destination.remaining() > 0; i++)
            System.out.print(destination.get());
    } catch (Exception ioe) {
        System.out.println(ioe.getMessage());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.put((byte) 0xFF);
    bbuf.position(5);/*from   ww  w.j av  a 2  s .co  m*/
    bbuf.put((byte) 0xFF);
    int pos = bbuf.position();
    int rem = bbuf.remaining();
    bbuf.limit(7);
    bbuf.rewind();
}

From source file:com.openteach.diamond.network.waverider.network.Packet.java

public static void main(String[] args) {
    /*System.out.println("[DEBUG] Packet Header size = " + getHeaderSize());
            //from   www  .j a v  a  2 s  .com
    SlaveState slaveState = new SlaveState();
    slaveState.setId(1L);
    slaveState.setIsMasterCandidate(false);
    Command command = CommandFactory.createHeartbeatCommand(slaveState.toByteBuffer());
    Packet packet = Packet.newDataPacket(command);
    ByteBuffer buffer = packet.marshall();
    Packet p = Packet.unmarshall(buffer);
    Command cmd = Command.unmarshall(p.getPayLoad());
    SlaveState ss = SlaveState.fromByteBuffer(cmd.getPayLoad());
    System.out.println(cmd.toString());
            
            
    // Test 2
    MasterState masterState = new MasterState();
    masterState.setId(1L);
    masterState.setIp("127.0.0.1");
    masterState.setPort(8206);
    List<SessionState> sessionStateList = new LinkedList<SessionState>();
    masterState.setSessionStateList(sessionStateList);
    SessionState sessionState = null;
    for(int i = 0; i < 10; i++) {
       sessionState = new SessionState();
       sessionState.setIp("127.0.0.1");
       sessionState.setPriority(1L);
       sessionState.setIsMasterCandidate(false);
       sessionStateList.add(sessionState);
    }
    Command command2 = CommandFactory.createHeartbeatCommand(masterState.toByteBuffer());
    Packet packet2 = Packet.newDataPacket(command2);
    ByteBuffer buffer2 = packet2.marshall();
    Packet p2 = Packet.unmarshall(buffer2);
    Command cmd2 = Command.unmarshall(p2.getPayLoad());
    MasterState ms = MasterState.fromByteBuffer(cmd2.getPayLoad());
    System.out.println(cmd.toString());
    */

    System.out.println("[DEBUG] Packet Header size = " + getHeaderSize());

    BlockingQueue<ByteBuffer> queue = new LinkedBlockingQueue<ByteBuffer>();
    Random rand = new Random();
    int count = 0;
    int size = 0;
    for (int i = 0; i < 100000; i++) {
        SlaveState state = new SlaveState();
        state.setId(Long.valueOf(i));
        state.setIsMasterCandidate(true);
        Packet packet = Packet.newDataPacket(
                CommandFactory.createCommand(Command.AVAILABLE_COMMAND_START, state.toByteBuffer()));
        ByteBuffer buffer = packet.marshall();
        rand.setSeed(System.currentTimeMillis());
        count = rand.nextInt(100) + 1;
        size = buffer.remaining() / count;
        for (int j = 0; j < count; j++) {
            ByteBuffer buf = null;
            if (j == count - 1) {
                buf = ByteBuffer.allocate(buffer.remaining() - j * size);
                buf.put(buffer.array(), j * size, buffer.remaining() - j * size);
            } else {
                buf = ByteBuffer.allocate(size);
                buf.put(buffer.array(), j * size, size);
            }
            buf.flip();
            queue.add(buf);
        }
    }

    for (int i = 0; i < 100000; i++) {
        //Packet packet = Packet.parse(queue);
        //Command commad = Command.unmarshall(packet.getPayLoad());
        //SlaveState state = SlaveState.fromByteBuffer(commad.getPayLoad());
        //System.out.println(state.toString());
    }
}

From source file:com.openteach.diamond.network.waverider.session.DefaultSession.java

public static void main(String[] args) {

    BlockingQueue<ByteBuffer> inputBuffer = new LinkedBlockingQueue<ByteBuffer>();
    /*for (int i = 0; i < 10; i++)
    {*//*w ww . j a va  2s  . co m*/
    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    byteBuffer.put(makePacket().marshall());
    byteBuffer.put(makePacket().marshall());
    byteBuffer.flip();
    byte[] b = new byte[8];
    ByteBuffer halfBuf0 = ByteBuffer.allocate(8);
    byteBuffer.get(b);
    halfBuf0.put(b);
    halfBuf0.flip();
    inputBuffer.add(halfBuf0);
    inputBuffer.add(byteBuffer);
    /*}*/

    int size = 0;
    int oldSize = size;
    long length = Packet.getHeaderSize();
    ByteBuffer buffer = ByteBuffer.allocate(NetWorkConstants.DEFAULT_NETWORK_BUFFER_SIZE);
    ByteBuffer currentBuffer = null;

    while (size < length) {
        currentBuffer = inputBuffer.peek();
        oldSize = size;
        int position = currentBuffer.position();
        size += currentBuffer.remaining();
        buffer.put(currentBuffer);
        if (size >= Packet.getHeaderSize()) {
            length = buffer.getLong(Packet.getLengthPosition());
        }

        if (size <= length) {
            inputBuffer.remove();
        } else {
            currentBuffer.position(position);
            buffer.position(buffer.position() - currentBuffer.remaining());
            byte[] buf = new byte[(int) (length - oldSize)];
            currentBuffer.get(buf);
            buffer.put(buf);
        }
    }

    // buffer.position(0);
    buffer.flip();
    Packet packet = Packet.unmarshall(buffer);

    Command command = CommandFactory.createCommand(packet.getType(), packet.getPayLoad());

    String str = new String(command.getPayLoad().array());

    System.out.println(str);

}

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  a  v  a 2  s.c  om
        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);
    }
}