csharpdatamarshalinputstreamimpl.cs

来自「SRI international 发布的OAA框架软件」· CS 代码 · 共 370 行

CS
370
字号
using System;
using System.Net.Sockets;

namespace jnb.com.sri.sedc.javanetbridge
{
	public class CSharpDataMarshalInputStreamImpl : CSharpDataMarshalInputStream
	{
		private Socket socket;
		private byte[] tmpBuffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
		private char[] tmpCharArray = new char[] { '0' };

		public CSharpDataMarshalInputStreamImpl(Socket socket) 
		{
			this.socket = socket;
		}

		public byte readByte()
		{
			readByte1D(tmpBuffer, 0, 1);
			return tmpBuffer[0];
		}

		public short readShort()
		{
			readByte1D(tmpBuffer, 0, 2);
			return (short)((tmpBuffer[0] << 8) + (tmpBuffer[1] << 0));
		}

		public int readInt()
		{
			readByte1D(tmpBuffer, 0, 4);
			return ((tmpBuffer[0] << 24) + (tmpBuffer[1] << 16) + 
				    (tmpBuffer[2] << 8)  + (tmpBuffer[3] << 0));
		}

		public float readFloat()
		{
			return (float)readDouble();
		}

		public long readLong()
		{
			readByte1D(tmpBuffer, 0, 8);
			return (((long)tmpBuffer[0] << 56) +
				((long)(tmpBuffer[1] & 255) << 48) +
				((long)(tmpBuffer[2] & 255) << 40) +
				((long)(tmpBuffer[3] & 255) << 32) +
				((long)(tmpBuffer[4] & 255) << 24) +
				((tmpBuffer[5] & 255) << 16) +
				((tmpBuffer[6] & 255) <<  8) +
				((tmpBuffer[7] & 255) <<  0));
		}

		public double readDouble()
		{
			long asLong = readLong();
			return BitConverter.Int64BitsToDouble(asLong);
		}

		public char readChar()
		{
			readByte1D(tmpBuffer, 0, 2);
			byte tmp = tmpBuffer[0];
			tmpBuffer[0] = tmpBuffer[1];
			tmpBuffer[1] = tmp;
			char[] ret = System.Text.UnicodeEncoding.Unicode.GetString(tmpBuffer).ToCharArray();
			return ret[0];
		}

		public bool readBool()
		{
			return readByte() != 0;
		}

		public String readString()
		{
			int length = readInt();
			byte[] bytes = new byte[2*length];
			readByte1D(bytes, 0, bytes.Length);
			for (int i = 0; i < bytes.Length; i+=2) 
			{
				byte tmp = bytes[i];
				bytes[i] = bytes[i+1];
				bytes[i+1] = tmp;
			}
			return System.Text.Encoding.Unicode.GetString(bytes);
		}

		public byte[] readByte1D()
		{
			byte[] ret = new byte[readInt()];
			readByte1D(ret, 0, ret.Length);
			return ret;
		}

		public void readByte1D(byte[] data, int off, int len)
		{
			if (len > 0) 
			{
				int total = 0;
				do 
				{
					total += socket.Receive(data, off+total, len-total, SocketFlags.None);
				} while (total < len);
			}
		}

		public short[] readShort1D()
		{
			short[] ret = new short[readInt()];
			for (int i = 0; i < ret.Length; i++) 
			{
				ret[i] = readShort();
			}
			return ret;
		}

		public int[] readInt1D()
		{
			int[] ret = new int[readInt()];
			for (int i = 0; i < ret.Length; i++) 
			{
				ret[i] = readInt();
			}
			return ret;
		}

		public float[] readFloat1D()
		{
			float[] ret = new float[readInt()];
			for (int i = 0; i < ret.Length; i++) 
			{
				ret[i] = readFloat();
			}
			return ret;
		}

		public long[] readLong1D()
		{
			long[] ret = new long[readInt()];
			for (int i = 0; i < ret.Length; i++) 
			{
				ret[i] = readLong();
			}
			return ret;
		}

		public double[] readDouble1D()
		{
			double[] ret = new double[readInt()];
			for (int i = 0; i < ret.Length; i++) 
			{
				ret[i] = readDouble();
			}
			return ret;
		}

		public char[] readChar1D()
		{
			byte[] bytes = new byte[2*readInt()];
			readByte1D(bytes, 0, bytes.Length);
			for (int i = 0; i < bytes.Length; i+=2) 
			{
				byte tmp = bytes[i];
				bytes[i] = bytes[i+1];
				bytes[i+1] = tmp;
			}
			return System.Text.Encoding.Unicode.GetString(bytes).ToCharArray();
		}

		public bool[] readBool1D()
		{
			bool[] ret = new bool[readInt()];
			for (int i = 0; i < ret.Length; i++) 
			{
				ret[i] = readBool();
			}
			return ret;
		}

		public String[] readString1D()
		{
			String[] ret = new String[readInt()];
			for (int i = 0; i < ret.Length; i++) 
			{
				ret[i] = readString();
			}
			return ret;
		}

		public byte[][] readByte2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			byte[][] ret = new byte[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new byte[len2];
				readByte1D(ret[i], 0, len2);
			}
			return ret;
		}

		public short[][] readShort2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			short[][] ret = new short[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new short[len2];
				for (int j = 0; j < len2; j++) {
					ret[i][j] = readShort();
				}
			}
			return ret;
		}

		public int[][] readInt2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			int[][] ret = new int[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new int[len2];
				for (int j = 0; j < len2; j++) {
					ret[i][j] = readInt();
				}
			}
			return ret;
		}

		public float[][] readFloat2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			float[][] ret = new float[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new float[len2];
				for (int j = 0; j < len2; j++) {
					ret[i][j] = readFloat();
				}
			}
			return ret;
		}

		public long[][] readLong2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			long[][] ret = new long[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new long[len2];
				for (int j = 0; j < len2; j++) {
					ret[i][j] = readLong();
				}
			}
			return ret;
		}

		public double[][] readDouble2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			double[][] ret = new double[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new double[len2];
				for (int j = 0; j < len2; j++) {
					ret[i][j] = readDouble();
				}
			}
			return ret;
		}

		public char[][] readChar2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			char[][] ret = new char[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new char[len2];
				for (int j = 0; j < len2; j++) {
					ret[i][j] = readChar();
				}
			}
			return ret;
		}

		public bool[][] readBool2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			bool[][] ret = new bool[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new bool[len2];
				for (int j = 0; j < len2; j++) {
					ret[i][j] = readBool();
				}
			}
			return ret;
		}

		public String[][] readString2D()
		{
			int len = readInt();
			int len2 = readInt();
			if (len < 0) {
				throw new Exception("Received negative length for array: " + len);
			}
			if (len2 < 0) {
				throw new Exception("Received negative length for array: " + len2);
			}
			String[][] ret = new String[len][];
			for (int i = 0; i < len; i++) {
				ret[i] = new String[len2];
				for (int j = 0; j < len2; j++) {
					ret[i][j] = readString();
				}
			}
			return ret;
		}
	}
}

⌨️ 快捷键说明

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