一个服务端为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))处引发)和算术益处异常请各位高手帮忙看看,是否是在高低位转换的时候出现了问题。谢谢!!

解决方案 »

  1.   

    too longjava 不懂飘过
      

  2.   

    这样测试看看byte[] bytearr = new byte[utflen];
    string s = Encoding.UTF8.GetString(bytearr);
      

  3.   

    不行的,我已经试过了,得到"\0\0\0"这样的东西,因为服务端是JAVA编写,所以流的数据高低位是相反的,必须进行转换才行!!
      

  4.   

    to:hertcloud(·£孙子兵法£·) 
    不太清楚你说的特定的格式是什么,不过流当中的数据都是由客户端向服务端发送某个状态码,然后由服务端从数据库或内存中找到客户端所需要的数据发送给客户端!!
      

  5.   

    ((char)(((c & 0x0F) << 12) |                                                  ((char2 & 0x3F) << 6) |                                                  ((char3 & 0x3F) << 0)));c为int 型的..是否在转换成char时,超出了界限
      

  6.   

    so 看你代码这个对你也许有用
    http://blog.csdn.net/red_angelx/archive/2006/10/25/1350095.aspx
    写网络程序的时候多个数据的push pop很麻烦所以我写了个通用bytebuffer类,不过不是很完善,也没时间改
      

  7.   

    to Red_angelX(八戒)
    你写的这个类正好是我之前要用却找不到的类,但后来我用了MemoryStream来解决问题,好象MemoryStream不支持数据压缩的功能!!
      

  8.   

    恩  MemoryStream配合BinaryReader很方便
      

  9.   

    Red_angelX(八戒),你是否有C#多线程同步的资料?能否给几个连接?
      

  10.   

    资料有,不过是书 叫<<c#线程参考>>