GZIPClient.java

1     
2    import org.apache.commons.httpclient.HttpClient; 
3    import org.apache.commons.httpclient.methods.InputStreamRequestEntity; 
4    import org.apache.commons.httpclient.methods.PostMethod; 
5    import org.apache.commons.httpclient.methods.RequestEntity; 
6     
7    import java.io.BufferedInputStream; 
8    import java.io.FileInputStream; 
9    import java.io.InputStream; 
10   import java.io.OutputStream; 
11   import java.io.PipedInputStream; 
12   import java.io.PipedOutputStream; 
13   import java.util.zip.GZIPOutputStream; 
14    
15   /** 
16    * @author Julius Davies 
17    * @author http://juliusdavies.ca/ 
18    * @since 22-Mar-2006 
19    */ 
20   public class GZIPClient 
21   { 
22    
23      public static void main( String[] args ) throws Exception 
24      { 
25         // The unzipped file to be uploaded as a POST is supplied 
26         // as the first argument. 
27          
28         final PipedOutputStream pipeOut = new PipedOutputStream(); 
29         final PipedInputStream pipeIn = new PipedInputStream( pipeOut ); 
30         final InputStream fileIn = new FileInputStream( args[ 0 ] ); 
31         final InputStream in = new BufferedInputStream( fileIn ); 
32         final OutputStream out = new GZIPOutputStream( pipeOut ); 
33    
34         Runnable r = new Runnable() 
35         { 
36            public void run() 
37            { 
38               try 
39               { 
40                  try 
41                  { 
42                     byte[] buf = new byte[ 4096 ]; 
43                     int count = in.read( buf ); 
44                     while ( count > 0 ) 
45                     { 
46                        System.out.println( "GZipping " + count + " bytes." ); 
47                        out.write( buf, 0, count ); 
48                        count = in.read( buf ); 
49                     } 
50                     System.out.println( "Finished gzipping!" ); 
51                  } 
52                  finally 
53                  { 
54                     pipeOut.flush(); 
55                     out.flush(); 
56                     out.close(); 
57                     in.close(); 
58                     fileIn.close(); 
59                  } 
60               } 
61               catch ( Exception e ) 
62               { 
63                  e.printStackTrace(); 
64               } 
65            } 
66         }; 
67          
68         // A separate thread fills the PipedInputStream with the gzipped content. 
69         new Thread( r ).start(); 
70    
71         PostMethod post = new PostMethod( "http://127.0.0.1:8080/gzip/hello" ); 
72         // It's very important to set Transfer-Encoding to "chunked", unless we 
73         // want to pre-zip the file to disk or memory to figure out an exact 
74         // content-length value. 
75         int AUTO = InputStreamRequestEntity.CONTENT_LENGTH_AUTO; 
76         RequestEntity re = new InputStreamRequestEntity( pipeIn, AUTO ); 
77         post.setRequestEntity( re ); 
78         post.setRequestHeader( "Content-Encoding", "gzip" ); 
79    
80         HttpClient client = new HttpClient(); 
81         int statusCode = client.executeMethod( post ); 
82    
83         // Let's see what the server sent back as content. 
84         InputStream response = post.getResponseBodyAsStream(); 
85         response = new BufferedInputStream( response ); 
86         int c = response.read(); 
87         StringBuffer buf = new StringBuffer( 8 * 1024 ); 
88         while ( c >= 0 ) 
89         { 
90            byte b = (byte) c; 
91            buf.append( (char) b ); 
92            c = response.read(); 
93         } 
94         post.releaseConnection(); 
95         System.out.println( "RESPONSE FROM HTTP SERVER:" ); 
96         System.out.println( "------------------------------------------------" ); 
97         System.out.println( buf.toString() ); 
98         System.out.println( "------------------------------------------------" );      
99         System.out.println( "HTTP Status Code: " + statusCode ); 
100     } 
101   
102   
103  }