C#读取二进制流的问题,撒高分求助!
一个服务端为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))处引发)和算术益处异常
请各位高手帮忙看看,是否是在高低位转换的时候出现了问题。谢谢!!
[解决办法]
这样测试看看
byte[] bytearr = new byte[utflen];
string s = Encoding.UTF8.GetString(bytearr);
[解决办法]
楼主 这样的写法
应该是按照 特定的 格式 对流 进行处理
是不是那里 格式 定义错了
[解决办法]
((char)(((c & 0x0F) < < 12) | ((char2 & 0x3F) < < 6) | ((char3 & 0x3F) < < 0)));
c为int 型的..
是否在转换成char时,超出了界限
[解决办法]
so 看你代码这个对你也许有用
http://blog.csdn.net/red_angelx/archive/2006/10/25/1350095.aspx
写网络程序的时候多个数据的push pop很麻烦所以我写了个通用bytebuffer类,不过不是很完善,也没时间改