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 org.apache.hc.client5.http.entity.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 import java.util.List;
36 import java.util.Set;
37
38 import org.apache.hc.core5.function.Supplier;
39 import org.apache.hc.core5.http.ContentTooLongException;
40 import org.apache.hc.core5.http.ContentType;
41 import org.apache.hc.core5.http.Header;
42 import org.apache.hc.core5.http.HttpEntity;
43
44 class MultipartFormEntity implements HttpEntity {
45
46 private final AbstractMultipartFormat multipart;
47 private final ContentType contentType;
48 private final long contentLength;
49
50 MultipartFormEntity(
51 final AbstractMultipartFormat multipart,
52 final ContentType contentType,
53 final long contentLength) {
54 super();
55 this.multipart = multipart;
56 this.contentType = contentType;
57 this.contentLength = contentLength;
58 }
59
60 AbstractMultipartFormat getMultipart() {
61 return this.multipart;
62 }
63
64 @Override
65 public boolean isRepeatable() {
66 return this.contentLength != -1;
67 }
68
69 @Override
70 public boolean isChunked() {
71 return !isRepeatable();
72 }
73
74 @Override
75 public boolean isStreaming() {
76 return !isRepeatable();
77 }
78
79 @Override
80 public long getContentLength() {
81 return this.contentLength;
82 }
83
84 @Override
85 public String getContentType() {
86 return this.contentType != null ? this.contentType.toString() : null;
87 }
88
89 @Override
90 public String getContentEncoding() {
91 return null;
92 }
93
94 @Override
95 public InputStream getContent() throws IOException {
96 if (this.contentLength < 0) {
97 throw new ContentTooLongException("Content length is unknown");
98 } else if (this.contentLength > 25 * 1024) {
99 throw new ContentTooLongException("Content length is too long: " + this.contentLength);
100 }
101 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
102 writeTo(outStream);
103 outStream.flush();
104 return new ByteArrayInputStream(outStream.toByteArray());
105 }
106
107 @Override
108 public void writeTo(final OutputStream outStream) throws IOException {
109 this.multipart.writeTo(outStream);
110 }
111
112 @Override
113 public Supplier<List<? extends Header>> getTrailers() {
114 return null;
115 }
116
117 @Override
118 public Set<String> getTrailerNames() {
119 return null;
120 }
121
122 @Override
123 public void close() throws IOException {
124 }
125
126 }