summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/core/Chunker.java
blob: 8f5ef139db053f36c6e9803f86a665a2d34a198e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

package org.simpleframework.http.core;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Chunker extends FilterOutputStream { 


   private byte[] size = {'0', '0', '0', '0', '0', 
                          '0', '0', '0',  13, 10};


   private byte[] index = {'0', '1', '2', '3', '4', '5','6', '7',
                          '8', '9', 'a', 'b', 'c', 'd','e', 'f'};


   private byte[] zero = {'0', 13, 10, 13, 10};


   public Chunker(OutputStream out){
      super(out);
   }
   
   public void write(int octet) throws IOException {
	   byte[] swap = new byte[1];
	   swap[0] = (byte)octet;
	   write(swap);
   }
   

   public void write(byte[] buf, int off, int len) throws IOException {
      int pos = 7;

      if(len > 0) {
         for(int num = len; num > 0; num >>>= 4){      
            size[pos--] = index[num & 0xf];
         }
         String text = String.format("%s; %s\r\n", Integer.toHexString(len), len);
         
         out.write(text.getBytes("ISO-8859-1"));   
         out.write(buf, off, len);
         out.write(size, 8, 2);
      }
   }
   
   public void close() throws IOException {
	   out.write(zero);
	   out.close();
   }
}