changeset 4:fef687d346bc

pack unpack test
author fuchita
date Sun, 22 Oct 2006 15:31:48 +0900
parents 6a447a456e7b
children 3ac650d064d5
files src/sample/pack/PackTest.java
diffstat 1 files changed, 46 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/sample/pack/PackTest.java	Sun Oct 22 14:49:07 2006 +0900
+++ b/src/sample/pack/PackTest.java	Sun Oct 22 15:31:48 2006 +0900
@@ -24,43 +24,64 @@
     byte REP_QUIT_ACK_CMD = 54;
 
     static byte[] chgBytes( int i ){
-        byte[] b = new byte[4] ;
+    	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);
+    	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;
+    	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();
+    	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);   
+    	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);
+    	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);
+    	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;
+    	return buffer;
     }
 
-    public static void main(String args[]) {
-	ByteBuffer testbuf;
+    public static void unpack(ByteBuffer buffer, int cmd, int sid, int eid, int seqid, int lineno, String text) {
+    	int textsiz = 0;
 
-	System.out.println("pack unpack test.");
-	testbuf = pack(cmd, sid, eid, seqid, lineno, "test");
+    	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);
+    	
+    }
+    
+    public static void main(String args[]) {
+    	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);
     }
 }