Java ByteBuffer Put put(ByteBuffer from, ByteBuffer to)

Here you can find the source of put(ByteBuffer from, ByteBuffer to)

Description

Put data from one buffer into another, avoiding over/under flows

License

Open Source License

Parameter

Parameter Description
from Buffer to take bytes from in flush mode
to Buffer to put bytes to in fill mode.

Return

number of bytes moved

Declaration

public static int put(ByteBuffer from, ByteBuffer to) 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

import java.nio.ByteBuffer;

public class Main {
    /**/* w ww.j  a v  a 2  s. c  om*/
     * Put data from one buffer into another, avoiding over/under flows
     * @param from Buffer to take bytes from in flush mode
     * @param to   Buffer to put bytes to in fill mode.
     * @return number of bytes moved
     */
    public static int put(ByteBuffer from, ByteBuffer to) {
        int put;
        int remaining = from.remaining();
        if (remaining > 0) {
            if (remaining <= to.remaining()) {
                to.put(from);
                put = remaining;
                from.position(from.limit());
            } else if (from.hasArray()) {
                put = to.remaining();
                to.put(from.array(), from.arrayOffset() + from.position(), put);
                from.position(from.position() + put);
            } else {
                put = to.remaining();
                ByteBuffer slice = from.slice();
                slice.limit(put);
                to.put(slice);
                from.position(from.position() + put);
            }
        } else
            put = 0;

        return put;
    }
}

Related

  1. put(ByteBuffer bb, byte[] bytes, int from)
  2. put(ByteBuffer bb, String s)
  3. put(ByteBuffer bbuf, byte[] post)
  4. put(ByteBuffer buf, String s, String charsetName)
  5. put(ByteBuffer dst, ByteBuffer src, int transferLimit)
  6. put(ByteBuffer src, ByteBuffer dst)
  7. put3ByteInt(ByteBuffer buffer, int val)
  8. putAscii(ByteBuffer bytes, String value)
  9. putAsMuchAsPossible(ByteBuffer dest, ByteBuffer src)