Mercurial > hg > RemoteEditor > Eclipse
view src/sample/pack/PackTest3.java @ 6:55632138c7a5
add
author | yabiku |
---|---|
date | Sun, 22 Oct 2006 16:58:45 +0900 |
parents | |
children |
line wrap: on
line source
package sample.pack; import java.nio.*; public class PackTest3 { private int cmd = 47; private int eid = 1; private int lineno = 100; private int sid = 1; private int seqid = 32767; private 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; public 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 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(); textsiz = text.length(); System.out.println(cmd); b_cmd = chgBytes(cmd); System.out.println(b_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 void unpack(ByteBuffer buffer, int cmd, int sid, int eid, int seqid, int lineno, String text) { int textsiz = 0; buffer.flip(); buffer.getInt(cmd); buffer.getInt(sid); buffer.getInt(eid); buffer.getInt(seqid); buffer.getInt(lineno); buffer.getInt(textsiz); byte[] readbyte = new byte[textsiz]; for(int i = 0; i < textsiz; i++){ readbyte[i] = buffer.get(); //System.out.println(readbyte[i]); } text = new String(readbyte, 0, textsiz); System.out.println("unpack:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ text); } public void job() { ByteBuffer testbuf = ByteBuffer.allocateDirect(1024); System.out.println("pack unpack test."); testbuf = pack(cmd, sid, eid, seqid, lineno, "test"); int cmd2=0,sid2=0,eid2=0,seqid2=0,lineno2=0; String string = ""; unpack(testbuf,cmd2,sid2,eid2,seqid2,lineno2,string); System.out.println("unpack:" + cmd2 +", "+ sid2 +", "+ eid2 +", "+ seqid2 +", "+ lineno2 +", "+ string); } public static void main(String args[]) { new PackTest3().job(); } }