Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

Expand Down Expand Up @@ -690,22 +686,23 @@ public Variable unpackValue(Variable var)
}
case ARRAY: {
int size = unpackArrayHeader();
List<Value> list = new ArrayList<Value>(size);
Value[] kvs = new Value[size];
for (int i = 0; i < size; i++) {
list.add(unpackValue());
kvs[i] = unpackValue();
}
var.setArrayValue(list);
var.setArrayValue(kvs);
return var;
}
case MAP: {
int size = unpackMapHeader();
Map<Value, Value> map = new HashMap<Value, Value>();
for (int i = 0; i < size; i++) {
Value k = unpackValue();
Value v = unpackValue();
map.put(k, v);
Value[] kvs = new Value[size * 2];
for (int i = 0; i < size * 2; ) {
kvs[i] = unpackValue();
i++;
kvs[i] = unpackValue();
i++;
}
var.setMapValue(map);
var.setMapValue(kvs);
return var;
}
case EXTENSION: {
Expand Down
88 changes: 49 additions & 39 deletions msgpack-core/src/main/java/org/msgpack/value/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -798,6 +799,14 @@ public void writeTo(MessagePacker pk)
//

public Variable setArrayValue(List<Value> v)
{
this.type = Type.LIST;
this.accessor = arrayAccessor;
this.objectValue = v.toArray();
return this;
}

public Variable setArrayValue(Value[] v)
{
this.type = Type.LIST;
this.accessor = arrayAccessor;
Expand All @@ -824,29 +833,29 @@ public ArrayValue asArrayValue()
@Override
public ImmutableArrayValue immutableValue()
{
return ValueFactory.newArray(list());
return ValueFactory.newArray(array());
}

@Override
public int size()
{
return list().size();
return array().length;
}

@Override
public Value get(int index)
{
return list().get(index);
return array()[index];
}

@Override
public Value getOrNilValue(int index)
{
List<Value> l = list();
if (l.size() < index && index >= 0) {
Value[] a = array();
if (a.length < index && index >= 0) {
return ValueFactory.newNil();
}
return l.get(index);
return a[index];
}

@Override
Expand All @@ -856,21 +865,21 @@ public Iterator<Value> iterator()
}

@Override
@SuppressWarnings("unchecked")
public List<Value> list()
{
return (List<Value>) objectValue;
return Arrays.asList(array());
}

public Value[] array()
{
return (Value[]) objectValue;
}

@Override
public void writeTo(MessagePacker pk)
throws IOException
{
List<Value> l = list();
pk.packArrayHeader(l.size());
for (Value e : l) {
e.writeTo(pk);
}
immutableValue().writeTo(pk);
}
}

Expand All @@ -882,7 +891,25 @@ public Variable setMapValue(Map<Value, Value> v)
{
this.type = Type.MAP;
this.accessor = mapAccessor;
this.objectValue = v;
Value[] kvs = new Value[v.size() * 2];
Iterator<Map.Entry<Value, Value>> ite = v.entrySet().iterator();
int i = 0;
while (ite.hasNext()) {
Map.Entry<Value, Value> pair = ite.next();
kvs[i] = pair.getKey();
i++;
kvs[i] = pair.getValue();
i++;
}
this.objectValue = kvs;
return this;
}

public Variable setMapValue(Value[] kvs)
{
this.type = Type.MAP;
this.accessor = mapAccessor;
this.objectValue = kvs;
return this;
}

Expand All @@ -905,66 +932,49 @@ public MapValue asMapValue()
@Override
public ImmutableMapValue immutableValue()
{
return ValueFactory.newMap(map());
return ValueFactory.newMap(getKeyValueArray());
}

@Override
public int size()
{
return map().size();
return getKeyValueArray().length / 2;
}

@Override
public Set<Value> keySet()
{
return map().keySet();
return immutableValue().keySet();
}

@Override
public Set<Map.Entry<Value, Value>> entrySet()
{
return map().entrySet();
return immutableValue().entrySet();
}

@Override
public Collection<Value> values()
{
return map().values();
return immutableValue().values();
}

@Override
public Value[] getKeyValueArray()
{
Map<Value, Value> v = map();
Value[] kvs = new Value[v.size() * 2];
Iterator<Map.Entry<Value, Value>> ite = v.entrySet().iterator();
int i = 0;
while (ite.hasNext()) {
Map.Entry<Value, Value> pair = ite.next();
kvs[i] = pair.getKey();
i++;
kvs[i] = pair.getValue();
i++;
}
return kvs;
return (Value[]) objectValue;
}

@SuppressWarnings("unchecked")
public Map<Value, Value> map()
{
return (Map<Value, Value>) objectValue;
return immutableValue().map();
}

@Override
public void writeTo(MessagePacker pk)
throws IOException
{
Map<Value, Value> m = map();
pk.packArrayHeader(m.size());
for (Map.Entry<Value, Value> pair : m.entrySet()) {
pair.getKey().writeTo(pk);
pair.getValue().writeTo(pk);
}
immutableValue().writeTo(pk);
}
}

Expand Down