package com.eactive.eai.adapter.socket2.protocol.cert.message;

import com.eactive.eai.adapter.socket2.common.CommonLib;
import com.eactive.eai.adapter.socket2.protocol.ByteMessage;

public class CertMessage extends ByteMessage {
	public static final int LL_LENGTH = 9;
	public static final int COMMON_HEADER_LENGTH = 223;
	public static final int INDIVIDUAL_HEADER_LENGTH = 235;
	
	byte[] header;
	
	public CertMessage() {
		this(new byte[0]);
	}

	public CertMessage(String body) {
		this(body.getBytes());
	}
	
	public CertMessage(byte[] body) {
		this(new byte[COMMON_HEADER_LENGTH + INDIVIDUAL_HEADER_LENGTH], body);
	}
	
	public CertMessage(byte[] header, byte[] body) {
		this.header = header;
		this.value  = body;
		String ll = CommonLib.getFormatString(COMMON_HEADER_LENGTH - LL_LENGTH + INDIVIDUAL_HEADER_LENGTH + body.length, LL_LENGTH);
		System.arraycopy(ll.getBytes(), 0, header, 0, LL_LENGTH);
	}
	
	public byte[] getHeader() {
		return header;
	}
	
	@Override
	public String hexDump() {
		return CommonLib.hexDump(value);
	}
    
    @Override
    public String toString() {
    	StringBuilder sb = new StringBuilder();
    	sb.append("CertMessage - [");
    	sb.append(new String(value));
    	sb.append("]");
    	return sb.toString();
    }
}
