package serialization.primitives;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import commons.utils.NumericUtils;
/*
 * This class serializes array of integers in the following format:
 *   1. length of array
 *   Each 4 integers joined into single block
 *   2.  for each block 
 *   2.1 mask of lengths for each integer in the block
 *   2.2 4 integers
 */
public class IntArraySerializer implements IIntArraySerializer {

    public void serialize(OutputStream stream, int[] array) throws IOException {
        stream.write(array.length);
        for (int i = 0; i < array.length; i += 4) {
            byte[] lenghtsMask = new byte[4];
            byte[][] blockBytes = new byte[4][];
            for (int j = 0; j < 4 && i + j < array.length; ++j) {
                byte[] buffer = NumericUtils.toBytes(array[i + j]);
                lenghtsMask[j] = (byte) buffer.length;
                blockBytes[j] = buffer;
            }

            stream.write(lenghtsMask);
            for (int j = 0; j < 4 && i + j < array.length; ++j)
                stream.write(blockBytes[j]);
        }
    }

    public int[] deserialize(InputStream stream) throws IOException {
        int count = stream.read();
        int[] array = new int[count];
        for (int i = 0; i < count; i += 4) {
            byte[] lengthsMask = new byte[4];
            stream.read(lengthsMask);
            for (int j = 0; j < 4 && i + j < count; ++j) {
                byte[] bytes = new byte[lengthsMask[j]];
                stream.read(bytes);
                array[i + j] = NumericUtils.fromBytes(bytes);
            }
        }
        return array;
    }
}


@@ -580,16 +580,37 @@
 m.write(
+NumericUtils.toBytes(
 array.le
@@ -614,16 +614,17 @@
 .length)
+)
 ;%0D%0A     
@@ -1180,23 +1180,48 @@
      %7D%0D%0A
+        stream.flush();%0D%0A
     %7D%0D%0A
-
 %0D%0A    pu
@@ -1297,34 +1297,138 @@
     
-int count = stream.read();
+byte%5B%5D intBuffer = new byte%5B4%5D;%0D%0A        stream.read(intBuffer);%0D%0A        int count = NumericUtils.fromBytes(intBuffer);%0D%0A        
 %0D%0A  

fix bug at IntArraySerializer