Java ByteBuffer Copy copy(ByteBuffer source, int byteCount)

Here you can find the source of copy(ByteBuffer source, int byteCount)

Description

copy

License

Open Source License

Declaration

public static ByteBuffer copy(ByteBuffer source, int byteCount) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    private static final ByteOrder NATIVE_BYTE_ORDER = ByteOrder.nativeOrder();

    public static ByteBuffer copy(ByteBuffer source, int byteCount) {
        ByteBuffer duplicate = source.duplicate();
        duplicate.position(0);//from   w  ww  .  j  a v  a 2s.  c o m
        int copyLimit = Math.min(duplicate.capacity(), byteCount);
        duplicate.limit(copyLimit);
        ByteBuffer copy = createByteBuffer(byteCount);
        copy.put(duplicate);
        copy.position(0);
        return copy;
    }

    public static ByteBuffer createByteBuffer(int byteCount) {
        return ByteBuffer.allocate(byteCount).order(NATIVE_BYTE_ORDER);
    }
}

Related

  1. copy(ByteBuffer buffer)
  2. copy(ByteBuffer buffer)
  3. copy(ByteBuffer from, ByteBuffer to)
  4. copy(ByteBuffer origin, int start, int end)
  5. copy(ByteBuffer source)
  6. copy(ByteBuffer src, ByteBuffer dst)
  7. copy(ByteBuffer src, ByteBuffer dst, int length)
  8. copy(ByteBuffer src, int srcStartindex, ByteBuffer dest, int destStartIndex, int length)
  9. copy(ByteBuffer src, int startindex, int endindex)