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