1.package minaUDP;
2.
3.import java.nio.ByteOrder;
4.import java.nio.charset.CharacterCodingException;
5.
6.import org.apache.mina.core.buffer.IoBuffer;
7.
8./**
9. * iobuffer 转换成String
10. * @Description:TODO
11. * @Date:2012-3-15
12. * @author
13. */
14.public class IoBufferTOString {
15.
16. public static String ioBufferToString(IoBuffer iobuffer){
17. System.out.println("message = " + iobuffer + iobuffer.limit());
18. iobuffer.flip(); //调换buffer当前位置,并将当前位置设置成0
19. byte[] b = new byte[iobuffer.limit()];
20. iobuffer.get(b);
21. //此处用stringbuffer是因为 String类是字符串常量,是不可更改的常量。而StringBuffer是字符串变量,它的对象是可以扩充和修改的。
22. StringBuffer stringBuffer = new StringBuffer();
23.
24. for(int i = 0; i < b.length; i++){
25. System.out.println("====" + b[i]);
26. stringBuffer.append((Byte) b[i]); //可以根据需要自己改变类型
27. System.out.println(b[i] +"---------" +i);
28. }
29. return stringBuffer.toString();
30. }
31.
32. /**
33. * @param args
34. * @param:@param args
35. * @return:void
36. * @author
37. * @throws CharacterCodingException
38. * @Date:2012-3-15
39. */
40. public static void main(String[] args) throws CharacterCodingException{
41. IoBuffer iobuffer = IoBuffer.allocate(8);
42. iobuffer.order(ByteOrder.LITTLE_ENDIAN);
43. iobuffer.putChar('z');
44. iobuffer.putInt(123);
45.
46. String str = ioBufferToString(iobuffer);
47. System.out.println(str);
48.
49. }
50.
51.}
52.
53.
54./**
55. * @author create on 2012-3-15
56. */
java mina iobuffer转换成String
最新推荐文章于 2021-02-13 15:38:12 发布