Skip to content

Commit fc5feb8

Browse files
committed
Allow parsing of more number types
Fixes #7
1 parent 13f22d4 commit fc5feb8

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/main/java/com/dampcake/bencode/BencodeInputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.io.InvalidObjectException;
2323
import java.io.PushbackInputStream;
24+
import java.math.BigDecimal;
2425
import java.nio.ByteBuffer;
2526
import java.nio.charset.Charset;
2627
import java.util.ArrayList;
@@ -198,7 +199,7 @@ public Long readNumber() throws IOException {
198199
buffer.append((char) token);
199200
}
200201

201-
return Long.parseLong(buffer.toString());
202+
return new BigDecimal(buffer.toString()).longValue();
202203
}
203204

204205
/**

src/test/java/com/dampcake/bencode/BencodeInputStreamTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ public void run() throws Exception {
243243
assertEquals(0, instance.available());
244244
}
245245

246+
@Test
247+
public void testReadNumberScientificNotation() throws Exception {
248+
instantiate("i-2.9155148901435E+18e");
249+
250+
assertEquals(-2915514890143500000L, instance.readNumber().longValue());
251+
}
252+
246253
@Test
247254
@SuppressWarnings("unchecked")
248255
public void testReadList() throws Exception {

0 commit comments

Comments
 (0)