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