MINA中IoBuffer、byte[]、String之间转换

Java代码
  1. /**
  2. *将byte[]转换成string
  3. *@parambutBuffer
  4. */
  5. publicstaticStringbyteToString(byte[]b)
  6. {
  7. StringBufferstringBuffer=newStringBuffer();
  8. for(inti=0;i<b.length;i++)
  9. {
  10. stringBuffer.append((char)b[i]);
  11. }
  12. returnstringBuffer.toString();
  13. }
  14. /**
  15. *将bytebuffer转换成string
  16. *@paramstr
  17. */
  18. publicstaticIoBufferstringToIoBuffer(Stringstr)
  19. {
  20. bytebt[]=str.getBytes();
  21. IoBufferioBuffer=IoBuffer.allocate(bt.length);
  22. ioBuffer.put(bt,0,bt.length);
  23. ioBuffer.flip();
  24. returnioBuffer;
  25. }
  26. /**
  27. *将IoBuffer转换成string
  28. *@paramstr
  29. */
  30. publicstaticIoBufferbyteToIoBuffer(byte[]bt,intlength)
  31. {
  32. IoBufferioBuffer=IoBuffer.allocate(length);
  33. ioBuffer.put(bt,0,length);
  34. ioBuffer.flip();
  35. returnioBuffer;
  36. }
  37. /**
  38. *将IoBuffer转换成byte
  39. *@paramstr
  40. */
  41. publicstaticbyte[]ioBufferToByte(Objectmessage)
  42. {
  43. if(!(messageinstanceofIoBuffer))
  44. {
  45. returnnull;
  46. }
  47. IoBufferioBuffer=(IoBuffer)message;
  48. byte[]b=newbyte[ioBuffer.limit()];
  49. ioBuffer.get(b);
  50. returnb;
  51. }
  52. /**
  53. *将IoBuffer转换成string
  54. *@parambutBuffer
  55. */
  56. publicstaticStringioBufferToString(Objectmessage)
  57. {
  58. if(!(messageinstanceofIoBuffer))
  59. {
  60. return"";
  61. }
  62. IoBufferioBuffer=(IoBuffer)message;
  63. byte[]b=newbyte[ioBuffer.limit()];
  64. ioBuffer.get(b);
  65. StringBufferstringBuffer=newStringBuffer();
  66. for(inti=0;i<b.length;i++)
  67. {
  68. stringBuffer.append((char)b[i]);
  69. }
  70. returnstringBuffer.toString();
  71. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值