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.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 }