changeset 7:db9137964f03

pack unpack
author fuchita
date Sun, 22 Oct 2006 18:05:10 +0900
parents 55632138c7a5
children bf97ccde466d
files src/sample/pack/PackTest.java
diffstat 1 files changed, 39 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/sample/pack/PackTest.java	Sun Oct 22 16:58:45 2006 +0900
+++ b/src/sample/pack/PackTest.java	Sun Oct 22 18:05:10 2006 +0900
@@ -5,12 +5,20 @@
 
 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;
+    private static int cmd = 47;
+    private static  int eid = 1;
+    private static  int lineno = 100;
+    private static  int sid = 1;
+    private static  int seqid = 32767;
+    private static  int textsiz = 5;
+    
+    /*private static int cmd2;
+    private static int eid2;
+    private static int lineno2;
+    private static int sid2;
+    private static int seqid2;
+    private static int textsiz2;
+   */
    
     byte REP_INSERT_CMD = 6;
     byte REP_INSERT_ACK_CMD = 7;
@@ -30,29 +38,31 @@
     	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[] b_cmd = new byte[4];
+    	byte[] b_sid = new byte[4]; 
+    	byte[] b_eid = new byte[4]; 
+    	byte[] b_seqid = new byte[4]; 
+    	byte[] b_lineno = new byte[4]; 
+    	byte[] b_textsiz = new byte[4];
     	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);   
 
+    	System.out.println("pack:" + b_cmd[3] +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno);
+    
     	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;
     }
 
@@ -60,18 +70,18 @@
     	int textsiz = 0;
 
     	buffer.flip();
-    	buffer.getInt(cmd); buffer.getInt(sid); buffer.getInt(eid);
-    	buffer.getInt(seqid); buffer.getInt(lineno); buffer.getInt(textsiz);
+    	
+    	cmd = buffer.getInt(); sid = buffer.getInt(); eid = buffer.getInt();
+    	seqid = buffer.getInt(); lineno = buffer.getInt(); textsiz = buffer.getInt();
     	
-    	byte[] readbyte = new byte[textsiz];
-    	for(int i = 0; i < textsiz; i++){
-			readbyte[i] = buffer.get();
+    	System.out.println("unpack:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno);
+    	//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);
-
+		//}
+    	//text = new String(readbyte, 0, textsiz);
+    	
     }
     
     public static void main(String args[]) {
@@ -79,8 +89,10 @@
 
     	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;
+
+    	System.out.println("check:" + testbuf.get(0)+ ":" +testbuf.get(1)+ ":" +testbuf.get(2)+ ":" +testbuf.get(3));
+    	System.out.println("check:" + testbuf.get(4)+ ":" +testbuf.get(5)+ ":" +testbuf.get(6)+ ":" +testbuf.get(7));
+
     	String string = "";
     	unpack(testbuf,cmd2,sid2,eid2,seqid2,lineno2,string);