use of java.io.InvalidObjectException in project j2objc by google.
the class HashMap method readObject.
/**
* Reconstitute the {@code HashMap} instance from a stream (i.e.,
* deserialize it).
*/
private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
// Read in the threshold (ignored), loadfactor, and any hidden stuff
s.defaultReadObject();
reinitialize();
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new InvalidObjectException("Illegal load factor: " + loadFactor);
// Read and ignore number of buckets
s.readInt();
// Read number of mappings (size)
int mappings = s.readInt();
if (mappings < 0)
throw new InvalidObjectException("Illegal mappings count: " + mappings);
else if (mappings > 0) {
// (if zero, use defaults)
// Size the table using given load factor only if within
// range of 0.25...4.0
float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
float fc = (float) mappings / lf + 1.0f;
int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ? DEFAULT_INITIAL_CAPACITY : (fc >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : tableSizeFor((int) fc));
float ft = (float) cap * lf;
threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ? (int) ft : Integer.MAX_VALUE);
@SuppressWarnings({ "rawtypes", "unchecked" }) Node<K, V>[] tab = (Node<K, V>[]) new Node[cap];
table = tab;
// Read the keys and values, and put the mappings in the HashMap
for (int i = 0; i < mappings; i++) {
@SuppressWarnings("unchecked") K key = (K) s.readObject();
@SuppressWarnings("unchecked") V value = (V) s.readObject();
putVal(hash(key), key, value, false, false);
}
}
}
use of java.io.InvalidObjectException in project j2objc by google.
the class URI method readObject.
/**
* Reconstitutes a URI from the given serial stream.
*
* <p> The {@link java.io.ObjectInputStream#defaultReadObject()} method is
* invoked to read the value of the {@code string} field. The result is
* then parsed in the usual way.
*
* @param is The object-input stream from which this object
* is being read
*/
private void readObject(ObjectInputStream is) throws ClassNotFoundException, IOException {
// Argh
port = -1;
is.defaultReadObject();
try {
new Parser(string).parse(false);
} catch (URISyntaxException x) {
IOException y = new InvalidObjectException("Invalid URI");
y.initCause(x);
throw y;
}
}
use of java.io.InvalidObjectException in project j2objc by google.
the class TCKValueRangeSerialization method test_invalid_serialform.
@Test
public void test_invalid_serialform() throws Exception {
byte[] template = { (byte) 172, (byte) 237, 0, 5, 115, 114, 0, 29, 106, 97, /* \u00ac \u00ed \u0000 \u0005 s r \u0000 \u001d j a */
118, 97, 46, 116, 105, 109, 101, 46, 116, 101, /* v a . t i m e . t e */
109, 112, 111, 114, 97, 108, 46, 86, 97, 108, /* m p o r a l . V a l */
117, 101, 82, 97, 110, 103, 101, (byte) 154, 113, (byte) 169, /* u e R a n g e \u009a q \u00a9 */
86, (byte) 242, (byte) 205, 90, (byte) 184, 2, 0, 4, 74, 0, /* V \u00f2 \u00cd Z \u00b8 \u0002 \u0000 \u0004 J \u0000 */
10, 109, 97, 120, 76, 97, 114, 103, 101, 115, /* m a x L a r g e s */
116, 74, 0, 11, 109, 97, 120, 83, 109, 97, /* t J \u0000 \u000b m a x S m a */
108, 108, 101, 115, 116, 74, 0, 10, 109, 105, /* l l e s t J \u0000 m i */
110, 76, 97, 114, 103, 101, 115, 116, 74, 0, /* n L a r g e s t J \u0000 */
11, 109, 105, 110, 83, 109, 97, 108, 108, 101, /* \u000b m i n S m a l l e */
115, 116, 120, 112, 0, 0, 0, 0, 0, 0, /* s t x p \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 */
0, 40, 0, 0, 0, 0, 0, 0, 0, 30, /* \u0000 ( \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 \u001e */
0, 0, 0, 0, 0, 0, 0, 20, 0, 0, /* \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 \u0000 \u0014 \u0000 \u0000 */
0, 0, 0, 0, 0, 10 /* \u0000 \u0000 \u0000 \u0000 \u0000 */
};
// minSmallest > minLargest, insert invalid values and deserialize
byte[] bad1 = { 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 4 };
byte[] val = Arrays.copyOf(template, template.length);
System.arraycopy(bad1, 0, val, 114, bad1.length);
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(val))) {
in.readObject();
fail("Invalid minSmallest > minLargest " + ValueRange.class.getName());
} catch (InvalidObjectException ioe) {
// Expected exception
}
// maxSmallest > maxLargest, insert invalid values and deserialize
byte[] bad2 = { 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 3 };
val = Arrays.copyOf(template, template.length);
System.arraycopy(bad1, 0, val, 114, bad2.length);
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(val))) {
in.readObject();
fail("Invalid maxSmallest > maxLargest " + ValueRange.class.getName());
} catch (InvalidObjectException ioe) {
// Expected exception
}
// minLagest > maxLargest, insert invalid values and deserialize
byte[] bad3 = { 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 3, 0, 0, 0, 4 };
val = Arrays.copyOf(template, template.length);
System.arraycopy(bad1, 0, val, 114, bad3.length);
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(val))) {
in.readObject();
fail("Invalid minLagest > maxLargest " + ValueRange.class.getName());
} catch (InvalidObjectException ioe) {
// Expected exception
}
}
use of java.io.InvalidObjectException in project guava by google.
the class ImmutableSetMultimap method readObject.
// java.io.ObjectInputStream
@GwtIncompatible
// Serialization type safety is at the caller's mercy.
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
Comparator<Object> valueComparator = (Comparator<Object>) stream.readObject();
int keyCount = stream.readInt();
if (keyCount < 0) {
throw new InvalidObjectException("Invalid key count " + keyCount);
}
ImmutableMap.Builder<Object, ImmutableSet<Object>> builder = ImmutableMap.builder();
int tmpSize = 0;
for (int i = 0; i < keyCount; i++) {
Object key = stream.readObject();
int valueCount = stream.readInt();
if (valueCount <= 0) {
throw new InvalidObjectException("Invalid value count " + valueCount);
}
ImmutableSet.Builder<Object> valuesBuilder = valuesBuilder(valueComparator);
for (int j = 0; j < valueCount; j++) {
valuesBuilder.add(stream.readObject());
}
ImmutableSet<Object> valueSet = valuesBuilder.build();
if (valueSet.size() != valueCount) {
throw new InvalidObjectException("Duplicate key-value pairs exist for key " + key);
}
builder.put(key, valueSet);
tmpSize += valueCount;
}
ImmutableMap<Object, ImmutableSet<Object>> tmpMap;
try {
tmpMap = builder.buildOrThrow();
} catch (IllegalArgumentException e) {
throw (InvalidObjectException) new InvalidObjectException(e.getMessage()).initCause(e);
}
FieldSettersHolder.MAP_FIELD_SETTER.set(this, tmpMap);
FieldSettersHolder.SIZE_FIELD_SETTER.set(this, tmpSize);
SetFieldSettersHolder.EMPTY_SET_FIELD_SETTER.set(this, emptySet(valueComparator));
}
use of java.io.InvalidObjectException in project guava by google.
the class ImmutableListMultimap method readObject.
// java.io.ObjectInputStream
@GwtIncompatible
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
int keyCount = stream.readInt();
if (keyCount < 0) {
throw new InvalidObjectException("Invalid key count " + keyCount);
}
ImmutableMap.Builder<Object, ImmutableList<Object>> builder = ImmutableMap.builder();
int tmpSize = 0;
for (int i = 0; i < keyCount; i++) {
Object key = stream.readObject();
int valueCount = stream.readInt();
if (valueCount <= 0) {
throw new InvalidObjectException("Invalid value count " + valueCount);
}
ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder();
for (int j = 0; j < valueCount; j++) {
valuesBuilder.add(stream.readObject());
}
builder.put(key, valuesBuilder.build());
tmpSize += valueCount;
}
ImmutableMap<Object, ImmutableList<Object>> tmpMap;
try {
tmpMap = builder.buildOrThrow();
} catch (IllegalArgumentException e) {
throw (InvalidObjectException) new InvalidObjectException(e.getMessage()).initCause(e);
}
FieldSettersHolder.MAP_FIELD_SETTER.set(this, tmpMap);
FieldSettersHolder.SIZE_FIELD_SETTER.set(this, tmpSize);
}
Aggregations