|
16 | 16 | package com.stormpath.sdk.lang; |
17 | 17 |
|
18 | 18 | import java.nio.charset.Charset; |
| 19 | +import java.nio.charset.StandardCharsets; |
19 | 20 | import java.util.ArrayList; |
20 | 21 | import java.util.Arrays; |
21 | 22 | import java.util.Collection; |
@@ -1264,4 +1265,34 @@ public static String arrayToCommaDelimitedString(Object[] arr) { |
1264 | 1265 | return arrayToDelimitedString(arr, ","); |
1265 | 1266 | } |
1266 | 1267 |
|
| 1268 | + /** |
| 1269 | + * Calls {@link String#getBytes(Charset)} |
| 1270 | + * |
| 1271 | + * @param string The string to encode (if null, return null). |
| 1272 | + * @param charset The {@link Charset} to encode the <code>String</code> |
| 1273 | + * @return the encoded bytes |
| 1274 | + * @since 1.2.1 |
| 1275 | + */ |
| 1276 | + private static byte[] getBytes(final String string, final Charset charset) { |
| 1277 | + if (string == null) { |
| 1278 | + return null; |
| 1279 | + } |
| 1280 | + return string.getBytes(charset); |
| 1281 | + } |
| 1282 | + |
| 1283 | + /** |
| 1284 | + * Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte |
| 1285 | + * array. |
| 1286 | + * |
| 1287 | + * @param string the String to encode, may be <code>null</code> |
| 1288 | + * @return encoded bytes, or <code>null</code> if the input string was <code>null</code> |
| 1289 | + * @throws NullPointerException Thrown if {@link StandardCharsets#UTF_8} is not initialized, which should never happen since it is |
| 1290 | + * required by the Java platform specification. |
| 1291 | + * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a> |
| 1292 | + * @since 1.2.1 |
| 1293 | + */ |
| 1294 | + public static byte[] getBytesUtf8(final String string) { |
| 1295 | + return getBytes(string, StandardCharsets.UTF_8); |
| 1296 | + } |
| 1297 | + |
1267 | 1298 | } |
0 commit comments