一个服务端为JAVA编写,客户端需要用C#编写的软件,在C#接受JAVA输出流发来的数据时,流当中有字符串,现在要把字符串读取出来,就需要进行转换,代码如下:
/**
* 读入一个已使用 UTF-8 格式编码的字符串
* param BinaryReader input
* return string
*/
public string ReadJavaUTF(BinaryReader input)
{
StringBuilder str = null;
try
{
int utflen = 0;
utflen = ReadJavaUnsignedShort(input);
str = new StringBuilder(utflen);
byte[] bytearr = new byte[utflen];
int c, char2, char3;
int count = 0; ReadJavaFully(input, bytearr, 0, utflen); while (count < utflen)
{
byte a = bytearr[count];
c = (int)bytearr[count] & 0xff;
switch (c >> 4)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
count++;
str.Append((char)c);
break;
case 12:
case 13:
count += 2;
if (count > utflen)
throw new Exception();
char2 = (int)bytearr[count - 1];
if ((char2 & 0xC0) != 0x80)
throw new Exception();
str.Append((char)(((c & 0x1F) << 6) | (char2 & 0x3F)));
break;
case 14:
count += 3;
if (count > utflen)
throw new Exception();
char2 = (int)bytearr[count - 2];
char3 = (int)bytearr[count - 1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
throw new Exception();
str.Append((char)(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0)));
break;
default:
throw new OutOfMemoryException();
}
}
}
catch (OutOfMemoryException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine(e.Data);
Console.WriteLine(e.InnerException);
Console.WriteLine(e.Source);
Console.WriteLine(e.TargetSite);
}
return str.ToString();
}
/**
* 从输入流中读取 len 个字节
* param BinaryReader input,byte[] b,int off,int len
*
*/
public static void ReadJavaFully(BinaryReader input, byte[] b, int off, int len)
{
if(len<0)
{
throw new Exception();
}
int n = 0;
if(n < len)
{
int count = input.Read(b, off + n, len - n);
if(count<0)
{
throw new Exception();
}
else
{
n += count;
}
}
}
/**
* 读取两个输入字节,并返回 0 到 65535 范围内的一个 int 值
* param BinaryReader input
* return int
*/
public static int ReadJavaUnsignedShort(BinaryReader input){
int ch1 = input.ReadByte();
int ch2 = input.ReadByte();
if ((ch1 | ch2) < 0)
throw new Exception();
return (ch1 << 8) + (ch2 << 0);
}现在出现异常:
未处理的异常: System.Exception: 引发类型为“System.Exception”的异常。(此异常引发位置在ReadJavaUTF方法的case 14里的str.Append((char)(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));处)
在 HQClient.gnnt.util.IO.InputStreamConvert.ReadJavaUTF(BinaryReader input)
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
此异常不是唯一的,有时候还会抛出OutOfMemoryException异常(在if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))处引发)和算术益处异常请各位高手帮忙看看,是否是在高低位转换的时候出现了问题。谢谢!!
/**
* 读入一个已使用 UTF-8 格式编码的字符串
* param BinaryReader input
* return string
*/
public string ReadJavaUTF(BinaryReader input)
{
StringBuilder str = null;
try
{
int utflen = 0;
utflen = ReadJavaUnsignedShort(input);
str = new StringBuilder(utflen);
byte[] bytearr = new byte[utflen];
int c, char2, char3;
int count = 0; ReadJavaFully(input, bytearr, 0, utflen); while (count < utflen)
{
byte a = bytearr[count];
c = (int)bytearr[count] & 0xff;
switch (c >> 4)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
count++;
str.Append((char)c);
break;
case 12:
case 13:
count += 2;
if (count > utflen)
throw new Exception();
char2 = (int)bytearr[count - 1];
if ((char2 & 0xC0) != 0x80)
throw new Exception();
str.Append((char)(((c & 0x1F) << 6) | (char2 & 0x3F)));
break;
case 14:
count += 3;
if (count > utflen)
throw new Exception();
char2 = (int)bytearr[count - 2];
char3 = (int)bytearr[count - 1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
throw new Exception();
str.Append((char)(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0)));
break;
default:
throw new OutOfMemoryException();
}
}
}
catch (OutOfMemoryException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine(e.Data);
Console.WriteLine(e.InnerException);
Console.WriteLine(e.Source);
Console.WriteLine(e.TargetSite);
}
return str.ToString();
}
/**
* 从输入流中读取 len 个字节
* param BinaryReader input,byte[] b,int off,int len
*
*/
public static void ReadJavaFully(BinaryReader input, byte[] b, int off, int len)
{
if(len<0)
{
throw new Exception();
}
int n = 0;
if(n < len)
{
int count = input.Read(b, off + n, len - n);
if(count<0)
{
throw new Exception();
}
else
{
n += count;
}
}
}
/**
* 读取两个输入字节,并返回 0 到 65535 范围内的一个 int 值
* param BinaryReader input
* return int
*/
public static int ReadJavaUnsignedShort(BinaryReader input){
int ch1 = input.ReadByte();
int ch2 = input.ReadByte();
if ((ch1 | ch2) < 0)
throw new Exception();
return (ch1 << 8) + (ch2 << 0);
}现在出现异常:
未处理的异常: System.Exception: 引发类型为“System.Exception”的异常。(此异常引发位置在ReadJavaUTF方法的case 14里的str.Append((char)(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));处)
在 HQClient.gnnt.util.IO.InputStreamConvert.ReadJavaUTF(BinaryReader input)
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
此异常不是唯一的,有时候还会抛出OutOfMemoryException异常(在if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))处引发)和算术益处异常请各位高手帮忙看看,是否是在高低位转换的时候出现了问题。谢谢!!
string s = Encoding.UTF8.GetString(bytearr);
不太清楚你说的特定的格式是什么,不过流当中的数据都是由客户端向服务端发送某个状态码,然后由服务端从数据库或内存中找到客户端所需要的数据发送给客户端!!
http://blog.csdn.net/red_angelx/archive/2006/10/25/1350095.aspx
写网络程序的时候多个数据的push pop很麻烦所以我写了个通用bytebuffer类,不过不是很完善,也没时间改
你写的这个类正好是我之前要用却找不到的类,但后来我用了MemoryStream来解决问题,好象MemoryStream不支持数据压缩的功能!!