Java tutorial
//package com.java2s; /** * Copyright 2008 - 2011 * * 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. * * @project loonframework * @author chenpeng * @emailceponline@yahoo.com.cn * @version 0.1 */ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.DoubleBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; public class Main { public static DoubleBuffer clone(final DoubleBuffer buf) { if (buf == null) { return null; } buf.rewind(); final DoubleBuffer copy; if (buf.isDirect()) { copy = createDoubleBuffer(buf.limit()); } else { copy = createDoubleBufferOnHeap(buf.limit()); } copy.put(buf); return copy; } public static FloatBuffer clone(final FloatBuffer buf) { if (buf == null) { return null; } buf.rewind(); final FloatBuffer copy; if (buf.isDirect()) { copy = createFloatBuffer(buf.limit()); } else { copy = createFloatBufferOnHeap(buf.limit()); } copy.put(buf); return copy; } public static IntBuffer clone(final IntBuffer buf) { if (buf == null) { return null; } buf.rewind(); final IntBuffer copy; if (buf.isDirect()) { copy = createIntBuffer(buf.limit()); } else { copy = createIntBufferOnHeap(buf.limit()); } copy.put(buf); return copy; } public static ByteBuffer clone(final ByteBuffer buf) { if (buf == null) { return null; } buf.rewind(); final ByteBuffer copy; if (buf.isDirect()) { copy = createByteBuffer(buf.limit()); } else { copy = createByteBufferOnHeap(buf.limit()); } copy.put(buf); return copy; } public static ShortBuffer clone(final ShortBuffer buf) { if (buf == null) { return null; } buf.rewind(); final ShortBuffer copy; if (buf.isDirect()) { copy = createShortBuffer(buf.limit()); } else { copy = createShortBufferOnHeap(buf.limit()); } copy.put(buf); return copy; } public static DoubleBuffer createDoubleBuffer(final int size) { final DoubleBuffer buf = ByteBuffer.allocateDirect(8 * size).order(ByteOrder.nativeOrder()) .asDoubleBuffer(); buf.clear(); return buf; } public static DoubleBuffer createDoubleBuffer(DoubleBuffer buf, final int size) { if (buf != null && buf.limit() == size) { buf.rewind(); return buf; } buf = createDoubleBuffer(size); return buf; } public static DoubleBuffer createDoubleBufferOnHeap(final int size) { final DoubleBuffer buf = ByteBuffer.allocate(8 * size).order(ByteOrder.nativeOrder()).asDoubleBuffer(); buf.clear(); return buf; } public static FloatBuffer createFloatBuffer(FloatBuffer buffer) { FloatBuffer dest = createFloatBuffer(buffer.capacity()); dest.clear(); dest.put(buffer); dest.position(0); return dest; } public static FloatBuffer createFloatBuffer(FloatBuffer buffer, int start, int end) { final float[] inds = new float[buffer.limit()]; for (int x = start - 1; x < end - 1; x++) { inds[x] = buffer.get(); } return createFloatBuffer(inds); } public static FloatBuffer createFloatBuffer(final int size) { final FloatBuffer buf = ByteBuffer.allocateDirect(4 * size).order(ByteOrder.nativeOrder()).asFloatBuffer(); buf.clear(); return buf; } public static FloatBuffer createFloatBuffer(float... floats) { FloatBuffer fb = createFloatBuffer(floats.length); fb.put(floats); fb.position(0); return fb; } public static FloatBuffer createFloatBuffer(final FloatBuffer reuseStore, final float... data) { if (data == null) { return null; } final FloatBuffer buff; if (reuseStore == null || reuseStore.capacity() != data.length) { buff = createFloatBuffer(data.length); } else { buff = reuseStore; buff.clear(); } buff.clear(); buff.put(data); buff.flip(); return buff; } public static FloatBuffer createFloatBufferOnHeap(final int size) { final FloatBuffer buf = ByteBuffer.allocate(4 * size).order(ByteOrder.nativeOrder()).asFloatBuffer(); buf.clear(); return buf; } public static IntBuffer createIntBuffer(final int... data) { if (data == null) { return null; } final IntBuffer buff = createIntBuffer(data.length); buff.clear(); buff.put(data); buff.flip(); return buff; } public static IntBuffer createIntBuffer(final int size) { final IntBuffer buf = ByteBuffer.allocateDirect(4 * size).order(ByteOrder.nativeOrder()).asIntBuffer(); buf.clear(); return buf; } public static IntBuffer createIntBuffer(IntBuffer buf, final int size) { if (buf != null && buf.limit() == size) { buf.rewind(); return buf; } buf = createIntBuffer(size); return buf; } public static IntBuffer createIntBufferOnHeap(final int size) { final IntBuffer buf = ByteBuffer.allocate(4 * size).order(ByteOrder.nativeOrder()).asIntBuffer(); buf.clear(); return buf; } public static ByteBuffer createByteBuffer(byte... bytes) { ByteBuffer bb = createByteBuffer(bytes.length).put(bytes); bb.position(0); return bb; } public static ByteBuffer createByteBuffer(final int size) { final ByteBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()); buf.clear(); return buf; } public static ByteBuffer createByteBuffer(ByteBuffer buf, final int size) { if (buf != null && buf.limit() == size) { buf.rewind(); return buf; } buf = createByteBuffer(size); return buf; } public static ByteBuffer createByteBufferOnHeap(final int size) { final ByteBuffer buf = ByteBuffer.allocate(size).order(ByteOrder.nativeOrder()); buf.clear(); return buf; } public static ByteBuffer createByteBufferOnHeap(ByteBuffer buf, final int size) { if (buf != null && buf.limit() == size) { buf.rewind(); return buf; } buf = createByteBufferOnHeap(size); return buf; } public static ShortBuffer createShortBuffer(final int size) { final ShortBuffer buf = ByteBuffer.allocateDirect(2 * size).order(ByteOrder.nativeOrder()).asShortBuffer(); buf.clear(); return buf; } public static ShortBuffer createShortBuffer(final short... data) { if (data == null) { return null; } final ShortBuffer buff = createShortBuffer(data.length); buff.clear(); buff.put(data); buff.flip(); return buff; } public static ShortBuffer createShortBuffer(ShortBuffer buf, final int size) { if (buf != null && buf.limit() == size) { buf.rewind(); return buf; } buf = createShortBuffer(size); return buf; } public static ShortBuffer createShortBufferOnHeap(final int size) { final ShortBuffer buf = ByteBuffer.allocate(2 * size).order(ByteOrder.nativeOrder()).asShortBuffer(); buf.clear(); return buf; } }