view rep/REPCommandPacker.java @ 203:4c0a94836357 simullator-nio-both-worked

*** empty log message ***
author kono
date Sat, 30 Aug 2008 11:21:43 +0900
parents d22384c0026c
children 6b0dd92b8e45
line wrap: on
line source

package rep;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

import rep.channel.REPPack;


public class REPCommandPacker implements REPPack<REPCommand> {
	// JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
	private final int HEADER_SIZE = 24;
	final int CHAR_ORDER = 5;


	/* (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(HEADER_SIZE+(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(HEADER_SIZE+length);
		buffer.rewind();
		
		return buffer;    	
	}


	public REPCommand unpackUConv(SocketChannel sc) throws IOException {
		ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
		long len = 0;
		header.clear();
		len = sc.read(header);
		if(len <= 0){
			return null;
		}
		if (len !=HEADER_SIZE) {
			throw new IOException();
		}
		header.rewind();  // position = 0

		int cmd = header.getInt();
		int sid = header.getInt();
		int eid = header.getInt();
		int seqid = header.getInt();
		int lineno = header.getInt();
		int textsiz = header.getInt();
		
		ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
		
		len = sc.read(textBuffer);
		if(len <= 0){
			return null;
		}
		if (len != textsiz) {
			throw new IOException();
		}
		textBuffer.rewind();

		//Decode UTF-8 to System Encoding(UTF-16) 
		Charset charset = Charset.forName("UTF-8");
		CharsetDecoder decoder = charset.newDecoder();
		CharBuffer cb = null;
		try {
			cb = decoder.decode(textBuffer);
		} catch (CharacterCodingException e) {
			e.printStackTrace();
		}
		cb.rewind();
		
		String string = cb.toString();
		
		textsiz = string.length();

		REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);

		return repcommand;		
	}


}