⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bufferutil.java

📁 sea是一个基于seda模式的实现。这个设计模式将系统分为很多stage。每个stage分布不同的任务(基于线程池)。通过任务流的方式提高系统的效率。
💻 JAVA
字号:
/* * Copyright (c) 2003, The Regents of the University of California, through * Lawrence Berkeley National Laboratory (subject to receipt of any required * approvals from the U.S. Dept. of Energy). All rights reserved. */package gov.lbl.dsd.sea.nio.util;import java.nio.ByteBuffer;/** * Various utilities related to the {@link java.nio.ByteBuffer} buffers. *  * @author whoschek@lbl.gov * @author $Author: hoschek3 $ * @version $Revision: 1.7 $, $Date: 2004/07/27 00:18:19 $ */class BufferUtil {	private BufferUtil() {} // not instantiable	/**	 * Ensures that a given buffer can hold up to <tt>minCapacity</tt>	 * bytes.	 * 	 * Returns the identical buffer if it can hold at least the number of	 * bytes specified. Otherwise, returns a new buffer with increased	 * capacity containing the same bytes, ensuring that it can hold at least	 * the number of elements specified by the minimum capacity argument.	 * The new buffer will have a limit equal to the new capacity.	 * 	 * Leaves the position and byte order unmodified.	 * 	 * @param minCapacity	 *            the desired minimum capacity.	 */	public static ByteBuffer ensureCapacity(ByteBuffer buffer, int minCapacity) {		if (minCapacity > buffer.capacity()) {			int newCapacity = Math.max(minCapacity, (buffer.capacity() * 3) / 2 + 1);			ByteBuffer newBuffer = buffer.isDirect() ? ByteBuffer.allocateDirect(newCapacity) : ByteBuffer.allocate(newCapacity);			newBuffer.order(buffer.order());			int pos = buffer.position();			buffer.position(0);						newBuffer.put(buffer);						buffer.position(pos);			newBuffer.position(pos);			//newBuffer.limit(buffer.limit());			return newBuffer;		} else {			return buffer;		}	}	/**	 * Creates and returns a new buffer containing the remaining bytes in the	 * given buffers; Leaves the input buffers unmodified.	 * 	 * @param buffers -	 *            the buffers to concatenate	 * @return the new buffer, ready to read from	 */	public static ByteBuffer concat(ByteBuffer[] buffers) {		int n = 0;		for (int i = 0; i < buffers.length; i++)			n += buffers[i].remaining();		ByteBuffer buf = (n > 0 && buffers[0].isDirect()) ? ByteBuffer				.allocateDirect(n) : ByteBuffer.allocate(n);		if (n > 0) buf.order(buffers[0].order());				for (int i = 0; i < buffers.length; i++)			buf.put(buffers[i].duplicate());		buf.flip();		return buf;	}	/**	 * The buffer equivalent of {@link System#arraycopy}; 	 * copies src[srcPos]..src[srcPos+length-1] to dest[destPos]..dest[destPos+length-1],	 * leaving the mark, position and limit of both src and dest unmodified;	 * Behaves as expected even if src==dest.	 * 	 * @param src the buffer to read from	 * @param srcPos the index of the first byte to read	 * @param dest the buffer to write to	 * @param destPos the index of the first byte to write	 * @param length the number of bytes to copy	 */	public static void copy(ByteBuffer src, int srcPos, ByteBuffer dest, int destPos, int length) {		if (length == 0) return;		src = src.duplicate();		src.position(srcPos);		src.limit(srcPos + length);				dest = dest.duplicate();		dest.position(destPos);		dest.limit(destPos + length);				dest.put(src);	}		/**	 * Creates a new buffer that is a deep copy of the remaining bytes in the	 * given buffer (between index buf.position() and buf.limit()); leaves the	 * src buffer unmodified. High performance implementation.	 * 	 * @param src	 *            the buffer to copy	 * @return the new buffer, ready to read from	 */	public static ByteBuffer copy(ByteBuffer src) {		ByteBuffer copy = src.isDirect() ? ByteBuffer				.allocateDirect(src.remaining()) : ByteBuffer.allocate(src.remaining());		copy.order(src.order());		copy.put(src.duplicate());		copy.flip();		return copy;		//		src = src.duplicate(); // leave unmodified		//		ByteBuffer copy = ByteBuffer.allocate(src.position());		//		src.flip();		//		copy.put(src);		//		return copy;	}//	/**//	 * Returns a string holding a decoded representation of the remaining bytes//	 * in the given buffer (relative bulk operation).//	 * //	 * @param buffer//	 *            the buffer to convert.//	 * @param  charset the requested charset to convert with //	 * 			(e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))//	 * @return the string//	 *///	public static String getString(ByteBuffer buffer, Charset charset) {//		return charset.decode(buffer).toString();//	}////	/**//	 * Fills the bytes of the encoded string into the given buffer (relative//	 * bulk operation).//	 * //	 * @param buffer//	 *            the buffer to fill into.//	 * @param str//	 *            the string to convert.//	 * @param  charset the requested charset to convert with //	 * 			(e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))//	 *///	public static void putString(ByteBuffer buffer, String str, Charset charset) {//		charset.newEncoder().encode(CharBuffer.wrap(str), buffer, true);//		//		// inefficient, would allocate temporary buffer memory://		// buffer.put(toByteBuffer(str, charset));//	}	////	/**//	 * Creates and returns a byte array filled with the remaining bytes of given//	 * buffer (between index buf.position() and buf.limit()); leaves the src//	 * buffer unmodified. High performance implementation.//	 * //	 * @param src//	 *            the bytebuffer to read from//	 * @return the byte array//	 *///	public static byte[] toByteArray(ByteBuffer src) {//		byte[] copy = new byte[src.remaining()];//		src.duplicate().get(copy);//		return copy;////		//		src = src.duplicate(); // leave unmodified//		//		byte[] copy = new byte[src.position()];//		//		src.flip();//		//		src.get(copy);//		//		return copy;//	}////	/**//	 * Returns a bytebuffer holding the encoded bytes of the given string.//	 * //	 * @param str the string to convert.//	 * @param  charset the requested charset to convert with //	 * 			(e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))//	 * @return the byte buffer//	 *///	public static ByteBuffer toByteBuffer(String str, Charset charset) {//		return charset.encode(CharBuffer.wrap(str));//	}////	/**//	 * Returns a byte array holding the encoded bytes of the given string.//	 * //	 * @param str the string to convert.//	 * @param  charset the requested charset to convert with //	 * 			(e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))//	 * @return the byte array//	 *///	public static byte[] toByteArray(String str, Charset charset) {//		return toByteArray(toByteBuffer(str, charset));//	}////	/**//	 * Returns a decoded string representation of the bytes in the given buffer;//	 * leaves the buffer unmodified.//	 * //	 * @param buffer the buffer to convert//	 * @param  charset the requested charset to convert with //	 * 			(e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))//	 * @return the string representation//	 *///	public static String toString(ByteBuffer buffer, Charset charset) {//		return charset.decode(buffer.duplicate()).toString();//	}////	/**//	 * Returns a decoded string representation of the given bytes.//	 * //	 * @param bytes//	 *            the bytes to convert//	 * @param offset//	 *            The offset of the subarray to be used; must be non-negative//	 *            and no larger than <tt>bytes</tt>.//	 * @param length//	 *            The length of the subarray to be used; must be non-negative//	 *            and no larger than <tt>bytes.length - offset</tt>.//	 * @param charset//	 *            the requested charset to convert with (e.g.//	 *            Charset.forName("US-ASCII"), Charset.forName("UTF-8"))//	 * @return the string representation//	 *///	public static String toString(byte[] bytes, int offset, int length, Charset charset) {//		return toString(ByteBuffer.wrap(bytes, offset, length), charset);//	}////	/**//	 * Returns a decoded string representation of the given bytes.//	 * //	 * @param bytes the bytes to convert//	 * @param  charset the requested charset to convert with //	 * 			(e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))//	 * @return the string representation//	 *///	public static String toString(byte[] bytes, Charset charset) {//		return toString(bytes, 0, bytes.length, charset);//	}//	public static void main(String[] args) {//	int runs = Integer.parseInt(args[0]);//	ByteBuffer buf = ByteBuffer.allocate(1);//	buf.mark();//	System.out.println("hasMark = "+ hasMark(buf));//	long start = System.currentTimeMillis();//	int k=0;//	for (int i=0; i < runs; i++) {//		if (hasMark(buf)) k++;//	}//	long time = System.currentTimeMillis() - start;//	System.out.println("time = "+ (time / 1000.0f));//	System.out.println("iters/s = "+ runs / (time / 1000.0f));//	System.out.println(k);//}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -