Mercurial > hg > RemoteEditor > REPSessionManager
changeset 42:7bf547c009ed
UTF-8 TEST Program
author | fuchita |
---|---|
date | Sat, 10 Nov 2007 22:11:07 +0900 |
parents | 86a1553028ad |
children | 24829e1969a0 |
files | test/TestUTF8.java |
diffstat | 1 files changed, 40 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/test/TestUTF8.java Sat Nov 10 21:35:40 2007 +0900 +++ b/test/TestUTF8.java Sat Nov 10 22:11:07 2007 +0900 @@ -13,18 +13,15 @@ import rep.REP; import rep.REPCommand; - -//import remoteeditor.command.REPCommand; - public class TestUTF8 { public static ByteBuffer pack(REPCommand command){ - //command.setString(command.string + ":temp:123456"); //あとで書き直す System.out.println("send command: " + command.toString()); ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2); buffer.clear(); // position = 0 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); buffer.putInt(command.seq); buffer.putInt(command.lineno); + System.out.println("Plane: Set REPComand textlen(Byte) : " + command.string.length()*2); buffer.putInt(command.string.length()*2); for(int i=0;i<command.string.length();i++) { buffer.putChar(command.string.charAt(i)); @@ -34,27 +31,16 @@ } public static ByteBuffer packUConv(REPCommand command){ - //command.setString(command.string + ":temp:123456"); //あとで書き直す - System.out.println("send command byUTF8: " + command.toString()); - ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*5); + ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*3); buffer.clear(); // position = 0 buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid); buffer.putInt(command.seq); buffer.putInt(command.lineno); - //buffer.putInt(command.string.length()*2); - int pos = buffer.position(); buffer.putInt(0); - /*for(int i=0;i<command.string.length();i++) { - buffer.putChar(command.string.charAt(i)); - }*/ - //buffer.position(pos); // limit = current position, position = 0 - - //Encode to UTF8 - //CharBuffer cb = buffer.asCharBuffer(); - CharBuffer cb = CharBuffer.wrap(command.string); - + //Encode to UTF8 + CharBuffer cb = CharBuffer.wrap(command.string); Charset charset = Charset.forName("UTF-8"); CharsetEncoder encoder = charset.newEncoder(); //try { @@ -63,16 +49,31 @@ // TODO Auto-generated catch block e.printStackTrace(); }*/ - //byte[] outdata = tmpbuffer.array(); - + System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4)); buffer.putInt(pos, (buffer.position()-pos-4)); buffer.rewind(); - return buffer; } - public static void unpack_check(ByteBuffer buffer) { + String text = ""; + int cmd = buffer.getInt(); + int sid = buffer.getInt(); + int eid = buffer.getInt(); + int seqid = buffer.getInt(); + int lineno = buffer.getInt(); + int textsiz = buffer.getInt(); + + ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); + //textBuffer.rewind(); + for(int i=0;i<(textsiz/2);i++) { + text +=buffer.getChar(); + } + String string = text; + System.out.println("Plane: => cmd:"+cmd+" sid:"+sid+" eid:"+eid+"seqid:"+seqid+" lineno:"+lineno+" textsiz:" +textsiz+" text: "+string); + } + + public static void unpackUConv_check(ByteBuffer buffer) { final int HEADER_SIZE = 24; String text = ""; int cmd = buffer.getInt(); @@ -81,13 +82,7 @@ int seqid = buffer.getInt(); int lineno = buffer.getInt(); int textsiz = buffer.getInt(); - System.out.println("textsiz:" +textsiz); - //CharBuffer cb = buffer.asCharBuffer(); - /*for(int i=0;i<textsiz;i++) { - text +=buffer.getChar(); - }*/ - //CharBuffer cb = CharBuffer.allocate(textsiz); - //CharBuffer cb = buffer.asCharBuffer(); + Charset charset1 = Charset.forName("UTF-8"); CharsetDecoder decoder = charset1.newDecoder(); CharBuffer cb = null; @@ -97,37 +92,30 @@ // TODO Auto-generated catch block e.printStackTrace(); } - - System.out.println("cb siz:" +cb.length()); - //cb.flip(); - //String string = text; - System.out.println(cb.toString()); - //getSocket(string); - //REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); - //System.out.println("received command: " + repcommand.toString()); - //getSocket(repcommand); - //System.out.println("received command: " + repcommand.toString()); - //return repcommand; + + String string = cb.toString(); + System.out.println("UTF-8: => cmd:"+cmd+" sid:"+sid+" eid:"+eid+"seqid:"+seqid+" lineno:"+lineno+" textsiz:" +textsiz+" text: "+string); + //System.out.println("string size:"+string.length()); + //System.out.println(string); } public static void main(String[] args) throws IOException{ - String text = "あおいえおかきくけこ"; + String text = "あいうえお、かきくけこ、さしすせそ"; REP rep; - REPCommand utf8buf = new REPCommand(0, 0, 0, 0, 0, 0, text); - REPCommand buf = new REPCommand(0, 0, 0, 0, 0, 0, text); + REPCommand utf8buf = new REPCommand(1, 2, 3, 4, 5, 0, text); + REPCommand buf = new REPCommand(1, 2, 3, 4, 5, 0, text); ByteBuffer buf1,buf2; buf1 = pack(buf); buf2 = packUConv(utf8buf); - - System.out.println("Encode TEST1."); - System.out.println("Plane ByteBuffer :" +buf1.toString()); - System.out.println("Plane ByteBuffer length :" +buf1.capacity()); + System.out.println(" "); + System.out.println("Encode TEST"); + + System.out.println("Plane: ByteBuffer :" +buf1.toString()); + System.out.println("UTF-8: Encoded byteBuffer :" +buf2.toString()); + - System.out.println("UTF-8 Encoded byteBuffer :" +buf2.toString()); - System.out.println("UTF-8 Encoded text length :" +buf2.capacity()); - - //unpack_check(buf1); - unpack_check(buf2); + unpack_check(buf1); + unpackUConv_check(buf2); } }