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