View Javadoc
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.impl.client.cache;
28  
29  import java.io.FilterInputStream;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.io.SequenceInputStream;
34  
35  import org.apache.http.client.cache.Resource;
36  import org.apache.http.entity.AbstractHttpEntity;
37  import org.apache.http.util.Args;
38  
39  class CombinedEntity extends AbstractHttpEntity {
40  
41      private final Resource resource;
42      private final InputStream combinedStream;
43  
44      CombinedEntity(final Resource resource, final InputStream inStream) throws IOException {
45          super();
46          this.resource = resource;
47          this.combinedStream = new SequenceInputStream(
48                  new ResourceStream(resource.getInputStream()), inStream);
49      }
50  
51      @Override
52      public long getContentLength() {
53          return -1;
54      }
55  
56      @Override
57      public boolean isRepeatable() {
58          return false;
59      }
60  
61      @Override
62      public boolean isStreaming() {
63          return true;
64      }
65  
66      @Override
67      public InputStream getContent() throws IOException, IllegalStateException {
68          return this.combinedStream;
69      }
70  
71      @Override
72      public void writeTo(final OutputStream outStream) throws IOException {
73          Args.notNull(outStream, "Output stream");
74          final InputStream inStream = getContent();
75          try {
76              int l;
77              final byte[] tmp = new byte[2048];
78              while ((l = inStream.read(tmp)) != -1) {
79                  outStream.write(tmp, 0, l);
80              }
81          } finally {
82              inStream.close();
83          }
84      }
85  
86      private void dispose() {
87          this.resource.dispose();
88      }
89  
90      class ResourceStream extends FilterInputStream {
91  
92          protected ResourceStream(final InputStream in) {
93              super(in);
94          }
95  
96          @Override
97          public void close() throws IOException {
98              try {
99                  super.close();
100             } finally {
101                 dispose();
102             }
103         }
104 
105     }
106 
107 }