Mina字符转换类MinaUtil

本文介绍了一个用于Apache Mina框架的实用工具类MinaUtil,该工具类提供了多种方法实现不同数据类型之间的转换,例如byte数组与字符串之间的转换、字符串与IoBuffer之间的转换等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用Mina的时候,难免会出现将字符互相转换。

下面是转换类:

 

[java]  view plain copy
 
  1. /** 
  2.  * MinaUtil.java 
  3.  * 版权所有(C) 2012  
  4.  * 创建:cuiran 2012-05-08 15:43:50 
  5.  */  
  6. package com.wpndemo.test;  
  7.   
  8. import org.apache.mina.core.buffer.IoBuffer;  
  9.   
  10. /** 
  11.  * mina iobuffer的字符转换 
  12.  * @author cuiran 
  13.  * @version 1.0.0 
  14.  */  
  15. public class MinaUtil {  
  16.     /** 
  17.     * 将byte[]转换成string   
  18.     * @param butBuffer 
  19.     */  
  20.     public static String byteToString(byte [] b)  
  21.     {  
  22.            StringBuffer stringBuffer = new StringBuffer();  
  23.            for (int i = 0; i < b.length; i++)  
  24.            {  
  25.                stringBuffer.append((char) b [i]);  
  26.            }  
  27.            return stringBuffer.toString();  
  28.     }  
  29.   
  30.     /** 
  31.     * 将bytebuffer转换成string   
  32.     * @param str 
  33.     */  
  34.     public static IoBuffer stringToIoBuffer(String str)  
  35.     {  
  36.   
  37.            byte bt[] = str.getBytes();  
  38.   
  39.            IoBuffer ioBuffer = IoBuffer.allocate(bt.length);  
  40.            ioBuffer.put(bt, 0, bt.length);  
  41.            ioBuffer.flip();  
  42.            return ioBuffer;  
  43.     }  
  44.     /** 
  45.     * 将IoBuffer转换成string   
  46.     * @param str 
  47.     */  
  48.     public static IoBuffer byteToIoBuffer(byte [] bt,int length)  
  49.     {  
  50.   
  51.            IoBuffer ioBuffer = IoBuffer.allocate(length);  
  52.            ioBuffer.put(bt, 0, length);  
  53.            ioBuffer.flip();  
  54.            return ioBuffer;  
  55.     }  
  56.     /** 
  57.     * 将IoBuffer转换成byte   
  58.     * @param str 
  59.     */  
  60.     public static byte [] ioBufferToByte(Object message)  
  61.     {  
  62.           if (!(message instanceof IoBuffer))  
  63.           {  
  64.               return null;  
  65.           }  
  66.           IoBuffer ioBuffer = (IoBuffer)message;  
  67.           byte[] b = new byte[ioBuffer.limit()];  
  68.           ioBuffer.get(b);  
  69.           return b;  
  70.     }  
  71.     /** 
  72.     * 将IoBuffer转换成string   
  73.     * @param butBuffer 
  74.     */  
  75.     public static String ioBufferToString(Object message)  
  76.     {  
  77.           if (!(message instanceof IoBuffer))  
  78.           {  
  79.             return "";  
  80.           }  
  81.           IoBuffer ioBuffer = (IoBuffer) message;  
  82.           byte[] b = new byte [ioBuffer.limit()];  
  83.           ioBuffer.get(b);  
  84.           StringBuffer stringBuffer = new StringBuffer();  
  85.   
  86.           for (int i = 0; i < b.length; i++)  
  87.           {  
  88.   
  89.            stringBuffer.append((char) b [i]);  
  90.           }  
  91.            return stringBuffer.toString();  
  92.     }  
  93.   
  94. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值