View Javadoc

1   /*
2    * ====================================================================
3    *
4    *  Licensed to the Apache Software Foundation (ASF) under one or more
5    *  contributor license agreements.  See the NOTICE file distributed with
6    *  this work for additional information regarding copyright ownership.
7    *  The ASF licenses this file to You under the Apache License, Version 2.0
8    *  (the "License"); you may not use this file except in compliance with
9    *  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, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   * ====================================================================
19   *
20   * This software consists of voluntary contributions made by many
21   * individuals on behalf of the Apache Software Foundation.  For more
22   * information on the Apache Software Foundation, please see
23   * <http://www.apache.org/>.
24   *
25   */
26  package org.apache.http.client.entity;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.OutputStream;
31  
32  import org.apache.http.HttpEntity;
33  import org.apache.http.entity.HttpEntityWrapper;
34  
35  /**
36   * Common base class for decompressing {@link HttpEntity} implementations.
37   *
38   * @since 4.1
39   */
40  abstract class DecompressingEntity extends HttpEntityWrapper {
41  
42      /**
43       * Default buffer size.
44       */
45      private static final int BUFFER_SIZE = 1024 * 2;
46  
47      /**
48       * {@link #getContent()} method must return the same {@link InputStream}
49       * instance when DecompressingEntity is wrapping a streaming entity.
50       */
51      private InputStream content;
52  
53      /**
54       * Creates a new {@link DecompressingEntity}.
55       *
56       * @param wrapped
57       *            the non-null {@link HttpEntity} to be wrapped
58       */
59      public DecompressingEntity(final HttpEntity wrapped) {
60          super(wrapped);
61      }
62  
63      abstract InputStream decorate(final InputStream wrapped) throws IOException;
64  
65      private InputStream getDecompressingStream() throws IOException {
66          InputStream in = wrappedEntity.getContent();
67          try {
68              return decorate(in);
69          } catch (IOException ex) {
70              in.close();
71              throw ex;
72          }
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public InputStream getContent() throws IOException {
80          if (wrappedEntity.isStreaming()) {
81              if (content == null) {
82                  content = getDecompressingStream();
83              }
84              return content;
85          } else {
86              return getDecompressingStream();
87          }
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public void writeTo(OutputStream outstream) throws IOException {
95          if (outstream == null) {
96              throw new IllegalArgumentException("Output stream may not be null");
97          }
98          InputStream instream = getContent();
99          try {
100             byte[] buffer = new byte[BUFFER_SIZE];
101             int l;
102             while ((l = instream.read(buffer)) != -1) {
103                 outstream.write(buffer, 0, l);
104             }
105         } finally {
106             instream.close();
107         }
108     }
109 
110 }