www.pudn.com > cmppNeu.rar > MsgHead.java


package cmpp.v2_0; 
 
import java.io.*; 
 
public class MsgHead 
{ 
    public 		byte Head[]	= null; 
    private  	int TotalLength; 
    private  	int CommandID; 
    private  	int Seq; 
 
    /** 
     *  
     * 
     */ 
    public MsgHead() 
    { 
        Head = new byte[CMPP.LEN_CMPP_HEADER]; 
    } 
 
    /** 
     *  
     * @param totalLen 
     * @param cmdId 
     * @param seq 
     */ 
    public MsgHead(int totalLen, int cmdId, int seq) 
    { 
        Head = new byte[CMPP.LEN_CMPP_HEADER]; 
         
        TotalLength = totalLen; 
        CommandID = cmdId; 
        Seq = seq; 
         
        CMPP.BytesCopy(CMPP.IntToBytes4(totalLen), Head, 0, 3, 0); 
        CMPP.BytesCopy(CMPP.IntToBytes4(cmdId), Head, 0, 3, 4); 
        CMPP.BytesCopy(CMPP.IntToBytes4(seq), Head, 0, 3, 8); 
    } 
 
    /** 
     *  
     * @param headBytes 
     */ 
    public MsgHead(byte headBytes[]) 
    { 
        Head = new byte[CMPP.LEN_CMPP_HEADER]; 
 
        //copy 12 byte to head 
        for(int i = 0; i < CMPP.LEN_CMPP_HEADER; i++) 
            Head[i] = headBytes[i]; 
 
        //get header  
        byte tempbytes[] =	new byte[4]; 
        for(int i = 0, j=0; i < 4; i++, j++ )  
        	tempbytes[i] = Head[j]; 
        TotalLength = CMPP.Bytes4ToInt(tempbytes); 
         
        for(int i = 0, j=4; i < 4; i++, j++ )  
        	tempbytes[i] = Head[j]; 
        CommandID = CMPP.Bytes4ToInt(tempbytes); 
 
        for(int i = 0, j=8; i < 4; i++, j++ )  
        	tempbytes[i] = Head[j]; 
        Seq = CMPP.Bytes4ToInt(tempbytes); 
    } 
 
    public int getCommandID() 
    { 
        return CommandID; 
    } 
 
    public int getSeq() 
    { 
        return Seq; 
    } 
 
    public int getTotalLength() 
    { 
        return TotalLength; 
    } 
 
    public int read(InputStream inputstream) 
    { 
        byte headBytes[] = new byte[4]; 
        try { 
			inputstream.read(headBytes); 
            CMPP.BytesCopy(headBytes, Head, 0, 3, 0); 
            TotalLength = CMPP.Bytes4ToInt(headBytes); 
             
			inputstream.read(headBytes); 
            CMPP.BytesCopy(headBytes, Head, 0, 3, 4); 
            CommandID = CMPP.Bytes4ToInt(headBytes); 
             
			inputstream.read(headBytes); 
            CMPP.BytesCopy(headBytes, Head, 0, 3, 8); 
            Seq = CMPP.Bytes4ToInt(headBytes); 
             
            return 0; 
		} catch (IOException exception) { 
            System.out.println(exception.toString()); 
			return -1; 
		} 
 
    } 
    public void setCommandID(int i) 
    { 
        CommandID = i; 
        CMPP.BytesCopy(CMPP.IntToBytes4(i), Head, 0, 3, 4); 
    } 
 
    public void setSeq(int i) 
    { 
        Seq = i; 
        CMPP.BytesCopy(CMPP.IntToBytes4(i), Head, 0, 3, 8); 
    } 
 
    public void setTotalLength(int i) 
    { 
        TotalLength = i; 
        CMPP.BytesCopy(CMPP.IntToBytes4(i), Head, 0, 3, 0); 
    } 
 
    public int write(OutputStream outputstream) 
    { 
        try 
        { 
            outputstream.write(Head); 
        } 
        catch(Exception exception) 
        { 
            System.out.println(exception.toString()); 
            return -1; 
        } 
        return 0; 
    } 
     
    public String toString() { 
    	 
    	StringBuffer sb = new StringBuffer(); 
 
    	sb.append("TotalLength			=" + TotalLength + "\n"); 
    	sb.append("CommandID			=" + CommandID + "\n"); 
    	sb.append("Seq					=" + Seq + "\n"); 
    	 
    	return sb.toString(); 
 
    } 
}