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

📄 byteconverter.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;

/**
Efficient big endian array conversions for high performance I/O through bulk data 
transfer rather than piece-wise transfer. (You may want to disregard this class in 
favour of the conversion facilities in the {@link java.nio.ByteBuffer} family of classes).
<p>
Avoids deep non-inlined synchronized 
call chains in the java.io package. The following conversions are supported: 
<ul>
  <li>for all primitive data types: <tt>type[] --> byte[]</tt>, as well as <tt>type   --> byte[]</tt></li>
  <li>for all primitive data types: <tt>byte[] --> type[]</tt>, as well as <tt>byte[] --> type</tt></li>
</ul>
This <b>exactly mimics</b> the semantics of {@link java.io.DataInputStream} and 
{@link java.io.DataOutputStream}. In fact, this is a copy&paste of the JDK 1.4.2 
source code of these two classes, with efficient loops written around.
Not only for disk I/O, but also for other kinds of serialization, e.g. high bandwidth networking.
<h3>Fast writing:</h3>
<ul>
  <li>Convert your data into a byte[] using 
	<ul>
	  <li>for all primitive data types: type[] --> byte[]: Example: <tt>write(double[] 
		src, int srcPos, byte[] dest, int destPos, int length)</tt></li>
	  <li>for all primitive data types: type --> byte[]: Example: <tt>writeDouble(double 
		src, byte[] dest, int destPos)</tt></li>
	</ul>
  <li>then write it with a single call {@link java.io.FileOutputStream#write(byte[], int, int)}, perhaps buffered.</li>
</ul>
<h3>Fast reading:</h3>
<ul>
  <li>Read your data into a byte[] with a single call {@link java.io.FileInputStream#read(byte[], int, int)}, perhaps buffered. 
  <li>Then convert it into the desired data type using 
	<ul>
	  <li>for all primitive data types: byte[] --> type[]: Example: <tt>read(byte[] 
		src, int srcPos, double[] dest, int destPos, int length)</tt></li>
	  <li>for all primitive data types: byte[] --> type: Example: <tt>readInt(byte[] 
		src, int srcPos)</tt></li>
	</ul>
  </li>
</ul>
The method arguments are exactly as used in {@link System#arraycopy}, except for 
the <tt>length</tt> argument which is always given in primitive element units!
Please don't flame for not writing redundant javadoc for each and every method.

@author whoschek@lbl.gov
@author $Author: hoschek3 $
@version $Revision: 1.12 $, $Date: 2004/07/28 01:53:13 $
*/

public class ByteConverter {
	// TODO: Consider rewriting this class using java.nio.ByteBuffer !? 
	
	protected ByteConverter() {}
	
	// number of bytes occupied for each primitive data type:
	public static final int SIZE_OF_BOOLEAN = 1;
	public static final int SIZE_OF_BYTE = 1;
	public static final int SIZE_OF_CHAR = 2;
	public static final int SIZE_OF_SHORT = 2;
	public static final int SIZE_OF_INT = 4;
	public static final int SIZE_OF_FLOAT = 4;
	public static final int SIZE_OF_LONG = 8;
	public static final int SIZE_OF_DOUBLE = 8;
	
	public static byte readByte(byte[] src, int srcPos) {
		return src[srcPos];
	}
	
	public static boolean readBoolean(byte[] src, int srcPos) {
		return (src[srcPos] & 0xFF) != 0;
	}
	
	public static char readChar(byte[] src, int srcPos) {
		int b0 = src[srcPos  ] & 0xFF;
		int b1 = src[srcPos+1] & 0xFF;
		return (char)((b0 << 8) + (b1 << 0));
	}
	
	public static short readShort(byte[] src, int srcPos) {
		int b0 = src[srcPos  ] & 0xFF;
		int b1 = src[srcPos+1] & 0xFF;
		return (short)((b0 << 8) + (b1 << 0));
	}
	
	public static int readInt(byte[] src, int srcPos) {
		int b0 = src[srcPos  ] & 0xFF;
		int b1 = src[srcPos+1] & 0xFF;
		int b2 = src[srcPos+2] & 0xFF;
		int b3 = src[srcPos+3] & 0xFF;
		return (b0 << 24) + (b1 << 16) + (b2 << 8) + (b3 << 0);
	}
	
	public static float readFloat(byte[] src, int srcPos) {
		return Float.intBitsToFloat(readInt(src,srcPos));
	}
	
	public static long readLong(byte[] src, int srcPos) {
		return (((long)src[srcPos] << 56) +
				((long)(src[srcPos+1] & 255) << 48) +
				((long)(src[srcPos+2] & 255) << 40) +
				((long)(src[srcPos+3] & 255) << 32) +
				((long)(src[srcPos+4] & 255) << 24) +
				((src[srcPos+5] & 255) << 16) +
				((src[srcPos+6] & 255) <<  8) +
				((src[srcPos+7] & 255) <<  0));
	}
	
	public static double readDouble(byte[] src, int srcPos) {
		return Double.longBitsToDouble(readLong(src,srcPos));
	}
	
