@Test public void testParseInteger() { checkParse(0L, "i0e"); checkParse(11L, "i11e"); checkParse(-749L, "i-749e"); checkParse(9223372036854775807L, "i9223372036854775807e"); checkParse(-9223372036854775808L, "i-9223372036854775808e"); }
private Long parseInteger() throws IOException { StringBuilder sb = new StringBuilder(); while (true) { byte b = readByte(); if (b == 'e') break; boolean ok; if ("".contentEquals(sb)) ok = b == '-' || '0' <= b && b <= '9'; else if ("-".contentEquals(sb)) ok = '1' <= b && b <= '9'; else if ("0".contentEquals(sb)) ok = false; else ok = '0' <= b && b <= '9'; if (!ok) throw new IllegalArgumentException("Unexpected integer character"); sb.append((char)b); } if ("".contentEquals(sb) || "-".contentEquals(sb)) throw new IllegalArgumentException("Invalid integer syntax"); return Long.parseLong(sb.toString()); }
Bencode { private Long parseInteger() throws IOException { StringBuilder sb = new StringBuilder(); while (true) { byte b = readByte(); if (b == 'e') break; boolean ok; if ("".contentEquals(sb)) ok = b == '-' || '0' <= b && b <= '9'; else if ("-".contentEquals(sb)) ok = '1' <= b && b <= '9'; else if ("0".contentEquals(sb)) ok = false; else ok = '0' <= b && b <= '9'; if (!ok) throw new IllegalArgumentException("Unexpected integer character"); sb.append((char)b); } if ("".contentEquals(sb) || "-".contentEquals(sb)) throw new IllegalArgumentException("Invalid integer syntax"); return Long.parseLong(sb.toString()); } }
Bencode { private Long parseInteger() throws IOException { StringBuilder sb = new StringBuilder(); while (true) { byte b = readByte(); if (b == 'e') break; boolean ok; if ("".contentEquals(sb)) ok = b == '-' || '0' <= b && b <= '9'; else if ("-".contentEquals(sb)) ok = '1' <= b && b <= '9'; else if ("0".contentEquals(sb)) ok = false; else ok = '0' <= b && b <= '9'; if (!ok) throw new IllegalArgumentException("Unexpected integer character"); sb.append((char)b); } if ("".contentEquals(sb) || "-".contentEquals(sb)) throw new IllegalArgumentException("Invalid integer syntax"); return Long.parseLong(sb.toString()); } private  Bencode(InputStream in); }
Bencode { private Long parseInteger() throws IOException { StringBuilder sb = new StringBuilder(); while (true) { byte b = readByte(); if (b == 'e') break; boolean ok; if ("".contentEquals(sb)) ok = b == '-' || '0' <= b && b <= '9'; else if ("-".contentEquals(sb)) ok = '1' <= b && b <= '9'; else if ("0".contentEquals(sb)) ok = false; else ok = '0' <= b && b <= '9'; if (!ok) throw new IllegalArgumentException("Unexpected integer character"); sb.append((char)b); } if ("".contentEquals(sb) || "-".contentEquals(sb)) throw new IllegalArgumentException("Invalid integer syntax"); return Long.parseLong(sb.toString()); } 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 { private Long parseInteger() throws IOException { StringBuilder sb = new StringBuilder(); while (true) { byte b = readByte(); if (b == 'e') break; boolean ok; if ("".contentEquals(sb)) ok = b == '-' || '0' <= b && b <= '9'; else if ("-".contentEquals(sb)) ok = '1' <= b && b <= '9'; else if ("0".contentEquals(sb)) ok = false; else ok = '0' <= b && b <= '9'; if (!ok) throw new IllegalArgumentException("Unexpected integer character"); sb.append((char)b); } if ("".contentEquals(sb) || "-".contentEquals(sb)) throw new IllegalArgumentException("Invalid integer syntax"); return Long.parseLong(sb.toString()); } 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);  }