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.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.io.Serializable;
33  
34  import org.apache.http.Header;
35  import org.apache.http.HttpEntity;
36  import org.apache.http.annotation.Contract;
37  import org.apache.http.annotation.ThreadingBehavior;
38  import org.apache.http.client.cache.HttpCacheEntry;
39  import org.apache.http.protocol.HTTP;
40  import org.apache.http.util.Args;
41  
42  @Contract(threading = ThreadingBehavior.IMMUTABLE)
43  class CacheEntity implements HttpEntity, Serializable {
44  
45      private static final long serialVersionUID = -3467082284120936233L;
46  
47      private final HttpCacheEntry cacheEntry;
48  
49      public CacheEntity(final HttpCacheEntry cacheEntry) {
50          super();
51          this.cacheEntry = cacheEntry;
52      }
53  
54      @Override
55      public Header getContentType() {
56          return this.cacheEntry.getFirstHeader(HTTP.CONTENT_TYPE);
57      }
58  
59      @Override
60      public Header getContentEncoding() {
61          return this.cacheEntry.getFirstHeader(HTTP.CONTENT_ENCODING);
62      }
63  
64      @Override
65      public boolean isChunked() {
66          return false;
67      }
68  
69      @Override
70      public boolean isRepeatable() {
71          return true;
72      }
73  
74      @Override
75      public long getContentLength() {
76          return this.cacheEntry.getResource().length();
77      }
78  
79      @Override
80      public InputStream getContent() throws IOException {
81          return this.cacheEntry.getResource().getInputStream();
82      }
83  
84      @Override
85      public void writeTo(final OutputStream outStream) throws IOException {
86          Args.notNull(outStream, "Output stream");
87          final InputStream inStream = this.cacheEntry.getResource().getInputStream();
88          try {
89              IOUtils.copy(inStream, outStream);
90          } finally {
91              inStream.close();
92          }
93      }
94  
95      @Override
96      public boolean isStreaming() {
97          return false;
98      }
99  
100     @Override
101     public void consumeContent() throws IOException {
102     }
103 
104     @Override
105     public Object clone() throws CloneNotSupportedException {
106         return super.clone();
107     }
108 
109 }