package com.feeyo.net.codec.http;

import java.util.Map;

/**
 * 
 * Http Response encode
 * 
 * @author xuwenfeng
 *
 */
public class HttpResponseEncoder {
	
	private static final String CRLF = "\r\n"; 	
	private static final String SP = " "; 		
	private static final String COLON = ":"; 		
	
	public byte[] encode(HttpResponse response) {
		
		if ( response == null )
			return null;
		
		
		// headline
		StringBuffer buf = new StringBuffer();
		buf.append( response.getHttpVersion() ).append(SP);
        buf.append(String.valueOf(response.getStatusCode())).append(SP);
        buf.append( response.getReasonPhrase() ).append(CRLF);
	    
        
        // headers
        for (Map.Entry<String, String> h : response.headers().entrySet()) {
			buf.append(h.getKey()).append(COLON).append(SP);
			buf.append(h.getValue()).append(CRLF);
		}
        buf.append(CRLF);
        
        // content
        byte[] src1 = buf.toString().getBytes();
        byte[] src2 = response.getContent();
        
        byte[] dest = new byte[ src1.length + src2.length ]; 
        System.arraycopy(src1, 0, dest, 0, src1.length);
        System.arraycopy(src2, 0, dest, dest.length, src2.length);
 
		return dest;
	}
}

@@ -907,20 +907,20 @@
  byte%5B%5D 
-src1
+head
  = buf.t
@@ -956,20 +956,20 @@
  byte%5B%5D 
-src2
+body
  = respo
@@ -999,24 +999,52 @@
    %0A        
+if(body != null) %7B%0A        %09
 byte%5B%5D dest 
@@ -1055,20 +1055,20 @@
 w byte%5B 
-src1
+head
 .length 
@@ -1069,20 +1069,20 @@
 ength + 
-src2
+body
 .length 
@@ -1085,16 +1085,17 @@
 gth %5D; %0A
+%09
         
@@ -1111,20 +1111,20 @@
 raycopy(
-src1
+head
 , 0, des
@@ -1133,12 +1133,12 @@
  0, 
-src1
+head
 .len
@@ -1143,16 +1143,17 @@
 ength);%0A
+%09
         
@@ -1169,20 +1169,20 @@
 raycopy(
-src2
+body
 , 0, des
@@ -1201,12 +1201,12 @@
 th, 
-src2
+body
 .len
@@ -1215,12 +1215,17 @@
 h);%0A
- %0A%09%09
+%09        
 retu
@@ -1233,13 +1233,63 @@
 n dest;%0A
+        %7Delse %7B%0A        %09return head;%0A        %7D%0A %0A
 %09%7D%0A%7D%0A

fix bug