/** * Same as {@link #unmarshallArray(ObjectInput, ArrayBuilder)} but specialized for byte array. * <p> * No {@link ArrayBuilder} is necessary. * * @see #unmarshallArray(ObjectInput, ArrayBuilder). */ public static byte[] unmarshallByteArray(ObjectInput in) throws IOException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } else if (size == 0) { return Util.EMPTY_BYTE_ARRAY; } byte[] array = new byte[size]; in.readFully(array); return array; }
/** * Same as {@link #unmarshallCollection(ObjectInput, CollectionBuilder)}. * <p> * Used when the size of the {@link Collection} is not needed for it construction. * * @see #unmarshallCollection(ObjectInput, CollectionBuilder). */ public static <E, T extends Collection<E>> T unmarshallCollectionUnbounded(ObjectInput in, UnboundedCollectionBuilder<E, T> builder) throws IOException, ClassNotFoundException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } T collection = Objects.requireNonNull(builder, "UnboundedCollectionBuilder must be non-null").build(); for (int i = 0; i < size; ++i) { //noinspection unchecked collection.add((E) in.readObject()); } return collection; }
/** * Unmarshall arrays. * * @param in {@link ObjectInput} to read. * @param builder {@link ArrayBuilder} to build the array. * @param <E> Array type. * @return The populated array. * @throws IOException If any of the usual Input/Output related exceptions occur. * @throws ClassNotFoundException If the class of a serialized object cannot be found. * @see #marshallArray(Object[], ObjectOutput). */ public static <E> E[] unmarshallArray(ObjectInput in, ArrayBuilder<E> builder) throws IOException, ClassNotFoundException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } final E[] array = Objects.requireNonNull(builder, "ArrayBuilder must be non-null").build(size); for (int i = 0; i < size; ++i) { //noinspection unchecked array[i] = (E) in.readObject(); } return array; }
/** * Unmarshalls a collection of integers. * * @param in the {@link ObjectInput} to read from. * @param builder the {@link CollectionBuilder} to build the collection of integer. * @param <T> the concrete type of the collection. * @return the collection. * @throws IOException if an error occurs. */ public static <T extends Collection<Integer>> T unmarshallIntCollection(ObjectInput in, CollectionBuilder<Integer, T> builder) throws IOException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } T collection = Objects.requireNonNull(builder, "CollectionBuilder must be non-null").build(size); for (int i = 0; i < size; ++i) { collection.add(in.readInt()); } return collection; }
/** * Unmarshall the {@link Map}. * <p> * If the marshalled map is {@link null}, then the {@link MapBuilder} is not invoked. * * @param in {@link ObjectInput} to read. * @param builder {@link MapBuilder} to create the concrete {@link Map} implementation. * @return The populated {@link Map} created by the {@link MapBuilder} or {@code null}. * @throws IOException If any of the usual Input/Output related exceptions occur. * @throws ClassNotFoundException If the class of a serialized object cannot be found. * @see #marshallMap(Map, ObjectOutput) */ public static <K, V, T extends Map<K, V>> T unmarshallMap(ObjectInput in, MapBuilder<K, V, T> builder) throws IOException, ClassNotFoundException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } final T map = Objects.requireNonNull(builder, "MapBuilder must be non-null").build(computeCapacity(size)); for (int i = 0; i < size; i++) //noinspection unchecked map.put((K) in.readObject(), (V) in.readObject()); return map; }
/** * Unmarshal a {@link Collection}. * * @param in {@link ObjectInput} to read. * @param builder {@link CollectionBuilder} builds the concrete {@link Collection} based on size. * @param reader {@link ElementReader} reads one element from the input. * @param <E> Collection's element type. * @param <T> {@link Collection} implementation. * @return The concrete {@link Collection} implementation. * @throws IOException If any of the usual Input/Output related exceptions occur. * @throws ClassNotFoundException If the class of a serialized object cannot be found. */ public static <E, T extends Collection<E>> T unmarshallCollection(ObjectInput in, CollectionBuilder<E, T> builder, ElementReader<E> reader) throws IOException, ClassNotFoundException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } T collection = Objects.requireNonNull(builder, "CollectionBuilder must be non-null").build(size); for (int i = 0; i < size; ++i) { //noinspection unchecked collection.add(reader.readFrom(in)); } return collection; }
/** * Unmarshall the {@link Map}. * <p> * If the marshalled map is {@link null}, then the {@link MapBuilder} is not invoked. * * @param in {@link ObjectInput} to read. * @param builder {@link MapBuilder} to create the concrete {@link Map} implementation. * @return The populated {@link Map} created by the {@link MapBuilder} or {@code null}. * @throws IOException If any of the usual Input/Output related exceptions occur. * @throws ClassNotFoundException If the class of a serialized object cannot be found. * @see #marshallMap(Map, ElementWriter, ElementWriter, ObjectOutput) */ public static <K, V, T extends Map<K, V>> T unmarshallMap(ObjectInput in, ElementReader<K> keyReader, ElementReader<V> valueReader, MapBuilder<K, V, T> builder) throws IOException, ClassNotFoundException { final int size = unmarshallSize(in); if (size == NULL_VALUE) { return null; } final T map = Objects.requireNonNull(builder, "MapBuilder must be non-null").build(computeCapacity(size)); for (int i = 0; i < size; i++) //noinspection unchecked map.put(keyReader.readFrom(in), valueReader.readFrom(in)); return map; }
@Test public void testRandomPositiveInt() throws IOException { Random random = new Random(System.nanoTime()); ObjectInputOutput io = new ObjectInputOutput(); for (int i = 0; i < NR_RANDOM; ++i) { int v = random.nextInt(); if (v < 0) { v = -v; } io.reset(); MarshallUtil.marshallSize(io, v); Assert.assertEquals("Error for v=" + v, v, MarshallUtil.unmarshallSize(io)); } }
private static void checkNegativeInt(int i, ObjectInputOutput io) throws IOException { io.reset(); MarshallUtil.marshallSize(io, i); Assert.assertEquals("Error for i=" + i, 1, io.buffer.size()); Assert.assertEquals("Error for i=" + i, -1, MarshallUtil.unmarshallSize(io)); Assert.assertEquals("Error for i=" + i, 0, io.buffer.size()); }
private static void checkIntAndByteArray(int i, int bytesExpected, ObjectInputOutput io) throws IOException { io.reset(); MarshallUtil.marshallSize(io, i); Assert.assertEquals("Error for i=" + i, bytesExpected, io.buffer.size()); Assert.assertEquals("Error for i=" + i, i, MarshallUtil.unmarshallSize(io)); Assert.assertEquals("Error for i=" + i, 0, io.buffer.size()); }
@Test public void testRandomNegativeInt() throws IOException { Random random = new Random(System.nanoTime()); ObjectInputOutput io = new ObjectInputOutput(); for (int i = 0; i < NR_RANDOM; ++i) { int v = random.nextInt(); if (v > 0) { v = -v; } else if (v == 0) { i--; continue; } io.reset(); MarshallUtil.marshallSize(io, v); Assert.assertEquals("Error for v=" + v, -1, MarshallUtil.unmarshallSize(io)); } }