	public static int readUnsignedByte(byte[] src, int srcPos) {
		return src[srcPos] & 0xFF;
	}
	
	public static int readUnsignedShort(byte[] src, int srcPos) {
		int b0 = src[srcPos  ] & 0xFF;
		int b1 = src[srcPos+1] & 0xFF;
		return (b0 << 8) + (b1 << 0);
	}

	public static long readUnsignedInt(byte[]src, int srcPos) {
		return ((src[srcPos ] & 0xFFL) << 24)
			+ ((src[srcPos+1] & 0xFF) << 16)
			+ ((src[srcPos+2] & 0xFF) << 8)
			+ ( src[srcPos+3] & 0xFF);
	}

	/**
	 * Returns the integer value gained when interpreting the value v as an <em>unsigned byte</em>.
	 * Example: readUnsignedByte(0xFF) == 255, rather than -1.
	 */
	public static int readUnsignedByte(byte v) {
		return v & 0xFF;
	}

	/**
	 * Returns the integer value gained when interpreting the value v as an <em>unsigned short</em>.
	 */
	public static int readUnsignedShort(short v) {
		return v & 0xFFFF;
	}

	/**
	 * Returns the long value gained when interpreting the value v as an <em>unsigned int</em>.
	 */
	public static long readUnsignedInt(int v) {
		return v & 0xFFFFFFFFL;
	}

	public static void read(byte[] src, int srcPos, byte[] dest, int destPos, int length) {
		System.arraycopy(src, srcPos, dest, destPos, length);
	}
	
	public static void read(byte[] src, int srcPos, boolean[] dest, int destPos, int length) {
		while (--length >= 0) {
			dest[destPos++] = readBoolean(src,srcPos);
			srcPos += SIZE_OF_BOOLEAN;
		}
	}
	
	public static void read(byte[] src, int srcPos, char[] dest, int destPos, int length) {
		while (--length >= 0) {
			dest[destPos++] = readChar(src,srcPos);
			srcPos += SIZE_OF_CHAR;
		}
	}
	
	public static void read(byte[] src, int srcPos, short[] dest, int destPos, int length) {
		while (--length >= 0) {
			dest[destPos++] = readShort(src,srcPos);
			srcPos += SIZE_OF_SHORT;
		}
	}
	
	public static void read(byte[] src, int srcPos, int[] dest, int destPos, int length) {
		while (--length >= 0) {
			dest[destPos++] = readInt(src,srcPos);
			srcPos += SIZE_OF_INT;
		}
	}
	
	public static void read(byte[] src, int srcPos, float[] dest, int destPos, int length) {
		while (--length >= 0) {
			dest[destPos++] = readFloat(src,srcPos);
			srcPos += SIZE_OF_FLOAT;
		}
	}
	
	public static void read(byte[] src, int srcPos, long[] dest, int destPos, int length) {
		while (--length >= 0) {
			dest[destPos++] = readLong(src,srcPos);
			srcPos += SIZE_OF_LONG;
		}
	}
	
	public static void read(byte[] src, int srcPos, double[] dest, int destPos, int length) {
		while (--length >= 0) {
			dest[destPos++] = readDouble(src,srcPos);
			srcPos += SIZE_OF_DOUBLE;
		}
	}
	
//	/**
//	 * The inverse to {@link #writeBytes(String, int, byte[], int, int)}; each single byte is converted to a 2 byte char.
//	 */
//	public static String readBytes(byte[] src, int srcPos, char[] buffer, int destPos, int length) {
//		int len = length;
//		int pos = destPos;
//		while (--length >= 0) {
//			buffer[destPos++] = (char) readByte(src,srcPos);
//			srcPos += SIZE_OF_BYTE;
//		}
//		return new String(buffer,pos,len);
//	}
//	
//	public static String readChars(byte[] src, int srcPos, char[] buffer, int destPos, int length) {
//		int len = length;
//		int pos = destPos;
//		while (--length >= 0) {
//			buffer[destPos++] = readChar(src,srcPos);
//			srcPos += SIZE_OF_CHAR;
//		}
//		return new String(buffer,pos,len);
//	}	
	
	public static void writeByte(byte v, byte[] dest, int destPos) {
		dest[destPos] = v;
	}
	
	public static void writeBoolean(boolean v, byte[] dest, int destPos) {
		dest[destPos] = (byte) (v ? 1 : 0);
	}
	
	public static void writeChar(char v, byte[] dest, int destPos) {
		dest[destPos  ] = (byte) ((v >>> 8) & 0xFF);
		dest[destPos+1] = (byte) ((v >>> 0) & 0xFF);
	}
	
	public static void writeShort(short v, byte[] dest, int destPos) {
		dest[destPos  ] = (byte) ((v >>> 8) & 0xFF);
		dest[destPos+1] = (byte) ((v >>> 0) & 0xFF);
	}
	
	public static void writeFloat(float v, byte[] dest, int destPos) {
		writeInt(Float.floatToIntBits(v),dest,destPos);
	}
	
