view src/sample/pack/PackTest.java @ 3:6a447a456e7b

*** empty log message ***
author fuchita
date Sun, 22 Oct 2006 14:49:07 +0900
parents
children fef687d346bc
line wrap: on
line source

package sample.pack;


import java.nio.*;

public class PackTest {

    static int cmd = 47;
    static  int eid = 1;
    static  int lineno = 100;
    static  int sid = 1;
    static  int seqid = 32767;
    static  int textsiz = 5;
   
    byte REP_INSERT_CMD = 6;
    byte REP_INSERT_ACK_CMD = 7;
    byte REP_JOIN_CMD = 41;
    byte REP_JOIN_ACK_CMD = 42;
    byte REP_PUT_CMD = 45;
    byte REP_PUT_ACK_CMD = 46;
    byte REP_SELECT_CMD = 47;
    byte REP_SELECT_ACK_CMD = 48;
    byte REP_QUIT_CMD = 53;
    byte REP_QUIT_ACK_CMD = 54;

    static byte[] chgBytes( int i ){
        byte[] b = new byte[4] ;
        
        b[0] = (byte)((i >>> 24 ) & 0xFF);
        b[1] = (byte)((i >>> 16 ) & 0xFF);
        b[2] = (byte)((i >>>  8 ) & 0xFF);
        b[3] = (byte)((i >>>  0 ) & 0xFF);
        
        return b;
    }

    public static ByteBuffer pack(int cmd, int sid, int eid, int seqid, int lineno, String text ) {	
	byte[] b_cmd = new byte[3];
	byte[] b_sid = new byte[3]; 
	byte[] b_eid = new byte[3]; 
	byte[] b_seqid = new byte[3]; 
	byte[] b_lineno = new byte[3]; 
	byte[] b_textsiz = new byte[3];
	byte[] textToByte = text.getBytes();
	int textsiz = text.length();

	b_cmd = chgBytes(cmd); b_sid = chgBytes(sid); b_eid = chgBytes(eid);
	b_seqid = chgBytes(seqid); b_lineno = chgBytes(lineno); b_textsiz = chgBytes(textsiz);   

	ByteBuffer buffer = ByteBuffer.allocateDirect(24+textsiz);

	buffer.put(b_cmd); buffer.put(b_sid); buffer.put(b_eid);
	buffer.put(b_seqid); buffer.put(b_lineno); buffer.put(b_textsiz); 
	buffer.put(textToByte);

	return buffer;
    }

    public static void main(String args[]) {
	ByteBuffer testbuf;

	System.out.println("pack unpack test.");
	testbuf = pack(cmd, sid, eid, seqid, lineno, "test");

    }
}