Code example for Buffer

Methods: getClass, limit, position

0
	} 
 
	public static void copy(byte[] src, int srcOffset, Buffer dst,
			int numElements) {
		if (useLoonNative) {
			bufferCopy(src, srcOffset, dst, positionInBytes(dst), numElements);
			dst.limit(dst.position() + bytesToElements(dst, numElements));
		} else { 
			putBuffer(dst, src, srcOffset, numElements);
		} 
	} 
 
	public static void copy(short[] src, int srcOffset, Buffer dst,
			int numElements) {
		if (useLoonNative) {
			bufferCopy(src, srcOffset << SIZEOF_BYTE, dst,
					positionInBytes(dst), numElements << SIZEOF_BYTE);
			dst.limit(dst.position()
					+ bytesToElements(dst, numElements << SIZEOF_BYTE));
		} else { 
			putBuffer(dst, src, srcOffset, numElements);
		} 
	} 
 
	public static void copy(char[] src, int srcOffset, Buffer dst,
			int numElements) {
		if (useLoonNative) {
			bufferCopy(src, srcOffset << SIZEOF_BYTE, dst,
					positionInBytes(dst), numElements << SIZEOF_BYTE);
			dst.limit(dst.position()
					+ bytesToElements(dst, numElements << SIZEOF_BYTE));
		} else { 
			putBuffer(dst, src, srcOffset, numElements);
		} 
	} 
 
	public static void copy(int[] src, int srcOffset, Buffer dst,
			int numElements) {
		if (useLoonNative) {
			bufferCopy(src, srcOffset << SIZEOF_SHORT, dst,
					positionInBytes(dst), numElements << SIZEOF_SHORT);
			dst.limit(dst.position()
					+ bytesToElements(dst, numElements << SIZEOF_SHORT));
		} else { 
			putBuffer(dst, src, srcOffset, numElements);
		} 
	} 
 
	public static void copy(long[] src, int srcOffset, Buffer dst,
			int numElements) {
		if (useLoonNative) {
			bufferCopy(src, srcOffset << 3, dst, positionInBytes(dst),
					numElements << 3);
			dst.limit(dst.position() + bytesToElements(dst, numElements << 3));
		} else { 
			putBuffer(dst, src, srcOffset, numElements);
		} 
	} 
 
	public static void copy(float[] src, int srcOffset, Buffer dst,
			int numElements) {
		if (useLoonNative) {
			bufferCopy(src, srcOffset << SIZEOF_SHORT, dst,
					positionInBytes(dst), numElements << SIZEOF_SHORT);
			dst.limit(dst.position()
					+ bytesToElements(dst, numElements << SIZEOF_SHORT));
		} else { 
			putBuffer(dst, src, srcOffset, numElements);
		} 
	} 
 
	public static void copy(double[] src, int srcOffset, Buffer dst,
			int numElements) {
		if (useLoonNative) {
			bufferCopy(src, srcOffset << 3, dst, positionInBytes(dst),
					numElements << 3);
			dst.limit(dst.position() + bytesToElements(dst, numElements << 3));
		} else { 
			putBuffer(dst, src, srcOffset, numElements);
		} 
	} 
 
	public static void copy(Buffer src, Buffer dst, int numElements) {
		if (useLoonNative) {
			int numBytes = elementsToBytes(src, numElements);
			bufferCopy(src, positionInBytes(src), dst, positionInBytes(dst),
					numBytes);
			dst.limit(dst.position() + bytesToElements(dst, numBytes));
		} else { 
			putBuffer(dst, src, numElements);
		} 
	} 
 
	private static int positionInBytes(Buffer dst) {
		if (dst instanceof ByteBuffer) {
			return dst.position();
		} else if (dst instanceof ShortBuffer) {
			return dst.position() << SIZEOF_BYTE;
		} else if (dst instanceof CharBuffer) {
			return dst.position() << SIZEOF_BYTE;
		} else if (dst instanceof IntBuffer) {
			return dst.position() << SIZEOF_SHORT;
		} else if (dst instanceof LongBuffer) {
			return dst.position() << 3;
		} else if (dst instanceof FloatBuffer) {
			return dst.position() << SIZEOF_SHORT;
		} else if (dst instanceof DoubleBuffer) {
			return dst.position() << 3;
		} else { 
			throw new RuntimeException("Can't copy to a "
					+ dst.getClass().getName() + " instance");
		} 
	} 
 
	private static int bytesToElements(Buffer dst, int bytes) {
		if (dst instanceof ByteBuffer) {
			return bytes;
		} else if (dst instanceof ShortBuffer) {
			return bytes >>> SIZEOF_BYTE;
		} else if (dst instanceof CharBuffer) {
			return bytes >>> SIZEOF_BYTE;
		} else if (dst instanceof IntBuffer) {
			return bytes >>> SIZEOF_SHORT;
		} else if (dst instanceof LongBuffer) {
			return bytes >>> 3;
		} else if (dst instanceof FloatBuffer) {
			return bytes >>> SIZEOF_SHORT;
		} else if (dst instanceof DoubleBuffer) {
			return bytes >>> 3;
		} else { 
			throw new RuntimeException("Can't copy to a "
					+ dst.getClass().getName() + " instance");
		} 
	} 
 
	private static int elementsToBytes(Buffer dst, int elements) {
		if (dst instanceof ByteBuffer) {
			return elements;
		} else if (dst instanceof ShortBuffer) {
			return elements << SIZEOF_BYTE;
		} else if (dst instanceof CharBuffer) {
			return elements << SIZEOF_BYTE;
		} else if (dst instanceof IntBuffer) {
			return elements << SIZEOF_SHORT;
		} else if (dst instanceof LongBuffer) {
			return elements << 3;
		} else if (dst instanceof FloatBuffer) {
			return elements << SIZEOF_SHORT;
		} else if (dst instanceof DoubleBuffer) {
			return elements << 3;
		} else { 
			throw new RuntimeException("Can't copy to a "
					+ dst.getClass().getName() + " instance");
		} 
	} 
 
	private static void putBuffer(Buffer dst, Object src, int offset,
			int numFloats) {
		if (dst instanceof ByteBuffer) {
			byte[] buffer = (byte[]) src;
			ByteBuffer writer = (ByteBuffer) dst;
			writer.limit(numFloats);
			writer.put(buffer, offset, numFloats);
		} else if (dst instanceof ShortBuffer) {
			short[] buffer = (short[]) src;
			ShortBuffer writer = (ShortBuffer) dst;
			writer.limit(numFloats);
			writer.put(buffer, offset, numFloats);
		} else if (dst instanceof CharBuffer) {
			char[] buffer = (char[]) src;
			CharBuffer writer = (CharBuffer) dst;
			writer.limit(numFloats);
			writer.put(buffer, offset, numFloats);
		} else if (dst instanceof IntBuffer) {
			int[] buffer = (int[]) src;
			IntBuffer writer = (IntBuffer) dst;
			writer.limit(numFloats);
			writer.put(buffer, offset, numFloats);
		} else if (dst instanceof LongBuffer) {
			long[] buffer = (long[]) src;
			LongBuffer writer = (LongBuffer) dst;
			writer.limit(numFloats);
			writer.put(buffer, offset, numFloats);
		} else if (dst instanceof FloatBuffer) {
			float[] buffer = (float[]) src;
			FloatBuffer writer = (FloatBuffer) dst;
			writer.limit(numFloats);
			writer.put(buffer, offset, numFloats);
		} else if (dst instanceof DoubleBuffer) {
			double[] buffer = (double[]) src;
			DoubleBuffer writer = (DoubleBuffer) dst;
			writer.limit(numFloats);
			writer.put(buffer, offset, numFloats);
		} else { 
			throw new RuntimeException("Can't copy to a "
					+ dst.getClass().getName() + " instance");
		} 
		dst.position(0);
	} 
 
	private static void putBuffer(Buffer dst, Buffer src, int numFloats) {