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.hc.client5.http.impl.cache;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.io.SequenceInputStream;
34  import java.util.List;
35  import java.util.Set;
36  
37  import org.apache.hc.core5.function.Supplier;
38  import org.apache.hc.core5.http.Header;
39  import org.apache.hc.core5.http.HttpEntity;
40  import org.apache.hc.core5.util.Args;
41  import org.apache.hc.core5.util.ByteArrayBuffer;
42  
43  class CombinedEntity implements HttpEntity {
44  
45      private final HttpEntity entity;
46      private final InputStream combinedStream;
47  
48      CombinedEntity(final HttpEntity entity, final ByteArrayBuffer buf) throws IOException {
49          super();
50          this.entity = entity;
51          this.combinedStream = new SequenceInputStream(
52                  new ByteArrayInputStream(buf.array(), 0, buf.length()),
53                  entity.getContent());
54      }
55  
56      @Override
57      public long getContentLength() {
58          return -1;
59      }
60  
61      @Override
62      public String getContentType() {
63          return entity.getContentType();
64      }
65  
66      @Override
67      public String getContentEncoding() {
68          return entity.getContentEncoding();
69      }
70  
71      @Override
72      public boolean isChunked() {
73          return true;
74      }
75  
76      @Override
77      public boolean isRepeatable() {
78          return false;
79      }
80  
81      @Override
82      public boolean isStreaming() {
83          return true;
84      }
85  
86      @Override
87      public InputStream getContent() throws IOException, IllegalStateException {
88          return this.combinedStream;
89      }
90  
91      @Override
92      public Set<String> getTrailerNames() {
93          return entity.getTrailerNames();
94      }
95  
96      @Override
97      public Supplier<List<? extends Header>> getTrailers() {
98          return entity.getTrailers();
99      }
100 
101     @Override
102     public void writeTo(final OutputStream outStream) throws IOException {
103         Args.notNull(outStream, "Output stream");
104         try (InputStream inStream = getContent()) {
105             int l;
106             final byte[] tmp = new byte[2048];
107             while ((l = inStream.read(tmp)) != -1) {
108                 outStream.write(tmp, 0, l);
109             }
110         }
111     }
112 
113     @Override
114     public void close() throws IOException {
115         try {
116             combinedStream.close();
117         } finally {
118             entity.close();
119         }
120     }
121 
122 }