- Fixed parsing of byte[]s to not parse them as strings and then

lose precision during the cast. Now it will freak out if the
  "byte" is out of range.
- A bit less hashing in our static initializer.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2782 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray.j.greenwell
2010-04-20 01:50:37 +00:00
parent edf86bdfec
commit 668ad3ec55
@@ -59,6 +59,7 @@ public class ValueMarshaller
protected static Map<Class<?>,Parser> _parsers = new HashMap<Class<?>,Parser>(); protected static Map<Class<?>,Parser> _parsers = new HashMap<Class<?>,Parser>();
static { static {
Parser p;
// we can parse strings // we can parse strings
_parsers.put(String.class, new Parser() { _parsers.put(String.class, new Parser() {
public Object parse (String source) throws Exception { public Object parse (String source) throws Exception {
@@ -67,61 +68,67 @@ public class ValueMarshaller
}); });
// and bytes // and bytes
_parsers.put(Byte.TYPE, new Parser() { p = new Parser() {
public Object parse (String source) throws Exception { public Object parse (String source) throws Exception {
return Byte.valueOf(source); return Byte.valueOf(source);
} }
}); };
_parsers.put(Byte.class, _parsers.get(Byte.TYPE)); _parsers.put(Byte.class, p);
_parsers.put(Byte.TYPE, p);
// and shorts // and shorts
_parsers.put(Short.TYPE, new Parser() { p = new Parser() {
public Object parse (String source) throws Exception { public Object parse (String source) throws Exception {
return Short.valueOf(source); return Short.valueOf(source);
} }
}); };
_parsers.put(Short.class, _parsers.get(Short.TYPE)); _parsers.put(Short.class, p);
_parsers.put(Short.TYPE, p);
// and ints // and ints
_parsers.put(Integer.TYPE, new Parser() { p = new Parser() {
public Object parse (String source) throws Exception { public Object parse (String source) throws Exception {
return Integer.valueOf(source); return Integer.valueOf(source);
} }
}); };
_parsers.put(Integer.class, _parsers.get(Integer.TYPE)); _parsers.put(Integer.class, p);
_parsers.put(Integer.TYPE, p);
// and longs // and longs
_parsers.put(Long.TYPE, new Parser() { p = new Parser() {
public Object parse (String source) throws Exception { public Object parse (String source) throws Exception {
return Long.valueOf(source); return Long.valueOf(source);
} }
}); };
_parsers.put(Long.class, _parsers.get(Long.TYPE)); _parsers.put(Long.class, p);
_parsers.put(Long.TYPE, p);
// and floats // and floats
_parsers.put(Float.TYPE, new Parser() { p = new Parser() {
public Object parse (String source) throws Exception { public Object parse (String source) throws Exception {
return Float.valueOf(source); return Float.valueOf(source);
} }
}); };
_parsers.put(Float.class, _parsers.get(Float.TYPE)); _parsers.put(Float.class, p);
_parsers.put(Float.TYPE, p);
// and booleans // and booleans
_parsers.put(Boolean.TYPE, new Parser() { p = new Parser() {
public Object parse (String source) throws Exception { public Object parse (String source) throws Exception {
return Boolean.valueOf(source); return Boolean.valueOf(source);
} }
}); };
_parsers.put(Boolean.class, _parsers.get(Boolean.TYPE)); _parsers.put(Boolean.class, p);
_parsers.put(Boolean.TYPE, p);
// and byte arrays // and byte arrays
_parsers.put(byte[].class, new Parser() { _parsers.put(byte[].class, new Parser() {
public Object parse (String source) throws Exception { public Object parse (String source) throws Exception {
int[] values = StringUtil.parseIntArray(source); String[] strs = StringUtil.parseStringArray(source);
int vcount = values.length; int count = strs.length;
byte[] bytes = new byte[vcount]; byte[] bytes = new byte[count];
for (int ii = 0; ii < vcount; ii++) { for (int ii = 0; ii < count; ii++) {
bytes[ii] = (byte)values[ii]; bytes[ii] = Byte.valueOf(strs[ii]);
} }
return bytes; return bytes;
} }