001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.http.entity;
018
019import static org.apache.juneau.commons.utils.AssertionUtils.*;
020
021import java.io.*;
022import java.nio.charset.*;
023import java.util.function.*;
024
025import org.apache.juneau.http.header.*;
026
027/**
028 * A repeatable entity that obtains its content from a byte array.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a>
032 * </ul>
033 */
034@SuppressWarnings("resource")
035public class ByteArrayEntity extends BasicHttpEntity {
036   private static final byte[] EMPTY = {};
037
038   /**
039    * Constructor.
040    */
041   public ByteArrayEntity() {}
042
043   /**
044    * Constructor.
045    *
046    * @param contentType The entity content type.
047    * @param contents The entity contents.
048    */
049   public ByteArrayEntity(ContentType contentType, byte[] contents) {
050      super(contentType, contents);
051   }
052
053   /**
054    * Copy constructor.
055    *
056    * @param copyFrom The bean being copied.
057    */
058   protected ByteArrayEntity(ByteArrayEntity copyFrom) {
059      super(copyFrom);
060   }
061
062   @Override /* Overridden from AbstractHttpEntity */
063   public byte[] asBytes() throws IOException {
064      return content();
065   }
066
067   @Override /* Overridden from AbstractHttpEntity */
068   public String asString() throws IOException {
069      return new String(content(), getCharset());
070   }
071
072   @Override
073   public ByteArrayEntity copy() {
074      return new ByteArrayEntity(this);
075   }
076
077   @Override /* Overridden from HttpEntity */
078   public InputStream getContent() throws IOException { return new ByteArrayInputStream(content()); }
079
080   @Override /* Overridden from HttpEntity */
081   public long getContentLength() { return isSupplied() ? super.getContentLength() : content().length; }
082
083   @Override /* Overridden from HttpEntity */
084   public boolean isRepeatable() { return true; }
085
086   @Override /* Overridden from BasicHttpEntity */
087   public ByteArrayEntity setCached() throws IOException {
088      super.setCached();
089      return this;
090   }
091
092   @Override /* Overridden from BasicHttpEntity */
093   public ByteArrayEntity setCharset(Charset value) {
094      super.setCharset(value);
095      return this;
096   }
097
098   @Override /* Overridden from BasicHttpEntity */
099   public ByteArrayEntity setChunked() {
100      super.setChunked();
101      return this;
102   }
103
104   @Override /* Overridden from BasicHttpEntity */
105   public ByteArrayEntity setChunked(boolean value) {
106      super.setChunked(value);
107      return this;
108   }
109
110   @Override /* Overridden from BasicHttpEntity */
111   public ByteArrayEntity setContent(Object value) {
112      super.setContent(value);
113      return this;
114   }
115
116   @Override /* Overridden from BasicHttpEntity */
117   public ByteArrayEntity setContent(Supplier<?> value) {
118      super.setContent(value);
119      return this;
120   }
121
122   @Override /* Overridden from BasicHttpEntity */
123   public ByteArrayEntity setContentEncoding(ContentEncoding value) {
124      super.setContentEncoding(value);
125      return this;
126   }
127
128   @Override /* Overridden from BasicHttpEntity */
129   public ByteArrayEntity setContentEncoding(String value) {
130      super.setContentEncoding(value);
131      return this;
132   }
133
134   @Override /* Overridden from BasicHttpEntity */
135   public ByteArrayEntity setContentLength(long value) {
136      super.setContentLength(value);
137      return this;
138   }
139
140   @Override /* Overridden from BasicHttpEntity */
141   public ByteArrayEntity setContentType(ContentType value) {
142      super.setContentType(value);
143      return this;
144   }
145
146   @Override /* Overridden from BasicHttpEntity */
147   public ByteArrayEntity setContentType(String value) {
148      super.setContentType(value);
149      return this;
150   }
151
152   @Override /* Overridden from BasicHttpEntity */
153   public ByteArrayEntity setMaxLength(int value) {
154      super.setMaxLength(value);
155      return this;
156   }
157
158   @Override /* Overridden from BasicHttpEntity */
159   public ByteArrayEntity setUnmodifiable() {
160      super.setUnmodifiable();
161      return this;
162   }
163
164   @Override /* Overridden from HttpEntity */
165   public void writeTo(OutputStream out) throws IOException {
166      assertArgNotNull("out", out);
167      out.write(content());
168   }
169
170   private byte[] content() {
171      return contentOrElse(EMPTY);
172   }
173}