Mercurial > hg > RemoteEditor > REPSessionManager
changeset 34:689622193437
add UTF-8 pack method -> packUConv()
author | fuchita |
---|---|
date | Sat, 10 Nov 2007 18:13:38 +0900 |
parents | 1b05995d3118 |
children | eb02fde61a69 |
files | rep/REPPacketSend.java |
diffstat | 1 files changed, 32 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/rep/REPPacketSend.java Sat Nov 10 18:13:14 2007 +0900 +++ b/rep/REPPacketSend.java Sat Nov 10 18:13:38 2007 +0900 @@ -2,7 +2,12 @@ import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.CharBuffer; import java.nio.channels.SocketChannel; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; + +import remoteeditor.command.REPCommand; public class REPPacketSend { SocketChannel socketchannel; @@ -25,6 +30,33 @@ return buffer; } + public ByteBuffer packUConv(REPCommand command){ + System.out.println("send command byUTF8: " + command.toString()); + ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*5); + buffer.clear(); // position = 0 + buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); + buffer.putInt(command.seq); buffer.putInt(command.lineno); + + int pos = buffer.position(); + buffer.putInt(0); + + //Encode to UTF8 + CharBuffer cb = CharBuffer.wrap(command.string); + Charset charset = Charset.forName("UTF-8"); + CharsetEncoder encoder = charset.newEncoder(); + try { + encoder.encode(cb, buffer, true); + } catch (IllegalStateException e) { + e.printStackTrace(); + } + + //Encoded string length set + buffer.putInt(pos, (buffer.position()-pos-4)); + buffer.flip(); + + return buffer; + } + public void send(REPCommand command){ try { socketchannel.write(pack(command));