Mercurial > hg > RemoteEditor > REPSessionManager
diff rep/channel/REPPacketSend.java @ 147:4ff68518e9ca
*** empty log message ***
author | kent |
---|---|
date | Wed, 27 Aug 2008 23:38:21 +0900 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rep/channel/REPPacketSend.java Wed Aug 27 23:38:21 2008 +0900 @@ -0,0 +1,74 @@ +package rep.channel; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; + +import rep.REPCommand; +import rep.channel.REPPack; + + +public class REPPacketSend implements REPPack<REPCommand> { + REPSocketChannel<REPCommand> socketchannel; + // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 + final int CHAR_ORDER = 5; + + public REPPacketSend(REPSocketChannel<REPCommand> channel){ + socketchannel = channel; + } + + /* (non-Javadoc) + * @see rep.REPPack#packUConv(rep.REPCommand) + */ + public ByteBuffer packUConv(REPCommand command){ + System.out.println("send command byUTF8: " + command.toString()); + if(command.string == null){ + command.setString("test"); + } + ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string.length()*CHAR_ORDER)); + 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 + int length = (buffer.position() -pos) -4; + System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4)); + if(length < 0) { + length = 0; + } + buffer.putInt(pos, length); + + buffer.limit(24+length); + buffer.rewind(); + + return buffer; + } + + /* (non-Javadoc) + * @see rep.REPPack#send(rep.REPCommand) + */ + public void send(REPCommand command){ + try { + //socketchannel.write(pack(command)); + socketchannel.write(packUConv(command)); + //System.out.println(command.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } +}