Code example for Buffer

Methods: getClass, limit, position

0
	} 
 
	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) {
		if (dst instanceof ByteBuffer) {
			ByteBuffer buffer = (ByteBuffer) dst;
			buffer.limit(numFloats);
			buffer.put((ByteBuffer) src);
		} else if (dst instanceof ShortBuffer) {
			ShortBuffer buffer = (ShortBuffer) dst;
			buffer.limit(numFloats);
			buffer.put((ShortBuffer) src);
		} else if (dst instanceof CharBuffer) {
			CharBuffer buffer = (CharBuffer) dst;
			buffer.limit(numFloats);
			buffer.put((CharBuffer) src);
		} else if (dst instanceof IntBuffer) {
			IntBuffer buffer = (IntBuffer) dst;
			buffer.limit(numFloats);
			buffer.put((IntBuffer) src);
		} else if (dst instanceof LongBuffer) {
			LongBuffer buffer = (LongBuffer) dst;
			buffer.limit(numFloats);
			buffer.put((LongBuffer) src);
		} else if (dst instanceof FloatBuffer) {
			FloatBuffer buffer = (FloatBuffer) dst;
			buffer.limit(numFloats);
			buffer.put((FloatBuffer) src);
		} else if (dst instanceof DoubleBuffer) {
			DoubleBuffer buffer = (DoubleBuffer) dst;
			buffer.limit(numFloats);
			buffer.put((DoubleBuffer) src);
		} else { 
			throw new RuntimeException("Can't copy to a "
					+ dst.getClass().getName() + " instance");
		} 
		dst.position(0);
	} 
 
	public static ByteBuffer replaceBytes(ByteBuffer dst, float[] src) {