Mercurial > hg > RemoteEditor > REPSessionManager
view test/channeltest/StringPacker.java @ 172:a776ec9ed848
*** empty log message ***
author | kono |
---|---|
date | Thu, 28 Aug 2008 18:57:17 +0900 |
parents | 690182302c05 |
children | fc8ee7600cf3 |
line wrap: on
line source
package test.channeltest; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.SocketChannel; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import rep.channel.REPPack; import rep.channel.REPSocketChannel; import rep.channel.REPUnpack; public class StringPacker implements REPPack<String>, REPUnpack<String> { public ByteBuffer packUConv(String log) { int size; ByteBuffer blog = ByteBuffer.allocate(log.length()*3); /* ヘッダ あとでもう一回書き直す */ blog.putInt(0); /* 文字列を追加 */ CharBuffer cb = blog.asCharBuffer(); cb.put(log); /* ヘッダに書き込む情報 */ size = blog.position(); blog.rewind(); /* ヘッダ 文字列の長さ */ blog.putInt(size); blog.rewind(); return blog; /* for(int i=0;i<log.length();i++) { blog.putChar(log.charAt(i)); } blog.flip(); return blog; */ } public void send(String log) { } public String unpackUConv(SocketChannel sc) throws IOException { ByteBuffer bb = ByteBuffer.allocate(10); // ヘッダの読み込み 4Byteのハズ...? bb.limit(4); sc.read(bb); int size = bb.getInt(); // Stringの読み込み bb = ByteBuffer.allocate(size*2); bb.limit(size); sc.read(bb); // Stringに変換して返す return bb.asCharBuffer().toString(); } /* public String unpack(ByteBuffer data){ data.getInt(); //Decode UTF-8 to System Encoding(UTF-16) Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); CharBuffer cb = null; try { cb = decoder.decode(data); } catch (CharacterCodingException e) { } cb.rewind(); data.rewind(); return cb.toString(); }*/ }