1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package com.foxinmy.weixin4j.http.apache.mime;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35
36 import com.foxinmy.weixin4j.http.ContentType;
37 import com.foxinmy.weixin4j.http.entity.HttpEntity;
38
39 class MultipartFormEntity implements HttpEntity {
40
41 private final AbstractMultipartForm multipart;
42 private final ContentType contentType;
43 private final long contentLength;
44
45 MultipartFormEntity(final AbstractMultipartForm multipart,
46 final ContentType contentType, final long contentLength) {
47 super();
48 this.multipart = multipart;
49 this.contentType = contentType;
50 this.contentLength = contentLength;
51 }
52
53 AbstractMultipartForm getMultipart() {
54 return this.multipart;
55 }
56
57 public boolean isRepeatable() {
58 return this.contentLength != -1;
59 }
60
61 public boolean isChunked() {
62 return !isRepeatable();
63 }
64
65 public boolean isStreaming() {
66 return !isRepeatable();
67 }
68
69 @Override
70 public long getContentLength() {
71 return this.contentLength;
72 }
73
74 public ContentType getContentType() {
75 return this.contentType;
76 }
77
78 @Override
79 public InputStream getContent() throws IOException {
80 if (this.contentLength < 0) {
81 throw new IllegalArgumentException("Content length is unknown");
82 } else if (this.contentLength > 5 * 1024 * 1024) {
83 throw new IllegalArgumentException("Content length is too long: "
84 + this.contentLength);
85 }
86 final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
87 writeTo(outstream);
88 outstream.flush();
89 return new ByteArrayInputStream(outstream.toByteArray());
90 }
91
92 @Override
93 public void writeTo(final OutputStream outstream) throws IOException {
94 this.multipart.writeTo(outstream);
95 }
96 }