Skip to content

Commit 89f5b60

Browse files
TheLQdampcake
authored andcommitted
Preserve source map key order for torrent files (#6)
* Preserve source map key order * added specific tests for LinkedHashMap ordering
1 parent 74a55e7 commit 89f5b60

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import java.io.PushbackInputStream;
2424
import java.nio.charset.Charset;
2525
import java.util.ArrayList;
26+
import java.util.LinkedHashMap;
2627
import java.util.List;
2728
import java.util.Map;
28-
import java.util.TreeMap;
2929

3030
/**
3131
* InputStream for reading bencoded data.
@@ -190,7 +190,7 @@ public Map<String, Object> readDictionary() throws IOException {
190190
int token = in.read();
191191
validateToken(token, Type.DICTIONARY);
192192

193-
Map<String, Object> map = new TreeMap<String, Object>();
193+
Map<String, Object> map = new LinkedHashMap<String, Object>();
194194
while ((token = in.read()) != Bencode.TERMINATOR) {
195195
checkEOF(token);
196196

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.io.OutputStream;
2121
import java.nio.charset.Charset;
2222
import java.util.Map;
23-
import java.util.SortedMap;
24-
import java.util.TreeMap;
2523

2624
/**
2725
* OutputStream for writing bencoded data.
@@ -157,14 +155,8 @@ private static String encode(final Iterable<?> l) {
157155
return buffer.toString();
158156
}
159157

160-
private static String encode(final Map<?, ?> m) {
161-
if (m == null) throw new NullPointerException("m cannot be null");
162-
163-
Map<?, ?> map;
164-
if (!(m instanceof SortedMap<?, ?>))
165-
map = new TreeMap<Object, Object>(m);
166-
else
167-
map = m;
158+
private static String encode(final Map<?, ?> map) {
159+
if (map == null) throw new NullPointerException("map cannot be null");
168160

169161
StringBuilder buffer = new StringBuilder();
170162
buffer.append(Bencode.DICTIONARY);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.io.ByteArrayOutputStream;
77
import java.util.ArrayList;
88
import java.util.HashMap;
9+
import java.util.LinkedHashMap;
10+
import java.util.TreeMap;
911
import java.util.concurrent.ConcurrentSkipListMap;
1012

1113
import static com.dampcake.bencode.Assert.assertThrows;
@@ -140,7 +142,7 @@ public void run() throws Exception {
140142
@Test
141143
@SuppressWarnings("unchecked")
142144
public void testWriteDictionary() throws Exception {
143-
instance.writeDictionary(new HashMap<Object, Object>() {{
145+
instance.writeDictionary(new LinkedHashMap<Object, Object>() {{
144146
put("string", "value");
145147
put("number", 123456);
146148
put("list", new ArrayList<Object>() {{
@@ -153,7 +155,7 @@ public void testWriteDictionary() throws Exception {
153155
}});
154156
}});
155157

156-
assertEquals("d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee",
158+
assertEquals("d6:string5:value6:numberi123456e4:listl11:list-item-111:list-item-2e4:dictd3:1234:test3:4565:thingee",
157159
new String(out.toByteArray(), instance.getCharset()));
158160
}
159161

@@ -168,7 +170,7 @@ public void testWriteDictionaryEmpty() throws Exception {
168170
public void testWriteDictionaryKeyCastException() throws Exception {
169171
assertThrows(ClassCastException.class, new Runnable() {
170172
public void run() throws Exception {
171-
instance.writeDictionary(new HashMap<Object, Object>() {{
173+
instance.writeDictionary(new TreeMap<Object, Object>() {{
172174
put("string", "value");
173175
put(123, "number-key");
174176
}});

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import java.nio.charset.Charset;
1111
import java.util.ArrayList;
1212
import java.util.HashMap;
13+
import java.util.LinkedHashMap;
1314
import java.util.List;
1415
import java.util.Map;
16+
import java.util.TreeMap;
1517
import java.util.concurrent.ConcurrentSkipListMap;
1618

1719
import static org.hamcrest.core.IsInstanceOf.any;
@@ -354,7 +356,7 @@ public void testWriteListNull() throws Exception {
354356

355357
@Test
356358
public void testWriteDictionary() throws Exception {
357-
byte[] encoded = instance.encode(new HashMap<Object, Object>() {{
359+
byte[] encoded = instance.encode(new LinkedHashMap<Object, Object>() {{
358360
put("string", "value");
359361
put("number", 123456);
360362
put("list", new ArrayList<Object>() {{
@@ -367,7 +369,7 @@ public void testWriteDictionary() throws Exception {
367369
}});
368370
}});
369371

370-
assertEquals("d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee",
372+
assertEquals("d6:string5:value6:numberi123456e4:listl11:list-item-111:list-item-2e4:dictd3:1234:test3:4565:thingee",
371373
new String(encoded, instance.getCharset()));
372374
}
373375

@@ -380,10 +382,9 @@ public void testWriteDictionaryEmpty() throws Exception {
380382

381383
@Test
382384
public void testWriteDictionaryKeyCastException() throws Exception {
383-
exception.expect(BencodeException.class);
384-
exception.expectCause(any(ClassCastException.class));
385+
exception.expect(any(ClassCastException.class));
385386

386-
instance.encode(new HashMap<Object, Object>() {{
387+
instance.encode(new TreeMap<Object, Object>() {{
387388
put("string", "value");
388389
put(123, "number-key");
389390
}});

0 commit comments

Comments
 (0)