Java ByteBuffer Set setBytesAtOffset(ByteBuffer buffer, int offset, int length, byte[] data)

Here you can find the source of setBytesAtOffset(ByteBuffer buffer, int offset, int length, byte[] data)

Description

Given a java.nio.ByteBuffer set bytes in an absolute offset without altering its current position.

License

Open Source License

Parameter

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 .

Declaration

public static void setBytesAtOffset(ByteBuffer buffer, int offset, int length, byte[] data) 

Method Source Code


//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);
        }
    }
}

Related

  1. resetBit(ByteBuffer in, int pos)
  2. set0byte(MappedByteBuffer mem, byte value)
  3. set24BitInt(ByteBuffer buffer, int value)
  4. setBatch(ByteBuffer bb, long batchId)
  5. setByteOrder(ByteBuffer buffer)
  6. setCell(ByteBuffer buffer, int cellIndex, int cellbytes)
  7. setCreationTime(ByteBuffer buf, Date date)
  8. setFree(int frameIx, int offset, boolean free, ByteBuffer[] frames)
  9. setLimIfNeeded(ByteBuffer bb, int lim)