	public static void writeInt(int v, byte[] dest, int destPos) {
		dest[destPos  ] = (byte) ((v >>> 24) & 0xFF);
		dest[destPos+1] = (byte) ((v >>> 16) & 0xFF);
		dest[destPos+2] = (byte) ((v >>>  8) & 0xFF);
		dest[destPos+3] = (byte) ((v >>>  0) & 0xFF);	
	}
	
	public static void writeLong(long v, byte[] dest, int destPos) {
		dest[destPos  ] = (byte) ((int)(v >>> 56) & 0xFF);
		dest[destPos+1] = (byte) ((int)(v >>> 48) & 0xFF);
		dest[destPos+2] = (byte) ((int)(v >>> 40) & 0xFF);
		dest[destPos+3] = (byte) ((int)(v >>> 32) & 0xFF);
		dest[destPos+4] = (byte) ((int)(v >>> 24) & 0xFF);
		dest[destPos+5] = (byte) ((int)(v >>> 16) & 0xFF);
		dest[destPos+6] = (byte) ((int)(v >>>  8) & 0xFF);
		dest[destPos+7] = (byte) ((int)(v >>>  0) & 0xFF);
	}
	
	public static void writeDouble(double src, byte[] dest, int destPos) {
		writeLong(Double.doubleToLongBits(src),dest,destPos);
	}
	
	public static void writeUnsignedShort(int v, byte[] dest, int destPos) {
		if (v < 0 || v > 65535) // (int) Math.pow(2.0, 16) - 1
				throw new IllegalArgumentException(
						"unsigned short: " + v + " must be in the range [0,65535]");
		dest[destPos  ] = (byte) ((v >>> 8) & 0xFF);
		dest[destPos+1] = (byte) ((v >>> 0) & 0xFF);
	}
	
	public static void writeUnsignedInt(long v, byte[] dest, int destPos) { // from karlo
		if (v < 0 || v > 4294967295L) // (long) Math.pow(2.0, 32) - 1
				throw new IllegalArgumentException(
						"unsigned int: " + v + " must be in the range [0,4294967295]");
		dest[destPos  ] = (byte) ((v >>> 24) & 0xFF);
		dest[destPos+1] = (byte) ((v >>> 16) & 0xFF);
		dest[destPos+2] = (byte) ((v >>>  8) & 0xFF);
		dest[destPos+3] = (byte) ((v >>>  0) & 0xFF);
	}
	
	public static void write(byte[] src, int srcPos, byte[] dest, int destPos, int length) {
		System.arraycopy(src,srcPos,dest,destPos,length); 
	}
	
	public static void write(boolean[] src, int srcPos, byte[] dest, int destPos, int length) {
		while (--length >= 0) {
			writeBoolean(src[srcPos++],dest,destPos);
			destPos += SIZE_OF_BOOLEAN;
		}
	}
	
	public static void write(char[] src, int srcPos, byte[] dest, int destPos, int length) {
		while (--length >= 0) {
			writeChar(src[srcPos++],dest,destPos);
			destPos += SIZE_OF_CHAR;
		}
	}
	
	public static void write(short[] src, int srcPos, byte[] dest, int destPos, int length) {
		while (--length >= 0) {
			writeShort(src[srcPos++],dest,destPos);
			destPos += SIZE_OF_SHORT;
		}
	}
	
	public static void write(int[] src, int srcPos, byte[] dest, int destPos, int length) {
		while (--length >= 0) {
			writeInt(src[srcPos++],dest,destPos);
			destPos += SIZE_OF_INT;
		}
	}
	
	public static void write(float[] src, int srcPos, byte[] dest, int destPos, int length) {
		while (--length >= 0) {
			writeFloat(src[srcPos++],dest,destPos);
			destPos += SIZE_OF_FLOAT;
		}
	}
	
	public static void write(long[] src, int srcPos, byte[] dest, int destPos, int length) {
		while (--length >= 0) {
			writeLong(src[srcPos++],dest,destPos);
			destPos += SIZE_OF_LONG;
		}
	}
	
	public static void write(double[] src, int srcPos, byte[] dest, int destPos, int length) {
		while (--length >= 0) {
			writeDouble(src[srcPos++],dest,destPos);
			destPos += SIZE_OF_DOUBLE;
		}
	}

//	/**
//	  * Writes out the string to the underlying byte array as a
//	  * sequence of bytes. Warning: Each character in the string is written out, in 
//	  * sequence, by ignoring its high eight bits.
//	  */
//	public static void writeBytes(String src, int srcPos, byte[] dest, int destPos, int length) {
//		byte v;
//		while (--length >= 0) {
//			v = (byte) src.charAt(srcPos++);
//			dest[destPos] = v;
//			destPos += SIZE_OF_BYTE;
//		}
//	}
//	
//	public static void writeChars(String src, int srcPos, byte[] dest, int destPos, int length) {
//		char v;
//		while (--length >= 0) {
//			v = src.charAt(srcPos++);
//			writeChar(v, dest, destPos);
//			destPos += SIZE_OF_CHAR;
//		}
//	}
		
}

⌨️ 快捷键说明

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