Skip to content

Commit 75d33d1

Browse files
authored
Prevent having two different unpacking methods (#548)
* Prevent having two different unpacking methods * Fix loop
1 parent 7f4a388 commit 75d33d1

File tree

2 files changed

+59
-52
lines changed

2 files changed

+59
-52
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
import java.nio.charset.CharsetDecoder;
3333
import java.nio.charset.CoderResult;
3434
import java.nio.charset.CodingErrorAction;
35-
import java.util.ArrayList;
36-
import java.util.HashMap;
37-
import java.util.List;
38-
import java.util.Map;
3935

4036
import static org.msgpack.core.Preconditions.checkNotNull;
4137

@@ -690,22 +686,23 @@ public Variable unpackValue(Variable var)
690686
}
691687
case ARRAY: {
692688
int size = unpackArrayHeader();
693-
List<Value> list = new ArrayList<Value>(size);
689+
Value[] kvs = new Value[size];
694690
for (int i = 0; i < size; i++) {
695-
list.add(unpackValue());
691+
kvs[i] = unpackValue();
696692
}
697-
var.setArrayValue(list);
693+
var.setArrayValue(kvs);
698694
return var;
699695
}
700696
case MAP: {
701697
int size = unpackMapHeader();
702-
Map<Value, Value> map = new HashMap<Value, Value>();
703-
for (int i = 0; i < size; i++) {
704-
Value k = unpackValue();
705-
Value v = unpackValue();
706-
map.put(k, v);
698+
Value[] kvs = new Value[size * 2];
699+
for (int i = 0; i < size * 2; ) {
700+
kvs[i] = unpackValue();
701+
i++;
702+
kvs[i] = unpackValue();
703+
i++;
707704
}
708-
var.setMapValue(map);
705+
var.setMapValue(kvs);
709706
return var;
710707
}
711708
case EXTENSION: {

msgpack-core/src/main/java/org/msgpack/value/Variable.java

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.nio.charset.CharacterCodingException;
3131
import java.nio.charset.CharsetDecoder;
3232
import java.nio.charset.CodingErrorAction;
33+
import java.util.Arrays;
3334
import java.util.Collection;
3435
import java.util.Iterator;
3536
import java.util.List;
@@ -798,6 +799,14 @@ public void writeTo(MessagePacker pk)
798799
//
799800

800801
public Variable setArrayValue(List<Value> v)
802+
{
803+
this.type = Type.LIST;
804+
this.accessor = arrayAccessor;
805+
this.objectValue = v.toArray();
806+
return this;
807+
}
808+
809+
public Variable setArrayValue(Value[] v)
801810
{
802811
this.type = Type.LIST;
803812
this.accessor = arrayAccessor;
@@ -824,29 +833,29 @@ public ArrayValue asArrayValue()
824833
@Override
825834
public ImmutableArrayValue immutableValue()
826835
{
827-
return ValueFactory.newArray(list());
836+
return ValueFactory.newArray(array());
828837
}
829838

830839
@Override
831840
public int size()
832841
{
833-
return list().size();
842+
return array().length;
834843
}
835844

836845
@Override
837846
public Value get(int index)
838847
{
839-
return list().get(index);
848+
return array()[index];
840849
}
841850

842851
@Override
843852
public Value getOrNilValue(int index)
844853
{
845-
List<Value> l = list();
846-
if (l.size() < index && index >= 0) {
854+
Value[] a = array();
855+
if (a.length < index && index >= 0) {
847856
return ValueFactory.newNil();
848857
}
849-
return l.get(index);
858+
return a[index];
850859
}
851860

852861
@Override
@@ -856,21 +865,21 @@ public Iterator<Value> iterator()
856865
}
857866

858867
@Override
859-
@SuppressWarnings("unchecked")
860868
public List<Value> list()
861869
{
862-
return (List<Value>) objectValue;
870+
return Arrays.asList(array());
871+
}
872+
873+
public Value[] array()
874+
{
875+
return (Value[]) objectValue;
863876
}
864877

865878
@Override
866879
public void writeTo(MessagePacker pk)
867880
throws IOException
868881
{
869-
List<Value> l = list();
870-
pk.packArrayHeader(l.size());
871-
for (Value e : l) {
872-
e.writeTo(pk);
873-
}
882+
immutableValue().writeTo(pk);
874883
}
875884
}
876885

@@ -882,7 +891,25 @@ public Variable setMapValue(Map<Value, Value> v)
882891
{
883892
this.type = Type.MAP;
884893
this.accessor = mapAccessor;
885-
this.objectValue = v;
894+
Value[] kvs = new Value[v.size() * 2];
895+
Iterator<Map.Entry<Value, Value>> ite = v.entrySet().iterator();
896+
int i = 0;
897+
while (ite.hasNext()) {
898+
Map.Entry<Value, Value> pair = ite.next();
899+
kvs[i] = pair.getKey();
900+
i++;
901+
kvs[i] = pair.getValue();
902+
i++;
903+
}
904+
this.objectValue = kvs;
905+
return this;
906+
}
907+
908+
public Variable setMapValue(Value[] kvs)
909+
{
910+
this.type = Type.MAP;
911+
this.accessor = mapAccessor;
912+
this.objectValue = kvs;
886913
return this;
887914
}
888915

@@ -905,66 +932,49 @@ public MapValue asMapValue()
905932
@Override
906933
public ImmutableMapValue immutableValue()
907934
{
908-
return ValueFactory.newMap(map());
935+
return ValueFactory.newMap(getKeyValueArray());
909936
}
910937

911938
@Override
912939
public int size()
913940
{
914-
return map().size();
941+
return getKeyValueArray().length / 2;
915942
}
916943

917944
@Override
918945
public Set<Value> keySet()
919946
{
920-
return map().keySet();
947+
return immutableValue().keySet();
921948
}
922949

923950
@Override
924951
public Set<Map.Entry<Value, Value>> entrySet()
925952
{
926-
return map().entrySet();
953+
return immutableValue().entrySet();
927954
}
928955

929956
@Override
930957
public Collection<Value> values()
931958
{
932-
return map().values();
959+
return immutableValue().values();
933960
}
934961

935962
@Override
936963
public Value[] getKeyValueArray()
937964
{
938-
Map<Value, Value> v = map();
939-
Value[] kvs = new Value[v.size() * 2];
940-
Iterator<Map.Entry<Value, Value>> ite = v.entrySet().iterator();
941-
int i = 0;
942-
while (ite.hasNext()) {
943-
Map.Entry<Value, Value> pair = ite.next();
944-
kvs[i] = pair.getKey();
945-
i++;
946-
kvs[i] = pair.getValue();
947-
i++;
948-
}
949-
return kvs;
965+
return (Value[]) objectValue;
950966
}
951967

952-
@SuppressWarnings("unchecked")
953968
public Map<Value, Value> map()
954969
{
955-
return (Map<Value, Value>) objectValue;
970+
return immutableValue().map();
956971
}
957972

958973
@Override
959974
public void writeTo(MessagePacker pk)
960975
throws IOException
961976
{
962-
Map<Value, Value> m = map();
963-
pk.packArrayHeader(m.size());
964-
for (Map.Entry<Value, Value> pair : m.entrySet()) {
965-
pair.getKey().writeTo(pk);
966-
pair.getValue().writeTo(pk);
967-
}
977+
immutableValue().writeTo(pk);
968978
}
969979
}
970980

0 commit comments

Comments
 (0)