Here you can find the source of insertByteArray(byte[] source, ByteBuffer target)
Parameter | Description |
---|---|
source | to be inserted |
target | buffer to insert in |
public static ByteBuffer insertByteArray(byte[] source, ByteBuffer target)
//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; } }