1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
105 }