1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 package org.apache.http.client.entity; 28 29 /* 30 * ==================================================================== 31 * Licensed to the Apache Software Foundation (ASF) under one 32 * or more contributor license agreements. See the NOTICE file 33 * distributed with this work for additional information 34 * regarding copyright ownership. The ASF licenses this file 35 * to you under the Apache License, Version 2.0 (the 36 * "License"); you may not use this file except in compliance 37 * with the License. You may obtain a copy of the License at 38 * 39 * http://www.apache.org/licenses/LICENSE-2.0 40 * 41 * Unless required by applicable law or agreed to in writing, 42 * software distributed under the License is distributed on an 43 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 44 * KIND, either express or implied. See the License for the 45 * specific language governing permissions and limitations 46 * under the License. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>. 53 * 54 */ 55 56 import java.io.IOException; 57 import java.io.InputStream; 58 import java.io.OutputStream; 59 import java.util.zip.GZIPOutputStream; 60 61 import org.apache.http.Header; 62 import org.apache.http.HttpEntity; 63 import org.apache.http.entity.HttpEntityWrapper; 64 import org.apache.http.message.BasicHeader; 65 import org.apache.http.protocol.HTTP; 66 import org.apache.http.util.Args; 67 68 /** 69 * Wrapping entity that compresses content when {@link #writeTo writing}. 70 * 71 * 72 * @since 4.0 73 */ 74 public class GzipCompressingEntity extends HttpEntityWrapper { 75 76 private static final String GZIP_CODEC = "gzip"; 77 78 public GzipCompressingEntity(final HttpEntity entity) { 79 super(entity); 80 } 81 82 @Override 83 public Header getContentEncoding() { 84 return new BasicHeader(HTTP.CONTENT_ENCODING, GZIP_CODEC); 85 } 86 87 @Override 88 public long getContentLength() { 89 return -1; 90 } 91 92 @Override 93 public boolean isChunked() { 94 // force content chunking 95 return true; 96 } 97 98 @Override 99 public InputStream getContent() throws IOException { 100 throw new UnsupportedOperationException(); 101 } 102 103 @Override 104 public void writeTo(final OutputStream outStream) throws IOException { 105 Args.notNull(outStream, "Output stream"); 106 final GZIPOutputStream gzip = new GZIPOutputStream(outStream); 107 wrappedEntity.writeTo(gzip); 108 // Only close output stream if the wrapped entity has been 109 // successfully written out 110 gzip.close(); 111 } 112 113 }