Java tutorial
//package com.java2s; /** * 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 */ import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main { /** * <p> * Creates a new byte buffer whose content is a shared subsequence of this * buffer's content. * </p> * <p> * The content of the new buffer will start at this buffer's current position. * Changes to this buffer's content will be visible in the new buffer, and * vice versa; the two buffers' position, limit, and mark values will be * independent. The utility method also preserves the byte order of the source * buffer into the new buffer. * </p> * <p> * The new buffer's position will be zero, its capacity and its limit will be * the number of bytes remaining in this buffer, and its mark will be * undefined. The new buffer will be direct if, and only if, this buffer is * direct, and it will be read-only if, and only if, this buffer is read-only. * </p> * * @param buffer * source buffer * @return new buffer */ public static ByteBuffer slice(ByteBuffer buffer) { final ByteOrder o = buffer.order(); final ByteBuffer r = buffer.slice(); r.order(o); return r; } }