Mercurial > hg > RemoteEditor > REPSessionManager
view test/TestUTF8.java @ 41:86a1553028ad
*** empty log message ***
author | pin |
---|---|
date | Sat, 10 Nov 2007 21:35:40 +0900 |
parents | ec1cd5d388f3 |
children | 7bf547c009ed |
line wrap: on
line source
//package rep; package test; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; 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); buffer.putInt(command.string.length()*2); for(int i=0;i<command.string.length();i++) { buffer.putChar(command.string.charAt(i)); } buffer.flip(); // limit = current position, position = 0 return buffer; } 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); 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); Charset charset = Charset.forName("UTF-8"); CharsetEncoder encoder = charset.newEncoder(); //try { encoder.encode(cb, buffer, false); /*} catch (CharacterCodingException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ //byte[] outdata = tmpbuffer.array(); buffer.putInt(pos, (buffer.position()-pos-4)); buffer.rewind(); return buffer; } public static void unpack_check(ByteBuffer buffer) { final int HEADER_SIZE = 24; 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(); 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; try { cb = decoder.decode(buffer); } catch (CharacterCodingException e) { // 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; } public static void main(String[] args) throws IOException{ 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); 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("UTF-8 Encoded byteBuffer :" +buf2.toString()); System.out.println("UTF-8 Encoded text length :" +buf2.capacity()); //unpack_check(buf1); unpack_check(buf2); } }