Java ByteBuffer Put putWhatFits(ByteBuffer dest, ByteBuffer src)

Here you can find the source of putWhatFits(ByteBuffer dest, ByteBuffer src)

Description

Puts as much of src as will fit in dest.

License

Apache License

Parameter

Parameter Description
dest the destination buffer.
src the source buffer.

Declaration

public static void putWhatFits(ByteBuffer dest, ByteBuffer src) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

public class Main {
    /**// w w  w  .ja v a  2  s.  c om
     * Puts as much of <code>src</code> as will fit in <code>dest</code>.
     *
     * The minimum of (<code>dest.remaining()</code> and <code>src.remaining()</code>) is copied from src
     * to dest, and their respective positions moved by that much.
     *
     * @param dest the destination buffer.
     * @param src the source buffer.
     */
    public static void putWhatFits(ByteBuffer dest, ByteBuffer src) {
        final int toPut = Math.min(dest.remaining(), src.remaining());
        if (toPut == src.remaining()) {
            dest.put(src);
        } else {
            if (toPut != 0) {
                final ByteBuffer slice = src.slice();
                slice.limit(toPut);
                src.position(src.position() + toPut);
                dest.put(slice);
            }
        }
    }
}

Related

  1. putUnsignedInt(final ByteBuffer dst, final long value)
  2. putUnsignedShort(ByteBuffer bytes, int value)
  3. putUUID(ByteBuffer bytes, UUID uuid)
  4. putVarint(ByteBuffer buffer, long number, int byteSize)
  5. putVInt(ByteBuffer b, int i)
  6. putWithChecksum(ByteBuffer buffer, int value, Adler32 checksum)
  7. read(InputStream is, ByteBuffer buf, int bytes)
  8. readAsByteBuffer(final InputStream source)
  9. readBER32(ByteBuffer input)