Java ByteBuffer convert to read only ByteBuffer

Description

Java ByteBuffer convert to read only ByteBuffer



import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    /**// w w  w . j  ava 2s . co  m
     * <p>
     * Creates a new, read-only byte buffer that shares this buffer's content.
     * </p>
     * <p>
     * The content of the new buffer will be that of this buffer. Changes to this
     * buffer's content will be visible in the new buffer; the new buffer itself,
     * however, will be read-only and will not allow the shared content to be
     * modified. The two buffers' position, limit, and mark values will be
     * independent. Its byte order will be preserved.
     * </p>
     * <p>
     * The new buffer's capacity, limit, position, and mark values will be
     * identical to those of this buffer.
     * </p>
     * <p>
     * If this buffer is itself read-only then this method behaves in exactly the
     * same way as the duplicate method.
     * </p>
     *
     * @param buffer
     *          source buffer
     * @return new buffer
     */
    public static ByteBuffer asReadonly(ByteBuffer buffer) {

        final ByteOrder o = buffer.order();
        final ByteBuffer r = buffer.asReadOnlyBuffer();
        r.order(o);

        return r;
    }
}
/**
 * Copyright (C) 2007 Sly Technologies, Inc. This library 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 2.1 of the License, or (at your option) any later version. This
 * library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */



PreviousNext

Related