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.*;
016
017import java.io.*;
018import java.nio.charset.*;
019import java.util.function.*;
020
021import org.apache.juneau.http.header.*;
022import org.apache.juneau.internal.*;
023
024/**
025 * A repeatable entity that obtains its content from a byte array.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
029 * </ul>
030 */
031@FluentSetters
032public class ByteArrayEntity extends BasicHttpEntity {
033
034   //-----------------------------------------------------------------------------------------------------------------
035   // Static
036   //-----------------------------------------------------------------------------------------------------------------
037
038   private static final byte[] EMPTY = new byte[0];
039
040   //-----------------------------------------------------------------------------------------------------------------
041   // Instance
042   //-----------------------------------------------------------------------------------------------------------------
043
044   /**
045    * Constructor.
046    */
047   public ByteArrayEntity() {
048      super();
049   }
050
051   /**
052    * Constructor.
053    *
054    * @param contentType The entity content type.
055    * @param contents The entity contents.
056    */
057   public ByteArrayEntity(ContentType contentType, byte[] contents) {
058      super(contentType, contents);
059   }
060
061   /**
062    * Copy constructor.
063    *
064    * @param copyFrom The bean being copied.
065    */
066   protected ByteArrayEntity(ByteArrayEntity copyFrom) {
067      super(copyFrom);
068   }
069
070   @Override
071   public ByteArrayEntity copy() {
072      return new ByteArrayEntity(this);
073   }
074
075   //-----------------------------------------------------------------------------------------------------------------
076   // Other methods
077   //-----------------------------------------------------------------------------------------------------------------
078
079   private byte[] content() {
080      return contentOrElse(EMPTY);
081   }
082
083   @Override /* AbstractHttpEntity */
084   public String asString() throws IOException {
085      return new String(content(), getCharset());
086   }
087
088   @Override /* AbstractHttpEntity */
089   public byte[] asBytes() throws IOException {
090      return content();
091   }
092
093   @Override /* HttpEntity */
094   public boolean isRepeatable() {
095      return true;
096   }
097
098   @Override /* HttpEntity */
099   public long getContentLength() {
100      return isSupplied() ? super.getContentLength() : content().length;
101   }
102
103   @Override /* HttpEntity */
104   public InputStream getContent() throws IOException {
105      return new ByteArrayInputStream(content());
106   }
107
108   @Override /* HttpEntity */
109   public void writeTo(OutputStream out) throws IOException {
110      assertArgNotNull("out", out);
111      out.write(content());
112   }
113
114   // <FluentSetters>
115
116   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
117   public ByteArrayEntity setCached() throws IOException{
118      super.setCached();
119      return this;
120   }
121
122   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
123   public ByteArrayEntity setCharset(Charset value) {
124      super.setCharset(value);
125      return this;
126   }
127
128   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
129   public ByteArrayEntity setChunked() {
130      super.setChunked();
131      return this;
132   }
133
134   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
135   public ByteArrayEntity setChunked(boolean value) {
136      super.setChunked(value);
137      return this;
138   }
139
140   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
141   public ByteArrayEntity setContent(Object value) {
142      super.setContent(value);
143      return this;
144   }
145
146   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
147   public ByteArrayEntity setContent(Supplier<?> value) {
148      super.setContent(value);
149      return this;
150   }
151
152   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
153   public ByteArrayEntity setContentEncoding(String value) {
154      super.setContentEncoding(value);
155      return this;
156   }
157
158   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
159   public ByteArrayEntity setContentEncoding(ContentEncoding value) {
160      super.setContentEncoding(value);
161      return this;
162   }
163
164   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
165   public ByteArrayEntity setContentLength(long value) {
166      super.setContentLength(value);
167      return this;
168   }
169
170   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
171   public ByteArrayEntity setContentType(String value) {
172      super.setContentType(value);
173      return this;
174   }
175
176   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
177   public ByteArrayEntity setContentType(ContentType value) {
178      super.setContentType(value);
179      return this;
180   }
181
182   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
183   public ByteArrayEntity setMaxLength(int value) {
184      super.setMaxLength(value);
185      return this;
186   }
187
188   @Override /* GENERATED - org.apache.juneau.http.entity.BasicHttpEntity */
189   public ByteArrayEntity setUnmodifiable() {
190      super.setUnmodifiable();
191      return this;
192   }
193
194   // </FluentSetters>
195}