/** * Same as {@link #marshallArray(Object[], ObjectOutput)} but specialized for byte arrays. * * @see #marshallArray(Object[], ObjectOutput). */ public static void marshallByteArray(byte[] array, ObjectOutput out) throws IOException { final int size = array == null ? NULL_VALUE : array.length; marshallSize(out, size); if (size <= 0) { return; } out.write(array); }
/** * Marshall arrays. * <p> * This method supports {@code null} {@code array}. * * @param array Array to marshall. * @param out {@link ObjectOutput} to write. * @param <E> Array type. * @throws IOException If any of the usual Input/Output related exceptions occur. */ public static <E> void marshallArray(E[] array, ObjectOutput out) throws IOException { final int size = array == null ? NULL_VALUE : array.length; marshallSize(out, size); if (size <= 0) { return; } for (int i = 0; i < size; ++i) { out.writeObject(array[i]); } }
/** * Marshalls a collection of integers. * * @param collection the collection to marshall. * @param out the {@link ObjectOutput} to write to. * @throws IOException if an error occurs. */ public static void marshallIntCollection(Collection<Integer> collection, ObjectOutput out) throws IOException { final int size = collection == null ? NULL_VALUE : collection.size(); marshallSize(out, size); if (size <= 0) { return; } for (Integer integer : collection) { out.writeInt(integer); } }
/** * Marshall the {@code map} to the {@code ObjectOutput}. * <p> * {@code null} maps are supported. * * @param map {@link Map} to marshall. * @param out {@link ObjectOutput} to write. It must be non-null. * @param <K> Key type of the map. * @param <V> Value type of the map. * @param <T> Type of the {@link Map}. * @throws IOException If any of the usual Input/Output related exceptions occur. */ public static <K, V, T extends Map<K, V>> void marshallMap(T map, ObjectOutput out) throws IOException { final int mapSize = map == null ? NULL_VALUE : map.size(); marshallSize(out, mapSize); if (mapSize <= 0) return; for (Map.Entry<K, V> me : map.entrySet()) { out.writeObject(me.getKey()); out.writeObject(me.getValue()); } }
/** * Marshall a {@link Collection}. * <p> * This method supports {@code null} {@code collection}. * * @param collection {@link Collection} to marshal. * @param out {@link ObjectOutput} to write. * @param writer {@link ElementWriter} that writes single element to the output. * @param <E> Collection's element type. * @throws IOException If any of the usual Input/Output related exceptions occur. */ public static <E> void marshallCollection(Collection<E> collection, ObjectOutput out, ElementWriter<E> writer) throws IOException { final int size = collection == null ? NULL_VALUE : collection.size(); marshallSize(out, size); if (size <= 0) { return; } for (E e : collection) { writer.writeTo(out, e); } }
/** * Marshall the {@code map} to the {@code ObjectOutput}. * <p> * {@code null} maps are supported. * * @param map {@link Map} to marshall. * @param out {@link ObjectOutput} to write. It must be non-null. * @param <K> Key type of the map. * @param <V> Value type of the map. * @param <T> Type of the {@link Map}. * @throws IOException If any of the usual Input/Output related exceptions occur. */ public static <K, V, T extends Map<K, V>> void marshallMap(T map, ElementWriter<K> keyWriter, ElementWriter<V> valueWrite, ObjectOutput out) throws IOException { final int mapSize = map == null ? NULL_VALUE : map.size(); marshallSize(out, mapSize); if (mapSize <= 0) return; for (Map.Entry<K, V> me : map.entrySet()) { keyWriter.writeTo(out, me.getKey()); valueWrite.writeTo(out, me.getValue()); } }
@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)); } }