mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 06:39:54 +01:00
Promote integer types to the expected type to allow, for example, a
list of mixed integer types to be read as a list of longs.
This commit is contained in:
@@ -407,7 +407,26 @@ class ReaderImpl implements Reader {
|
|||||||
|
|
||||||
private <T> T readObject(Class<T> t) throws IOException {
|
private <T> T readObject(Class<T> t) throws IOException {
|
||||||
try {
|
try {
|
||||||
return t.cast(readObject());
|
Object o = readObject();
|
||||||
|
// If this is a small integer type and we're expecting a larger
|
||||||
|
// integer type, promote before casting
|
||||||
|
if(o instanceof Byte) {
|
||||||
|
if(Short.class.isAssignableFrom(t))
|
||||||
|
return t.cast(Short.valueOf((Byte) o));
|
||||||
|
if(Integer.class.isAssignableFrom(t))
|
||||||
|
return t.cast(Integer.valueOf((Byte) o));
|
||||||
|
if(Long.class.isAssignableFrom(t))
|
||||||
|
return t.cast(Long.valueOf((Byte) o));
|
||||||
|
} else if(o instanceof Short) {
|
||||||
|
if(Integer.class.isAssignableFrom(t))
|
||||||
|
return t.cast(Integer.valueOf((Short) o));
|
||||||
|
if(Long.class.isAssignableFrom(t))
|
||||||
|
return t.cast(Long.valueOf((Short) o));
|
||||||
|
} else if(o instanceof Integer) {
|
||||||
|
if(Long.class.isAssignableFrom(t))
|
||||||
|
return t.cast(Long.valueOf((Integer) o));
|
||||||
|
}
|
||||||
|
return t.cast(o);
|
||||||
} catch(ClassCastException e) {
|
} catch(ClassCastException e) {
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user