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.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
36 import org.apache.http.ContentTooLongException;
37 import org.apache.http.Header;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.entity.ContentType;
40 import org.apache.http.message.BasicHeader;
41 import org.apache.http.protocol.HTTP;
42
43 @SuppressWarnings("deprecation")
44 class MultipartFormEntity implements HttpEntity {
45
46 private final AbstractMultipartForm multipart;
47 private final Header contentType;
48 private final long contentLength;
49
50 MultipartFormEntity(
51 final AbstractMultipartForm multipart,
52 final ContentType contentType,
53 final long contentLength) {
54 super();
55 this.multipart = multipart;
56 this.contentType = new BasicHeader(HTTP.CONTENT_TYPE, contentType.toString());
57 this.contentLength = contentLength;
58 }
59
60 AbstractMultipartForm 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 Header getContentType() {
86 return this.contentType;
87 }
88
89 @Override
90 public Header getContentEncoding() {
91 return null;
92 }
93
94 @Override
95 public void consumeContent() {
96 }
97
98 @Override
99 public InputStream getContent() throws IOException {
100 if (this.contentLength < 0) {
101 throw new ContentTooLongException("Content length is unknown");
102 } else if (this.contentLength > 25 * 1024) {
103 throw new ContentTooLongException("Content length is too long: " + this.contentLength);
104 }
105 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
106 writeTo(outStream);
107 outStream.flush();
108 return new ByteArrayInputStream(outStream.toByteArray());
109 }
110
111 @Override
112 public void writeTo(final OutputStream outStream) throws IOException {
113 this.multipart.writeTo(outStream);
114 }
115
116 }