1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.fileupload2.core;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.file.InvalidPathException;
22
23 import org.apache.commons.io.input.BoundedInputStream;
24
25
26
27
28 class FileItemInputImpl implements FileItemInput {
29
30
31
32
33
34
35 private final FileItemInputIteratorImpl fileItemInputIteratorImpl;
36
37
38
39
40 private final String contentType;
41
42
43
44
45 private final String fieldName;
46
47
48
49
50 private final String fileName;
51
52
53
54
55 private final boolean formField;
56
57
58
59
60 private final InputStream inputStream;
61
62
63
64
65 private boolean inputStreamClosed;
66
67
68
69
70 private FileItemHeaders headers;
71
72
73
74
75
76
77
78
79
80
81
82
83
84 FileItemInputImpl(final FileItemInputIteratorImpl fileItemIterator, final String fileName, final String fieldName, final String contentType,
85 final boolean formField, final long contentLength) throws FileUploadException, IOException {
86 this.fileItemInputIteratorImpl = fileItemIterator;
87 this.fileName = fileName;
88 this.fieldName = fieldName;
89 this.contentType = contentType;
90 this.formField = formField;
91 final var fileSizeMax = fileItemInputIteratorImpl.getFileSizeMax();
92 if (fileSizeMax != -1 && contentLength != -1 && contentLength > fileSizeMax) {
93 throw new FileUploadByteCountLimitException(String.format("The field %s exceeds its maximum permitted size of %s bytes.", fieldName, fileSizeMax),
94 contentLength, fileSizeMax, fileName, fieldName);
95 }
96
97 final var itemInputStream = fileItemInputIteratorImpl.getMultiPartInput().newInputStream();
98 InputStream istream = itemInputStream;
99 if (fileSizeMax != -1) {
100
101
102
103 istream = new BoundedInputStream(istream, fileSizeMax + 1) {
104 @Override
105 protected void onMaxLength(final long sizeMax, final long count) throws IOException {
106 itemInputStream.close(true);
107 throw new FileUploadByteCountLimitException(
108 String.format("The field %s exceeds its maximum permitted size of %s bytes.", fieldName, fileSizeMax), count, fileSizeMax, fileName,
109 fieldName);
110 }
111 };
112 }
113 this.inputStream = istream;
114 }
115
116
117
118
119
120
121 public void close() throws IOException {
122 inputStream.close();
123 inputStreamClosed = true;
124 }
125
126
127
128
129
130
131 @Override
132 public String getContentType() {
133 return contentType;
134 }
135
136
137
138
139
140
141 @Override
142 public String getFieldName() {
143 return fieldName;
144 }
145
146
147
148
149
150
151 @Override
152 public FileItemHeaders getHeaders() {
153 return headers;
154 }
155
156
157
158
159
160
161
162 @Override
163 public InputStream getInputStream() throws IOException {
164 if (inputStreamClosed) {
165 throw new FileItemInput.ItemSkippedException("getInputStream()");
166 }
167 return inputStream;
168 }
169
170
171
172
173
174
175
176
177 @Override
178 public String getName() {
179 return DiskFileItem.checkFileName(fileName);
180 }
181
182
183
184
185
186
187 @Override
188 public boolean isFormField() {
189 return formField;
190 }
191
192
193
194
195
196
197 @Override
198 public FileItemInputImpl setHeaders(final FileItemHeaders headers) {
199 this.headers = headers;
200 return this;
201 }
202
203 }