Here you can find the source of setBytesAtOffset(ByteBuffer buffer, int offset, int length, byte[] data)
Parameter | Description |
---|---|
buffer | The java.nio.ByteBuffer to write data to. |
offset | The absolute offset from which starting to write data. |
length | How many bytes to write. |
data | The data to write into the java.nio.ByteBuffer . |
public static void setBytesAtOffset(ByteBuffer buffer, int offset, int length, byte[] data)
//package com.java2s; /*/*w ww . j av a 2 s.c o m*/ * Copyright (c) Fabio Falcinelli 2016. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.nio.ByteBuffer; public class Main { /** * Given a {@link java.nio.ByteBuffer} set bytes in an absolute offset without altering its current position. * <p> * The operation is {@code synchronized} over the {@code buffer} to avoid thread interference. * * @param buffer The {@link java.nio.ByteBuffer} to write data to. * @param offset The absolute offset from which starting to write data. * @param length How many bytes to write. * @param data The data to write into the {@link java.nio.ByteBuffer}. */ public static void setBytesAtOffset(ByteBuffer buffer, int offset, int length, byte[] data) { synchronized (buffer) { int position = buffer.position(); buffer.position(offset); buffer.put(data, 0, length); buffer.position(position); } } }