How can I identify int that have a negative value that have been converted from byte[]?
<p>I have a strange problem. Basically I'm using a queue server that takes input only as byte[] so I'm taking two ints and int[] and converting them using <code>ByteArrayOutputStream</code>.  It has worked fine so far but as I'm transferring a lot of messages back and forth from the queue I am trying to compress my int[](it has several thousand items but the majority are zeros).  I got the idea to take the sequence of zeros and turn them into negative values(see the answer to this <a href="http://stackoverflow.com/questions/10369660/suggestions-for-compression-library-to-get-byte-as-small-as-possible-without-c">question</a>. </p>

<p>But I'm having problems because to convert my bytes[] back into the original format I used to take the length of the byte[] and divide it by 4(since each int was 4 in size and then loop through it).  Since I have introduced negative values into the list, the size has changed(by 1 per negative), which is throwing off my ability to unpack the data.  I have tried different ways to get data into Byte[] and ByteArrayOutputStream seems to be the fastest I have tried so far so, unless there is something faster, I prefer to stick with this method. Also in my linked question, the accepted answer has a method that seems to fit perfectly within the existing for loop structure I'm already using to the covert the data(the solution to replace all zero's with negative number of the sequence of zeros).</p>

<p>Any ideas how I can differentiate the stream of positive/negative bytes?</p>

<p>Here's the code:</p>

<pre><code>import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


public class compressionTest {

    public static void main(String[] args) throws IOException {
        //to convert to string
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        //data
        int data1 = 10;
        int data2 = 43;
        int[] sub = { 10, 40, 0, 0, 0, 30, 0, 100, 0, 0, 0, 0 }; //should become [10, 40, -3, 30, -1, 100, -4]
        //add data to bytes
        dos.writeInt(data1);
        dos.writeInt(data2);
        int count_zero = 0;
        for (int j : sub) {
            if (j == 0 ) {
                //System.out.println("Equals 0!");
                count_zero = count_zero + 1;
            } else {
                if ( count_zero != 0) {
                    dos.write(-1 * count_zero);
                    //System.out.println(-1 * count_zero);
                    count_zero = 0;

                }
                dos.writeInt(j); //orginally I just had this under the for loop and it works(if you delete the if data above)
            }
        }
        byte[] bytes = baos.toByteArray();

        System.out.println(bytes); //this is the data I send

        //now bring it back
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        DataInputStream dis = new DataInputStream(bais);
        System.out.println("****");
        int data1_return = 0;
        int data2_return = 0;
        System.out.println("size of byte[] is " + bytes.length);
        //ArrayList&lt;Integer&gt; sub_return = new ArrayList&lt;Integer&gt;();
        int[] sub_return = new int[(bytes.length/4)-2]; //size of data minus first two intgers
        for (int item = 0; item&lt;(bytes.length/4);item++){
            if (item == 0) {
                data1_return = dis.readInt();
            } else if (item == 1) {
                data2_return = dis.readInt();
            } else {
                sub_return[item-2] =  dis.readInt();
            }
        }

        //print out the data
        System.out.println(data1_return);
        System.out.println(data2_return);
        for (int i : sub_return) {
            System.out.println(i);
        }

    }

}
</code></pre>
