Code example for ByteBuffer

Methods: clear, limit, position, put

0
	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) {
		int size = src.length;
		dst.clear();
		copy(src, 0, dst, size);
		dst.position(0);
		return dst;
	} 
 
	public static FloatBuffer replaceFloats(FloatBuffer dst, float[] src) {
		int size = src.length;