001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.http.entity; 014 015import static org.apache.juneau.common.internal.ArgUtils.*; 016import static org.apache.juneau.common.internal.IOUtils.*; 017 018import java.io.*; 019import java.nio.charset.*; 020import java.util.function.*; 021 022import org.apache.juneau.http.header.*; 023import org.apache.juneau.internal.*; 024 025/** 026 * A streamed, non-repeatable entity that obtains its content from an {@link InputStream}. 027 * 028 * <h5 class='section'>See Also:</h5><ul> 029 * <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a> 030 * </ul> 031 */ 032@FluentSetters 033public class StreamEntity extends BasicHttpEntity { 034 035 //----------------------------------------------------------------------------------------------------------------- 036 // Instance 037 //----------------------------------------------------------------------------------------------------------------- 038 039 private byte[] byteCache; 040 private String stringCache; 041 042 /** 043 * Constructor. 044 */ 045 public StreamEntity() { 046 super(); 047 } 048 049 /** 050 * Constructor. 051 * 052 * @param contentType The entity content type. 053 * @param content The entity contents. 054 */ 055 public StreamEntity(ContentType contentType, InputStream content) { 056 super(contentType, content); 057 } 058 059 /** 060 * Copy constructor. 061 * 062 * @param copyFrom The bean being copied. 063 */ 064 protected StreamEntity(StreamEntity copyFrom) { 065 super(copyFrom); 066 } 067 068 @Override 069 public StreamEntity copy() { 070 return new StreamEntity(this); 071 } 072 073 //----------------------------------------------------------------------------------------------------------------- 074 // Other methods 075 //----------------------------------------------------------------------------------------------------------------- 076 077 private InputStream content() { 078 InputStream is = contentOrElse((InputStream)null); 079 if (is == null) 080 throw new RuntimeException("Input stream is null."); 081 return is; 082 } 083 084 @Override /* AbstractHttpEntity */ 085 public String asString() throws IOException { 086 if (isCached() && stringCache == null) 087 stringCache = read(content(), getCharset()); 088 if (stringCache != null) 089 return stringCache; 090 return read(content()); 091 } 092 093 @Override /* AbstractHttpEntity */ 094 public byte[] asBytes() throws IOException { 095 if (isCached() && byteCache == null) 096 byteCache = readBytes(content(), getMaxLength()); 097 if (byteCache != null) 098 return byteCache; 099 return readBytes(content(), getMaxLength()); 100 } 101 102 @Override /* HttpEntity */ 103 public boolean isRepeatable() { 104 return isCached(); 105 } 106 107 @Override /* HttpEntity */ 108 public long getContentLength() { 109 if (isCached()) 110 return asSafeBytes().length; 111 return super.getContentLength(); 112 } 113 114 @Override /* HttpEntity */ 115 public InputStream getContent() throws IOException { 116 if (isCached()) 117 return new ByteArrayInputStream(asBytes()); 118 return content(); 119 } 120 121 /** 122 * Writes bytes from the {@code InputStream} this entity was constructed 123 * with to an {@code OutputStream}. The content length 124 * determines how many bytes are written. If the length is unknown ({@code -1}), the 125 * stream will be completely consumed (to the end of the stream). 126 */ 127 @Override 128 public void writeTo(OutputStream out) throws IOException { 129 assertArgNotNull("out", out); 130 131 if (isCached()) { 132 out.write(asBytes()); 133 } else { 134 try (InputStream is = getContent()) { 135 pipe(is, out, getMaxLength()); 136 } 137 } 138 } 139 140 @Override /* HttpEntity */ 141 public boolean isStreaming() { 142 return ! isCached(); 143 } 144 145 // <FluentSetters> 146 147 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 148 public StreamEntity setCached() throws IOException{ 149 super.setCached(); 150 return this; 151 } 152 153 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 154 public StreamEntity setCharset(Charset value) { 155 super.setCharset(value); 156 return this; 157 } 158 159 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 160 public StreamEntity setChunked() { 161 super.setChunked(); 162 return this; 163 } 164 165 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 166 public StreamEntity setChunked(boolean value) { 167 super.setChunked(value); 168 return this; 169 } 170 171 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 172 public StreamEntity setContent(Object value) { 173 super.setContent(value); 174 return this; 175 } 176 177 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 178 public StreamEntity setContent(Supplier<?> value) { 179 super.setContent(value); 180 return this; 181 } 182 183 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 184 public StreamEntity setContentEncoding(String value) { 185 super.setContentEncoding(value); 186 return this; 187 } 188 189 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 190 public StreamEntity setContentEncoding(ContentEncoding value) { 191 super.setContentEncoding(value); 192 return this; 193 } 194 195 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 196 public StreamEntity setContentLength(long value) { 197 super.setContentLength(value); 198 return this; 199 } 200 201 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 202 public StreamEntity setContentType(String value) { 203 super.setContentType(value); 204 return this; 205 } 206 207 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 208 public StreamEntity setContentType(ContentType value) { 209 super.setContentType(value); 210 return this; 211 } 212 213 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 214 public StreamEntity setMaxLength(int value) { 215 super.setMaxLength(value); 216 return this; 217 } 218 219 @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */ 220 public StreamEntity setUnmodifiable() { 221 super.setUnmodifiable(); 222 return this; 223 } 224 225 // </FluentSetters> 226}