Skip to content

Commit 37a0137

Browse files
author
Stephane Landelle
committed
Use Charset.forName in order to get unchecked UnsupportedCharsetException
1 parent a1ffdd7 commit 37a0137

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

api/src/main/java/org/asynchttpclient/multipart/MultipartEncodingUtil.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.asynchttpclient.multipart;
1717

18-
import java.io.UnsupportedEncodingException;
18+
import java.nio.charset.Charset;
1919

2020
/**
2121
* This class is an adaptation of the Apache HttpClient implementation
@@ -25,39 +25,27 @@
2525
public class MultipartEncodingUtil {
2626

2727
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"));
3329
}
3430

3531
public static String getAsciiString(final byte[] data) {
3632
if (data == null) {
37-
throw new IllegalArgumentException("Parameter may not be null");
33+
throw new NullPointerException("data");
3834
}
3935

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"));
4537
}
4638

4739
public static byte[] getBytes(final String data, String charset) {
4840

4941
if (data == null) {
50-
throw new IllegalArgumentException("data may not be null");
42+
throw new NullPointerException("data");
5143
}
5244

5345
if (charset == null || charset.length() == 0) {
54-
throw new IllegalArgumentException("charset may not be null or empty");
46+
throw new NullPointerException("charset");
5547
}
5648

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));
6250
}
6351
}

0 commit comments

Comments
 (0)