001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.entity; 029 030import java.io.ByteArrayInputStream; 031import java.io.IOException; 032import java.io.InputStream; 033import java.io.OutputStream; 034 035import org.apache.http.util.Args; 036 037/** 038 * A self contained, repeatable entity that obtains its content from a byte array. 039 * 040 * @since 4.0 041 */ 042public class ByteArrayEntity extends AbstractHttpEntity implements Cloneable { 043 044 /** 045 * @deprecated (4.2) 046 */ 047 @Deprecated 048 protected final byte[] content; 049 private final byte[] b; 050 private final int off, len; 051 052 /** 053 * @since 4.2 054 */ 055 @SuppressWarnings("deprecation") 056 public ByteArrayEntity(final byte[] b, final ContentType contentType) { 057 super(); 058 Args.notNull(b, "Source byte array"); 059 this.content = b; 060 this.b = b; 061 this.off = 0; 062 this.len = this.b.length; 063 if (contentType != null) { 064 setContentType(contentType.toString()); 065 } 066 } 067 068 /** 069 * @since 4.2 070 */ 071 @SuppressWarnings("deprecation") 072 public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) { 073 super(); 074 Args.notNull(b, "Source byte array"); 075 if ((off < 0) || (off > b.length) || (len < 0) || 076 ((off + len) < 0) || ((off + len) > b.length)) { 077 throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length); 078 } 079 this.content = b; 080 this.b = b; 081 this.off = off; 082 this.len = len; 083 if (contentType != null) { 084 setContentType(contentType.toString()); 085 } 086 } 087 088 public ByteArrayEntity(final byte[] b) { 089 this(b, null); 090 } 091 092 public ByteArrayEntity(final byte[] b, final int off, final int len) { 093 this(b, off, len, null); 094 } 095 096 @Override 097 public boolean isRepeatable() { 098 return true; 099 } 100 101 @Override 102 public long getContentLength() { 103 return this.len; 104 } 105 106 @Override 107 public InputStream getContent() { 108 return new ByteArrayInputStream(this.b, this.off, this.len); 109 } 110 111 @Override 112 public void writeTo(final OutputStream outstream) throws IOException { 113 Args.notNull(outstream, "Output stream"); 114 outstream.write(this.b, this.off, this.len); 115 outstream.flush(); 116 } 117 118 119 /** 120 * Tells that this entity is not streaming. 121 * 122 * @return {@code false} 123 */ 124 @Override 125 public boolean isStreaming() { 126 return false; 127 } 128 129 @Override 130 public Object clone() throws CloneNotSupportedException { 131 return super.clone(); 132 } 133 134} // class ByteArrayEntity