|
15 | 15 | */
|
16 | 16 | package org.asynchttpclient.multipart;
|
17 | 17 |
|
18 |
| -import java.io.UnsupportedEncodingException; |
| 18 | +import java.nio.charset.Charset; |
19 | 19 |
|
20 | 20 | /**
|
21 | 21 | * This class is an adaptation of the Apache HttpClient implementation
|
|
25 | 25 | public class MultipartEncodingUtil {
|
26 | 26 |
|
27 | 27 | public static byte[] getAsciiBytes(String data) {
|
28 |
| - try { |
29 |
| - return data.getBytes("US-ASCII"); |
30 |
| - } catch (UnsupportedEncodingException e) { |
31 |
| - throw new RuntimeException(e); |
32 |
| - } |
| 28 | + return data.getBytes(Charset.forName("US-ASCII")); |
33 | 29 | }
|
34 | 30 |
|
35 | 31 | public static String getAsciiString(final byte[] data) {
|
36 | 32 | if (data == null) {
|
37 |
| - throw new IllegalArgumentException("Parameter may not be null"); |
| 33 | + throw new NullPointerException("data"); |
38 | 34 | }
|
39 | 35 |
|
40 |
| - try { |
41 |
| - return new String(data, "US-ASCII"); |
42 |
| - } catch (UnsupportedEncodingException e) { |
43 |
| - throw new RuntimeException(e); |
44 |
| - } |
| 36 | + return new String(data, Charset.forName("US-ASCII")); |
45 | 37 | }
|
46 | 38 |
|
47 | 39 | public static byte[] getBytes(final String data, String charset) {
|
48 | 40 |
|
49 | 41 | if (data == null) {
|
50 |
| - throw new IllegalArgumentException("data may not be null"); |
| 42 | + throw new NullPointerException("data"); |
51 | 43 | }
|
52 | 44 |
|
53 | 45 | if (charset == null || charset.length() == 0) {
|
54 |
| - throw new IllegalArgumentException("charset may not be null or empty"); |
| 46 | + throw new NullPointerException("charset"); |
55 | 47 | }
|
56 | 48 |
|
57 |
| - try { |
58 |
| - return data.getBytes(charset); |
59 |
| - } catch (UnsupportedEncodingException e) { |
60 |
| - throw new IllegalArgumentException(String.format("Unsupported encoding: %s", charset)); |
61 |
| - } |
| 49 | + return data.getBytes(Charset.forName(charset)); |
62 | 50 | }
|
63 | 51 | }
|
0 commit comments