36
36
* </ul>
37
37
*
38
38
* @author Arjen Poutsma
39
- * @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
40
39
* @since 3.0
40
+ * @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
41
41
*/
42
42
public abstract class UriUtils {
43
43
@@ -84,6 +84,7 @@ public abstract class UriUtils {
84
84
"^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" +
85
85
")?" + PATH_PATTERN + "(\\ ?" + LAST_PATTERN + ")?" );
86
86
87
+
87
88
static {
88
89
// variable names refer to RFC 3986, appendix A
89
90
BitSet alpha = new BitSet (256 );
@@ -183,11 +184,11 @@ public abstract class UriUtils {
183
184
FRAGMENT .set ('?' );
184
185
}
185
186
187
+
186
188
/**
187
- * Encodes the given source URI into an encoded String. All various URI components are encoded according to their
188
- * respective valid character sets.
189
- *
190
- * @param uri the URI to be encoded
189
+ * Encodes the given source URI into an encoded String. All various URI components
190
+ * are encoded according to their respective valid character sets.
191
+ * @param uri the URI to be encoded
191
192
* @param encoding the character encoding to encode to
192
193
* @return the encoded URI
193
194
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
@@ -215,12 +216,10 @@ public static String encodeUri(String uri, String encoding) throws UnsupportedEn
215
216
}
216
217
217
218
/**
218
- * Encodes the given HTTP URI into an encoded String. All various URI components are encoded according to their
219
- * respective valid character sets.
220
- *
221
- * <p><strong>Note</strong> that this method does not support fragments ({@code #}), as these are not supposed to be
222
- * sent to the server, but retained by the client.
223
- *
219
+ * Encodes the given HTTP URI into an encoded String. All various URI components
220
+ * are encoded according to their respective valid character sets.
221
+ * <p><strong>Note</strong> that this method does not support fragments ({@code #}),
222
+ * as these are not supposed to be sent to the server, but retained by the client.
224
223
* @param httpUrl the HTTP URL to be encoded
225
224
* @param encoding the character encoding to encode to
226
225
* @return the encoded URL
@@ -248,31 +247,25 @@ public static String encodeHttpUrl(String httpUrl, String encoding) throws Unsup
248
247
}
249
248
250
249
/**
251
- * Encodes the given source URI components into an encoded String. All various URI components are optional, but encoded according
250
+ * Encodes the given source URI components into an encoded String.
251
+ * All various URI components are optional, but encoded according
252
252
* to their respective valid character sets.
253
- *
254
- * @param scheme the scheme
253
+ * @param scheme the scheme
255
254
* @param authority the authority
256
- * @param userinfo the user info
257
- * @param host the host
258
- * @param port the port
259
- * @param path the path
260
- * @param query the query
261
- * @param fragment the fragment
262
- * @param encoding the character encoding to encode to
255
+ * @param userinfo the user info
256
+ * @param host the host
257
+ * @param port the port
258
+ * @param path the path
259
+ * @param query the query
260
+ * @param fragment the fragment
261
+ * @param encoding the character encoding to encode to
263
262
* @return the encoded URI
264
263
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
265
264
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
266
265
*/
267
- public static String encodeUriComponents (String scheme ,
268
- String authority ,
269
- String userinfo ,
270
- String host ,
271
- String port ,
272
- String path ,
273
- String query ,
274
- String fragment ,
275
- String encoding ) throws UnsupportedEncodingException {
266
+ public static String encodeUriComponents (String scheme , String authority , String userinfo ,
267
+ String host , String port , String path , String query , String fragment , String encoding )
268
+ throws UnsupportedEncodingException {
276
269
277
270
Assert .hasLength (encoding , "'encoding' must not be empty" );
278
271
StringBuilder sb = new StringBuilder ();
@@ -314,8 +307,7 @@ public static String encodeUriComponents(String scheme,
314
307
315
308
/**
316
309
* Encodes the given URI scheme.
317
- *
318
- * @param scheme the scheme to be encoded
310
+ * @param scheme the scheme to be encoded
319
311
* @param encoding the character encoding to encode to
320
312
* @return the encoded scheme
321
313
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -326,7 +318,6 @@ public static String encodeScheme(String scheme, String encoding) throws Unsuppo
326
318
327
319
/**
328
320
* Encodes the given URI user info.
329
- *
330
321
* @param userInfo the user info to be encoded
331
322
* @param encoding the character encoding to encode to
332
323
* @return the encoded user info
@@ -338,8 +329,7 @@ public static String encodeUserInfo(String userInfo, String encoding) throws Uns
338
329
339
330
/**
340
331
* Encodes the given URI host.
341
- *
342
- * @param host the host to be encoded
332
+ * @param host the host to be encoded
343
333
* @param encoding the character encoding to encode to
344
334
* @return the encoded host
345
335
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -350,8 +340,7 @@ public static String encodeHost(String host, String encoding) throws Unsupported
350
340
351
341
/**
352
342
* Encodes the given URI port.
353
- *
354
- * @param port the port to be encoded
343
+ * @param port the port to be encoded
355
344
* @param encoding the character encoding to encode to
356
345
* @return the encoded port
357
346
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -362,8 +351,7 @@ public static String encodePort(String port, String encoding) throws Unsupported
362
351
363
352
/**
364
353
* Encodes the given URI path.
365
- *
366
- * @param path the path to be encoded
354
+ * @param path the path to be encoded
367
355
* @param encoding the character encoding to encode to
368
356
* @return the encoded path
369
357
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -374,8 +362,7 @@ public static String encodePath(String path, String encoding) throws Unsupported
374
362
375
363
/**
376
364
* Encodes the given URI path segment.
377
- *
378
- * @param segment the segment to be encoded
365
+ * @param segment the segment to be encoded
379
366
* @param encoding the character encoding to encode to
380
367
* @return the encoded segment
381
368
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
@@ -386,7 +373,6 @@ public static String encodePathSegment(String segment, String encoding) throws U
386
373
387
374
/**
388
375
* Encodes the given URI query.
389
- *
390
376
* @param query the query to be encoded
391
377
* @param encoding the character encoding to encode to
392
378
* @return the encoded query
@@ -398,9 +384,8 @@ public static String encodeQuery(String query, String encoding) throws Unsupport
398
384
399
385
/**
400
386
* Encodes the given URI query parameter.
401
- *
402
387
* @param queryParam the query parameter to be encoded
403
- * @param encoding the character encoding to encode to
388
+ * @param encoding the character encoding to encode to
404
389
* @return the encoded query parameter
405
390
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
406
391
*/
@@ -410,7 +395,6 @@ public static String encodeQueryParam(String queryParam, String encoding) throws
410
395
411
396
/**
412
397
* Encodes the given URI fragment.
413
- *
414
398
* @param fragment the fragment to be encoded
415
399
* @param encoding the character encoding to encode to
416
400
* @return the encoded fragment
@@ -422,6 +406,7 @@ public static String encodeFragment(String fragment, String encoding) throws Uns
422
406
423
407
private static String encode (String source , String encoding , BitSet notEncoded )
424
408
throws UnsupportedEncodingException {
409
+
425
410
Assert .notNull (source , "'source' must not be null" );
426
411
Assert .hasLength (encoding , "'encoding' must not be empty" );
427
412
@@ -431,11 +416,8 @@ private static String encode(String source, String encoding, BitSet notEncoded)
431
416
432
417
private static byte [] encode (byte [] source , BitSet notEncoded ) {
433
418
Assert .notNull (source , "'source' must not be null" );
434
-
435
419
ByteArrayOutputStream bos = new ByteArrayOutputStream (source .length * 2 );
436
-
437
- for (int i = 0 ; i < source .length ; i ++) {
438
- int b = source [i ];
420
+ for (byte b : source ) {
439
421
if (b < 0 ) {
440
422
b += 256 ;
441
423
}
@@ -468,7 +450,6 @@ private static byte[] encode(byte[] source, BitSet notEncoded) {
468
450
* <li>A sequence "<code>%<i>xy</i></code>" is interpreted as a hexadecimal
469
451
* representation of the character.
470
452
* </ul>
471
- *
472
453
* @param source the source string
473
454
* @param encoding the encoding
474
455
* @return the decoded URI
0 commit comments