1 package com.foxinmy.weixin4j.http.entity; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 8 import com.foxinmy.weixin4j.http.ContentType; 9 10 public class ByteArrayEntity implements HttpEntity { 11 12 private final ContentType contentType; 13 private final byte[] content; 14 private final int off, len; 15 16 public ByteArrayEntity(byte[] content) { 17 this(content, ContentType.DEFAULT_BINARY); 18 } 19 20 public ByteArrayEntity(byte[] content, ContentType contentType) { 21 this(content, 0, content.length, contentType); 22 } 23 24 public ByteArrayEntity(byte[] content, int off, int len, 25 ContentType contentType) { 26 this.content = content; 27 this.off = off; 28 this.len = len; 29 this.contentType = contentType; 30 } 31 32 @Override 33 public ContentType getContentType() { 34 return contentType; 35 } 36 37 @Override 38 public long getContentLength() { 39 return len; 40 } 41 42 @Override 43 public InputStream getContent() throws IOException { 44 return new ByteArrayInputStream(this.content, this.off, this.len); 45 } 46 47 @Override 48 public void writeTo(OutputStream outstream) throws IOException { 49 outstream.write(this.content, this.off, this.len); 50 outstream.flush(); 51 } 52 }