- /**
- *将byte[]转换成string
- *@parambutBuffer
- */
- publicstaticStringbyteToString(byte[]b)
- {
- StringBufferstringBuffer=newStringBuffer();
- for(inti=0;i<b.length;i++)
- {
- stringBuffer.append((char)b[i]);
- }
- returnstringBuffer.toString();
- }
- /**
- *将bytebuffer转换成string
- *@paramstr
- */
- publicstaticIoBufferstringToIoBuffer(Stringstr)
- {
- bytebt[]=str.getBytes();
- IoBufferioBuffer=IoBuffer.allocate(bt.length);
- ioBuffer.put(bt,0,bt.length);
- ioBuffer.flip();
- returnioBuffer;
- }
- /**
- *将IoBuffer转换成string
- *@paramstr
- */
- publicstaticIoBufferbyteToIoBuffer(byte[]bt,intlength)
- {
- IoBufferioBuffer=IoBuffer.allocate(length);
- ioBuffer.put(bt,0,length);
- ioBuffer.flip();
- returnioBuffer;
- }
- /**
- *将IoBuffer转换成byte
- *@paramstr
- */
- publicstaticbyte[]ioBufferToByte(Objectmessage)
- {
- if(!(messageinstanceofIoBuffer))
- {
- returnnull;
- }
- IoBufferioBuffer=(IoBuffer)message;
- byte[]b=newbyte[ioBuffer.limit()];
- ioBuffer.get(b);
- returnb;
- }
- /**
- *将IoBuffer转换成string
- *@parambutBuffer
- */
- publicstaticStringioBufferToString(Objectmessage)
- {
- if(!(messageinstanceofIoBuffer))
- {
- return"";
- }
- IoBufferioBuffer=(IoBuffer)message;
- byte[]b=newbyte[ioBuffer.limit()];
- ioBuffer.get(b);
- StringBufferstringBuffer=newStringBuffer();
- for(inti=0;i<b.length;i++)
- {
- stringBuffer.append((char)b[i]);
- }
- returnstringBuffer.toString();
- }
MINA中IoBuffer、byte[]、String之间转换
最新推荐文章于 2021-03-11 22:49:28 发布