Java ByteBuffer Get insertByteArray(byte[] source, ByteBuffer target)

Here you can find the source of insertByteArray(byte[] source, ByteBuffer target)

Description

Insert a static byte array at the current position of the given message.

License

Open Source License

Parameter

Parameter Description
source to be inserted
target buffer to insert in

Return

new buffer with the position set at the current position

Declaration

public static ByteBuffer insertByteArray(byte[] source, ByteBuffer target) 

Method Source Code

//package com.java2s;
/*// w w w  . j  a  v a  2 s .c om
 *   Copyright (c) 2006-2016 Marvisan Pty. Ltd. All rights reserved.
 *               Use is subject to license terms.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Insert a static byte array at the current position of the given message.
     * @param source to be inserted
     * @param target buffer to insert in
     * @return new buffer with the position set at the current position
     */
    public static ByteBuffer insertByteArray(byte[] source, ByteBuffer target) {

        int current = target.position();
        target.rewind();
        byte[] first = new byte[current];
        byte[] last = new byte[target.capacity() - current];
        target.get(first, 0, first.length);
        target.get(last, 0, last.length);
        byte[] all = new byte[first.length + source.length + last.length];
        System.arraycopy(first, 0, all, 0, first.length);
        System.arraycopy(source, 0, all, first.length, source.length);
        System.arraycopy(last, 0, all, first.length + source.length, last.length);
        target = ByteBuffer.wrap(all);
        target.position(current);

        return target;
    }
}

Related

  1. getVariance(ByteBuffer simulationResults)
  2. getVInt(ByteBuffer bf, int index)
  3. getWithShortLength(ByteBuffer bb)
  4. getZeroTerminatedStringBytes(ByteBuffer dataBuffer)
  5. getZipEocdCentralDirectorySizeBytes(ByteBuffer zipEndOfCentralDirectory)
  6. transfer(ByteBuffer src, FileChannel target, long position, long count)
  7. transferUntilTargetPos(ByteBuffer src, ByteBuffer trg, int trgPos)
  8. utf8ByteBuffer(String messageTemplate, Object... args)