Java ByteBuffer Capacity resetAddressAndCapacity(final ByteBuffer byteBuffer, final long address, final int capacity)

Here you can find the source of resetAddressAndCapacity(final ByteBuffer byteBuffer, final long address, final int capacity)

Description

Set the private address of direct ByteBuffer .

License

Apache License

Parameter

Parameter Description
byteBuffer to set the address on.
address to set for the underlying buffer.
capacity of the new underlying buffer.

Return

the modified

Declaration

public static ByteBuffer resetAddressAndCapacity(final ByteBuffer byteBuffer, final long address,
        final int capacity) 

Method Source Code

//package com.java2s;
/*//from  w  w w.  ja v  a  2s .  c om
 * Copyright 2013 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Set the private address of direct {@link ByteBuffer}.
     *
     * <b>Note:</b> It is assumed a cleaner is not responsible for reclaiming the memory under this buffer and that
     * the caller is responsible for memory allocation and reclamation.
     *
     * @param byteBuffer to set the address on.
     * @param address    to set for the underlying buffer.
     * @param capacity   of the new underlying buffer.
     * @return the modified {@link ByteBuffer}
     */
    public static ByteBuffer resetAddressAndCapacity(final ByteBuffer byteBuffer, final long address,
            final int capacity) {
        if (!byteBuffer.isDirect()) {
            throw new IllegalArgumentException("Can only change address of direct buffers");
        }

        try {
            final Field addressField = Buffer.class.getDeclaredField("address");
            addressField.setAccessible(true);
            addressField.set(byteBuffer, Long.valueOf(address));

            final Field capacityField = Buffer.class.getDeclaredField("capacity");
            capacityField.setAccessible(true);
            capacityField.set(byteBuffer, Integer.valueOf(capacity));

            final Field cleanerField = byteBuffer.getClass().getDeclaredField("cleaner");
            cleanerField.setAccessible(true);
            cleanerField.set(byteBuffer, null);
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }

        return byteBuffer;
    }
}

Related

  1. increaseCapacity(ByteBuffer buffer, int size)
  2. leByteBuffer(int capacity)
  3. nativeByteBuffer(int capacity)
  4. newByteBuffer(int margin, int capacity)
  5. parseToByteBuffer(int capacity, String value)