File tree Expand file tree Collapse file tree 3 files changed +12
-4
lines changed
main/java/com/dampcake/bencode
test/java/com/dampcake/bencode Expand file tree Collapse file tree 3 files changed +12
-4
lines changed Original file line number Diff line number Diff line change 2222import java .nio .ByteBuffer ;
2323import java .nio .charset .Charset ;
2424import java .util .Map ;
25+ import java .util .SortedMap ;
26+ import java .util .TreeMap ;
2527
2628/**
2729 * OutputStream for writing bencoded data.
@@ -184,8 +186,14 @@ private byte[] encode(final Iterable<?> l) throws IOException {
184186 return buffer .toByteArray ();
185187 }
186188
187- private byte [] encode (final Map <?, ?> map ) throws IOException {
188- if (map == null ) throw new NullPointerException ("map cannot be null" );
189+ private byte [] encode (final Map <?, ?> m ) throws IOException {
190+ if (m == null ) throw new NullPointerException ("m cannot be null" );
191+
192+ Map <?, ?> map ;
193+ if (!(m instanceof SortedMap <?, ?>))
194+ map = new TreeMap <>(m );
195+ else
196+ map = m ;
189197
190198 ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
191199 buffer .write (Bencode .DICTIONARY );
Original file line number Diff line number Diff line change @@ -181,7 +181,7 @@ public void testWriteDictionary() throws Exception {
181181 }});
182182 }});
183183
184- assertEquals ("d6:string5:value6:numberi123456e4: listl11:list-item-111:list-item-2e4:dictd3:1234:test3:4565:thingee " ,
184+ assertEquals ("d4:dictd3:1234:test3:4565:thinge4: listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee " ,
185185 new String (out .toByteArray (), instance .getCharset ()));
186186 }
187187
Original file line number Diff line number Diff line change @@ -415,7 +415,7 @@ public void testWriteDictionary() throws Exception {
415415 }});
416416 }});
417417
418- assertEquals ("d6:string5:value6:numberi123456e4: listl11:list-item-111:list-item-2e4:dictd3:1234:test3:4565:thingee " ,
418+ assertEquals ("d4:dictd3:1234:test3:4565:thinge4: listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee " ,
419419 new String (encoded , instance .getCharset ()));
420420 }
421421
You can’t perform that action at this time.
0 commit comments