Skip to content

Commit 5e5eb22

Browse files
author
Adam Peck
authored
Merge pull request #3 from EonTechnology/master
Fix encoding on locales with non-arabic digits (as Hindi (India))
2 parents 9d1b339 + 59af4df commit 5e5eb22

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.OutputStream;
2121
import java.nio.charset.Charset;
22+
import java.util.Locale;
2223
import java.util.Map;
2324
import java.util.SortedMap;
2425
import java.util.TreeMap;
@@ -126,13 +127,13 @@ public void writeDictionary(final Map<?, ?> m) throws IOException {
126127
private static String encode(final String s) {
127128
if (s == null) throw new NullPointerException("s cannot be null");
128129

129-
return String.format("%d%s%s", s.length(), Bencode.SEPARATOR, s);
130+
return String.format(Locale.ENGLISH, "%d%s%s", s.length(), Bencode.SEPARATOR, s);
130131
}
131132

132133
private static String encode(final Number n) {
133134
if (n == null) throw new NullPointerException("n cannot be null");
134135

135-
return String.format("%s%d%s", Bencode.NUMBER, n.longValue(), Bencode.TERMINATOR);
136+
return String.format(Locale.ENGLISH, "%s%d%s", Bencode.NUMBER, n.longValue(), Bencode.TERMINATOR);
136137
}
137138

138139
private static String encode(final Iterable<?> l) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package com.dampcake.bencode;
22

3+
import org.junit.AfterClass;
34
import org.junit.Before;
45
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.junit.runners.Parameterized;
58

69
import java.io.ByteArrayOutputStream;
710
import java.util.ArrayList;
11+
import java.util.Collection;
812
import java.util.HashMap;
13+
import java.util.LinkedList;
14+
import java.util.Locale;
915
import java.util.concurrent.ConcurrentSkipListMap;
1016

1117
import static com.dampcake.bencode.Assert.assertThrows;
@@ -16,13 +22,35 @@
1622
*
1723
* @author Adam Peck
1824
*/
25+
@RunWith(Parameterized.class)
1926
public class BencodeOutputStreamTest {
2027

28+
private static Locale startLocale = Locale.getDefault();
29+
30+
@Parameterized.Parameter
31+
public Locale testLocale;
32+
33+
@AfterClass
34+
public static void restore() throws Exception {
35+
Locale.setDefault(startLocale);
36+
}
37+
38+
@Parameterized.Parameters(name = "{index}: {0}")
39+
public static Collection<Object[]> data() {
40+
LinkedList<Object[]> res = new LinkedList<Object[]>();
41+
for (Locale locale : Locale.getAvailableLocales()) {
42+
res.add(new Object[] {locale});
43+
}
44+
return res;
45+
}
46+
2147
private ByteArrayOutputStream out;
2248
private BencodeOutputStream instance;
2349

2450
@Before
2551
public void setUp() {
52+
Locale.setDefault(testLocale);
53+
2654
out = new ByteArrayOutputStream();
2755
instance = new BencodeOutputStream(out);
2856
}

0 commit comments

Comments
 (0)