`

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

    博客分类:
  • mina
 
阅读更多
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();   

分享到:
评论

相关推荐

    mina core 包

    org.apache.mina.core.buffer.IoBuffer mina core 包

    Apache MINA java UDP例子|byte数组

    最近做rfid读写,C#和java都用udp不用厂家的动态库,udp自己写也简单,但是试了一下Apache mina ,接收的不是string,二十byte[] 数组,简单实现了UDP,网上也有例子,但是不是我要的。可用。

    Mina 2.0 User Guide(Mina 2.0 用户指南)

    Chapter 10 - IoBuffer Chapter 11 - Codec Filter Chapter 12 - Executor Filter Chapter 13 - SSL Filter Chapter 14 - Logging Filter Part III - MINA Advanced Chapter 15 - Debugging Chapter 16 - State...

    mina学习的最佳代码

    mina学习的最佳代码

    mina自定义编解码器详解

    许多刚接触mina的朋友,对于mina的编解码器的编写很迷惑.希望这个文档可以帮助朋友们少走弯路。 资源中是一个比较典型的编解码器写法。生成了可执行文件。并对编解码器的代码有详细注释。

    Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)

    Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)Apache Mina Server 2.0中文参考手册V1.0,Apache Mina2.0学习笔记(修订版)

    mina中文开发手册

    Apache mina中文开发手册,socket开源通信框架

    Apache_Mina2.0学习笔记

    最近使用Mina开发一个Java的NIO服务端程序,...IoBuffer常用方法: 19 Demo1:模拟根据文本换行符编解码 20 Demo2:改进Demo1的代码 22 Demo3:自定义协议编解码 31 3.IoHandler接口 50 三. Mina实例 50 四. 其他 50

    mina中文开发手册.pdf

    mina中文开发手册.pdf

    mina使用mina使用mina使用

    mina的使用初步入门mina的使用初步入门mina的使用初步入门

    MINA中文官方教程

    Mina是一个基于NIO的网络框架,使用它编写程序时,可以专注于业务处理,而不用过于关心IO操作

    MINA2.0用户手册中文随笔翻译

    apache-mina-2.0.7 用户手册中文随笔翻译

    mina连接 mina心跳连接 mina断线重连

    mina连接,mina心跳连接,mina断线重连。其中客户端可直接用在android上。根据各方参考资料,经过自己的理解弄出来的。CSDN的资源分太难得了。

    Mina2.0完全剖析,完全自学手册【完整版】.doc

    mina学习资料 引言 1 一. Mina入门 2 ...IoBuffer常用方法: 19 Demo1:模拟根据文本换行符编解码 20 Demo2:改进Demo1的代码 22 Demo3:自定义协议编解码 31 3.IoHandler接口 50 三. Mina实例 50 四. 其他 50

    mina2.0 含11个jar包

    mina-core-2.0.0-M6.jar mina-example-2.0.0-M6.jar mina-filter-codec-netty-2.0.0-M6.jar mina-filter-compression-2.0.0-M6.jar mina-integration-beans-2.0.0-M6.jar mina-integration-jmx-2.0.0-M6.jar mina-...

    mina开发手册

    Apache Mina Server 2.0 中文参考手册 Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP、UDP/IP ...步(Mina 的异步IO 默认使用的是JAVA NIO 作为底层支持)操作的编程模型。

    MINA_API+MINA_DOC+mina

    里面包含mina2.0的api(英文)和mina自学手册,还有mina的开发指导

    Mina2中文文档

    Mina2的中文用户手册,仅供互相学习参考,如有侵权立删

    Apache MINA 2.0 用户指南中英文对照阅读版[带书签]

    中英文版的 pdf 均带有书签,方便读者朋友查阅。 mina_2.0_user_guide_cn.pdf 内容预览: 第一章:入门 第二章:基础知识 第三章:IO 服务 第四章:会话 第五章:过滤器 第六章:传输 第七章:事件处理器 第八章:...

Global site tag (gtag.js) - Google Analytics