@Test public void testByteStringToArrayInvalid() { String[] CASES = { "\u0100", "\uFFFF", "\uD800\uDC00", "abc\u0123def", }; for (String cs : CASES) { try { Bencode.byteStringToArray(cs); Assert.fail(); } catch (IllegalArgumentException e) {} } }
public static byte[] byteStringToArray(String str) { byte[] result = new byte[str.length()]; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c > 0xFF) throw new IllegalArgumentException("Character value outside of byte range"); result[i] = (byte)c; } return result; }
Bencode { public static byte[] byteStringToArray(String str) { byte[] result = new byte[str.length()]; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c > 0xFF) throw new IllegalArgumentException("Character value outside of byte range"); result[i] = (byte)c; } return result; } }
Bencode { public static byte[] byteStringToArray(String str) { byte[] result = new byte[str.length()]; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c > 0xFF) throw new IllegalArgumentException("Character value outside of byte range"); result[i] = (byte)c; } return result; } private  Bencode(InputStream in); }
Bencode { public static byte[] byteStringToArray(String str) { byte[] result = new byte[str.length()]; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c > 0xFF) throw new IllegalArgumentException("Character value outside of byte range"); result[i] = (byte)c; } return result; } private  Bencode(InputStream in); static void serialize(Object obj, OutputStream out); static Object parse(InputStream in); static byte[] byteStringToArray(String str); static String arrayToByteString(byte[] arr); }
Bencode { public static byte[] byteStringToArray(String str) { byte[] result = new byte[str.length()]; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c > 0xFF) throw new IllegalArgumentException("Character value outside of byte range"); result[i] = (byte)c; } return result; } private  Bencode(InputStream in); static void serialize(Object obj, OutputStream out); static Object parse(InputStream in); static byte[] byteStringToArray(String str); static String arrayToByteString(byte[] arr);  }