view test/TestUTF8.java @ 324:e235998427a6 before-merge-fix

try to fix merger
author kono
date Sat, 11 Oct 2008 16:31:03 +0900
parents 5b7abc22e61a
children
line wrap: on
line source

//package rep;
package test;

import java.io.IOException;
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.REPCommand;

public class TestUTF8 {
	
	public static ByteBuffer pack(REPCommand command){
    	System.out.println("send command: " + command.toString());
    	ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
    	buffer.clear();  // position = 0 
    	buffer.putInt(command.cmd.id); 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));
		}	
    	buffer.flip();    // limit = current position, position = 0
		return buffer;
	}
	
	public static ByteBuffer packUConv(REPCommand command){		
    	ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*3);
    	buffer.clear();  // position = 0 
    	buffer.putInt(command.cmd.id); 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, false);
		/*} catch (CharacterCodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		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();
		
		//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) {
		int cmd = buffer.getInt();
		int sid = buffer.getInt();
		int eid = buffer.getInt();
		int seqid = buffer.getInt();
		int lineno = buffer.getInt();
		int textsiz = buffer.getInt();

		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();
		}

		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 = "あいうえお、かきくけこ、さしすせそ";
		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(" ");    
	    System.out.println("Encode TEST");
	    
		System.out.println("Plane: ByteBuffer :" +buf1.toString());
		System.out.println("UTF-8: Encoded byteBuffer :" +buf2.toString());
		

		unpack_check(buf1);
		unpackUConv_check(buf2);
	}
}