关于mina iobuffer的字符转换

Java代码    收藏代码
  1. /** 
  2. * 将byte[]转换成string   
  3. * @param butBuffer 
  4. */  
  5. public static String byteToString(byte [] b)  
  6. {  
  7.        StringBuffer stringBuffer = new StringBuffer();  
  8.        for (int i = 0; i < b.length; i++)  
  9.        {  
  10.            stringBuffer.append((char) b [i]);  
  11.        }  
  12.        return stringBuffer.toString();  
  13. }  
  14.   
  15. /** 
  16. * 将bytebuffer转换成string   
  17. * @param str 
  18. */  
  19. public static IoBuffer stringToIoBuffer(String str)  
  20. {  
  21.   
  22.        byte bt[] = str.getBytes();  
  23.   
  24.        IoBuffer ioBuffer = IoBuffer.allocate(bt.length);  
  25.        ioBuffer.put(bt, 0, bt.length);  
  26.        ioBuffer.flip();  
  27.        return ioBuffer;  
  28. }  
  29. /** 
  30. * 将IoBuffer转换成string   
  31. * @param str 
  32. */  
  33. public static IoBuffer byteToIoBuffer(byte [] bt,int length)  
  34. {  
  35.   
  36.        IoBuffer ioBuffer = IoBuffer.allocate(length);  
  37.        ioBuffer.put(bt, 0, length);  
  38.        ioBuffer.flip();  
  39.        return ioBuffer;  
  40. }  
  41. /** 
  42. * 将IoBuffer转换成byte   
  43. * @param str 
  44. */  
  45. public static byte [] ioBufferToByte(Object message)  
  46. {  
  47.       if (!(message instanceof IoBuffer))  
  48.       {  
  49.           return null;  
  50.       }  
  51.       IoBuffer ioBuffer = (IoBuffer)message;  
  52.       byte[] b = new byte[ioBuffer.limit()];  
  53.       ioBuffer.get(b);  
  54.       return b;  
  55. }  
  56. /** 
  57. * 将IoBuffer转换成string   
  58. * @param butBuffer 
  59. */  
  60. public static String ioBufferToString(Object message)  
  61. {  
  62.       if (!(message instanceof IoBuffer))  
  63.       {  
  64.         return "";  
  65.       }  
  66.       IoBuffer ioBuffer = (IoBuffer) message;  
  67.       byte[] b = new byte [ioBuffer.limit()];  
  68.       ioBuffer.get(b);  
  69.       StringBuffer stringBuffer = new StringBuffer();  
  70.   
  71.       for (int i = 0; i < b.length; i++)  
  72.       {  
  73.   
  74.        stringBuffer.append((char) b [i]);  
  75.       }  
  76.        return stringBuffer.toString();  
  77. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值