title: Send int on socket as byte[] then recast to int not works
body: <p>I'm trying to serialize Object between NIO SocketChannel and blocking IO Socket. Since I can't use Serializable/writeObject on NIO, I thought to write code to serialize object into an ByteArrayOutputStream then send array length followed by array. </p>
  
  <p>Sender function is</p>
  
  <pre><code>public void writeObject(Object obj) throws IOException{
      ByteArrayOutputStream serializedObj = new ByteArrayOutputStream();
      ObjectOutputStream writer = new ObjectOutputStream(serializedObj);
      writer.writeUnshared(obj);
      ByteBuffer size =        ByteBuffer.allocate(4).putInt(serializedObj.toByteArray().length);
  
      this.getSocket().write(size);
      this.getSocket().write(ByteBuffer.wrap(serializedObj.toByteArray()));
  }
  </code></pre>
  
  <p>and receiver is:</p>
  
  <pre><code>public Object readObject(){
      try {
          //Leggi dimensione totale pacchetto
          byte[] dimension = new byte[4];
  
          int byteRead = 0;
  
          while(byteRead &lt; 4) {
              byteRead += this.getInputStream().read(dimension, byteRead, 4 - byteRead);
          }
  
          int size = ByteBuffer.wrap(dimension).getInt(); /* (*) */
          System.out.println(size);
  
          byte[] object = new byte[size];
  
          while(size &gt; 0){
              size -= this.getInputStream().read(object);
          }
  
          InputStream in = new ByteArrayInputStream(object, 0, object.length);
          ObjectInputStream ois = new ObjectInputStream(in);
          Object res = ois.readUnshared();
          ois.close();
          return res;
      } catch (IOException | ClassNotFoundException e) {
          return null;
      }
  }
  </code></pre>
  
  <p>The problem is that size (*) is always equals to -1393754107 when serializedObj.toByteArray().length in my test is 316. 
  I don't understand why casting not works properly. </p>