38
38
39
39
package org .w3c .css .util ;
40
40
41
+ import org .apache .commons .lang3 .tuple .ImmutablePair ;
42
+ import org .apache .commons .lang3 .tuple .Pair ;
43
+
41
44
import java .io .IOException ;
45
+ import java .nio .charset .StandardCharsets ;
46
+ import java .util .ArrayList ;
42
47
43
48
/**
44
49
* This class collects various encoders and decoders.
@@ -98,16 +103,18 @@ private Codecs() {
98
103
* @param cont_type the content type header (must contain the
99
104
* boundary string).
100
105
* @return an array of name/value pairs, one for each part;
101
- * the name is the 'name' attribute given in the
102
- * Content-Disposition header; the value is either
103
- * the name of the file if a filename attribute was
104
- * found, or the contents of the part.
106
+ * the name is the 'name' attribute given in the
107
+ * Content-Disposition header; the value is either
108
+ * the name of the file if a filename attribute was
109
+ * found, or the contents of the part.
105
110
* @throws IOException If any file operation fails.
106
111
*/
107
- public final static synchronized NVPair [] mpFormDataDecode (byte [] data ,
108
- String cont_type )
112
+ public final static synchronized ArrayList < Pair < String , ?>> mpFormDataDecode (byte [] data ,
113
+ String cont_type )
109
114
throws IOException {
110
115
116
+ ArrayList <Pair <String , ?>> pList = new ArrayList <>();
117
+
111
118
// Find and extract boundary string
112
119
String bndstr = getParameter ("boundary" , cont_type );
113
120
if (bndstr == null ) {
@@ -119,9 +126,9 @@ public final static synchronized NVPair[] mpFormDataDecode(byte[] data,
119
126
boundary = new byte [bndstr .length () + 6 ],
120
127
endbndry = new byte [bndstr .length () + 6 ];
121
128
122
- srtbndry = ("--" + bndstr + "\n " ).getBytes ();
123
- boundary = ("\n --" + bndstr + "\n " ).getBytes ();
124
- endbndry = ("\n --" + bndstr + "--" ).getBytes ();
129
+ srtbndry = ("--" + bndstr + "\n " ).getBytes (StandardCharsets . ISO_8859_1 );
130
+ boundary = ("\n --" + bndstr + "\n " ).getBytes (StandardCharsets . ISO_8859_1 );
131
+ endbndry = ("\n --" + bndstr + "--" ).getBytes (StandardCharsets . ISO_8859_1 );
125
132
126
133
if (debugMode ) {
127
134
System .err .println ("[START OF DATA]" );
@@ -163,7 +170,6 @@ public final static synchronized NVPair[] mpFormDataDecode(byte[] data,
163
170
164
171
start += srtbndry .length ;
165
172
166
- NVPair [] res = new NVPair [10 ];
167
173
boolean done = false ;
168
174
int idx ;
169
175
@@ -314,20 +320,14 @@ public final static synchronized NVPair[] mpFormDataDecode(byte[] data,
314
320
} else { // It's simple data
315
321
value = new String (data , start , end - start );
316
322
}
317
-
318
- if (idx >= res .length ) {
319
- res = Util .resizeArray (res , idx + 10 );
320
- }
321
-
322
- res [idx ] = new NVPair (name , value );
323
+ pList .add (new ImmutablePair <>(name , value ));
323
324
if (debugMode ) {
324
325
System .err .println ("[ADD " + name + ',' + value + ','
325
326
+ value .getClass () + ']' );
326
327
}
327
328
start = end + boundary .length ;
328
329
}
329
-
330
- return Util .resizeArray (res , idx );
330
+ return pList ;
331
331
}
332
332
333
333
@@ -364,22 +364,22 @@ public final static String getParameter(String param, String hdr) {
364
364
pbeg = vend + 1 ;
365
365
}
366
366
while (pbeg < len - 1
367
- && (Util . isWhiteSpace (hdr .charAt (pbeg )) || (hdr .charAt (pbeg ) == ';' ))) {
367
+ && (Character . isWhitespace (hdr .charAt (pbeg )) || (hdr .charAt (pbeg ) == ';' ))) {
368
368
pbeg ++;
369
369
}
370
370
if (pbeg == len - 1 ) return null ;
371
371
pend = hdr .indexOf ('=' , pbeg + 1 ); // get '='
372
372
if (pend == -1 ) return null ;
373
373
vbeg = pend + 1 ;
374
- while (Util . isWhiteSpace (hdr .charAt (--pend ))) ;
374
+ while (Character . isWhitespace (hdr .charAt (--pend ))) ;
375
375
pend ++;
376
376
377
377
if (debugMode ) {
378
378
System .err .println ("[DEBUG] p is [" + hdr .substring (pbeg , pend ) + "]" );
379
379
}
380
380
// mark parameter value
381
381
382
- while (vbeg < len && Util . isWhiteSpace (hdr .charAt (vbeg ))) vbeg ++;
382
+ while (vbeg < len && Character . isWhitespace (hdr .charAt (vbeg ))) vbeg ++;
383
383
if (vbeg == len ) return null ;
384
384
385
385
vend = vbeg ;
@@ -390,7 +390,7 @@ public final static String getParameter(String param, String hdr) {
390
390
} else { // is a simple token
391
391
vend = hdr .indexOf (';' , vbeg );
392
392
if (vend == -1 ) vend = hdr .length ();
393
- while (Util . isWhiteSpace (hdr .charAt (--vend ))) ;
393
+ while (Character . isWhitespace (hdr .charAt (--vend ))) ;
394
394
vend ++;
395
395
}
396
396
if (hdr .regionMatches (true , pbeg , param , 0 , pend - pbeg )) {
0 commit comments