java/proto/src/main/java/com/pravala/protocol/SerializableBase.java
public int deserializeWithLength ( InputStream fromStream ) throws IOException, CodecException
    {
        byte[] buf = null;
        int dataSize = 0;
        int bufSize = 0;

        while ( true )
        {
            // deserialize() function returns a positive value when it has all the bytes
            // needed to deserialize the function. The returned value is the number of consumed
            // bytes.
            // When the value is negative, it means that there was not enough data in the buffer.
            // The number of (at least) missing bytes is the absolute value of the (negative) number
            // returned. Here we focus on the case where there are some bytes missing,
            // so we use 'missingBytes' variable to represent this.
            // However, at first this variable actually stores both the number of consumed bytes
            // (if it's positive), and the number of missing bytes (if it's negative).
            int missingBytes = deserializeWithLength ( buf, 0, dataSize );

            // If missingBytes > 0, then it means that the value stored in missingBytes
            // is actually the number of 'consumed bytes', and that we now have a complete message.
            // Let's report the number of bytes we just consumed!
            if ( missingBytes > 0 )
            {
                return missingBytes;
            }

            // missingBytes is <= 0, which means there were not enough bytes to deserialize the message
            // Let's make the 'missingBytes' to store the number of bytes missing as a positive value.
            missingBytes *= -1;

            // From this point, missingBytes is positive, and stores the number of bytes that
            // are still missing (at least, this number could change in the future - this is because
            // we may not have enough bytes to decode even the message length, so the deserialize
            // could report the number of missing bytes it knows for sure it's going to need,
            // and, in the future, once it has the entire header, it will report the actual number
            // of missing bytes, that will most likely be larger).

            // deserialize above should not return 0!
            assert missingBytes > 0;

            if ( dataSize + missingBytes > bufSize )
            {
                bufSize = dataSize + missingBytes;

                // At the beginning we want to allocate a little more space,
                // just so we could avoid reallocating it after every byte of the message header!
                if ( bufSize < 16 )
                {
                    bufSize = 16;
                }

                byte[] oldBuf = buf;
                buf = new byte[ bufSize ];

                if ( dataSize > 0 )
                {
                    System.arraycopy ( oldBuf, 0, buf, 0, dataSize );
                }
            }

            while ( missingBytes > 0 )
            {
                int r = fromStream.read ( buf, dataSize, missingBytes );

                if ( r < 0 )
                {
                    return -1;
                }

                dataSize += r;
                missingBytes -= r;
            }
        }
    }
/**
     * It reads a single message from the input stream
     *
     * @param fromStream The stream to read the message from
     * @return The number of bytes consumed, or -1 if the end of the stream was reached before
     *  the entire message could be read.
     * @throws IOException, CodecException
     */
It reads a single message from the input stream
@param fromStream The stream to read the message from
@return The number of bytes consumed, or -1 if the end of the stream was reached before
the entire message could be read.
It reads a single message from the input stream @param fromStream The stream to read the message from @return The number of bytes consumed or - 1 if the end of the stream was reached before the entire message could be read .
public int deserializeWithLength ( InputStream fromStream ) throws IOException, CodecException
    {
        byte[] buf = null;
        int dataSize = 0;
        int bufSize = 0;
        while ( true )
        {
            int missingBytes = deserializeWithLength ( buf, 0, dataSize );
            if ( missingBytes > 0 )
            {
                return missingBytes;
            }
            missingBytes *= -1;
            assert missingBytes > 0;
            if ( dataSize + missingBytes > bufSize )
            {
                bufSize = dataSize + missingBytes;
                if ( bufSize < 16 )
                {
                    bufSize = 16;
                }
                byte[] oldBuf = buf;
                buf = new byte[ bufSize ];
                if ( dataSize > 0 )
                {
                    System.arraycopy ( oldBuf, 0, buf, 0, dataSize );
                }
            }
            while ( missingBytes > 0 )
            {
                int r = fromStream.read ( buf, dataSize, missingBytes );
                if ( r < 0 )
                {
                    return -1;
                }
                dataSize += r;
                missingBytes -= r;
            }
        }
    }
public int deserializeWithLength ( InputStream fromStream ) throws IOException , CodecException { byte [ ] buf = null ; int dataSize = 0 ; int bufSize = 0 ; while ( true ) { int missingBytes = deserializeWithLength ( buf , 0 , dataSize ) ; if ( missingBytes > 0 ) { return missingBytes ; } missingBytes *= - 1 ; assert missingBytes > 0 ; if ( dataSize + missingBytes > bufSize ) { bufSize = dataSize + missingBytes ; if ( bufSize < 16 ) { bufSize = 16 ; } byte [ ] oldBuf = buf ; buf = new byte [ bufSize ] ; if ( dataSize > 0 ) { System . arraycopy ( oldBuf , 0 , buf , 0 , dataSize ) ; } } while ( missingBytes > 0 ) { int r = fromStream . read ( buf , dataSize , missingBytes ) ; if ( r < 0 ) { return - 1 ; } dataSize += r ; missingBytes -= r ; } } }
It reads a single message from the input stream
 @param fromStream The stream to read the message from
 @return The number of bytes consumed, or -1 if the end of the stream was reached before
  the entire message could be read.
It reads a single message from the input stream @param fromStream The stream to read the message from @return The number of bytes consumed or - 1 if the end of the stream was reached before the entire message could be read .
// deserialize() function returns a positive value when it has all the bytes // needed to deserialize the function. The returned value is the number of consumed // bytes. // When the value is negative, it means that there was not enough data in the buffer. // The number of (at least) missing bytes is the absolute value of the (negative) number // returned. Here we focus on the case where there are some bytes missing, // so we use 'missingBytes' variable to represent this. // However, at first this variable actually stores both the number of consumed bytes // (if it's positive), and the number of missing bytes (if it's negative). // If missingBytes > 0, then it means that the value stored in missingBytes // is actually the number of 'consumed bytes', and that we now have a complete message. // Let's report the number of bytes we just consumed! // missingBytes is <= 0, which means there were not enough bytes to deserialize the message // Let's make the 'missingBytes' to store the number of bytes missing as a positive value. // From this point, missingBytes is positive, and stores the number of bytes that // are still missing (at least, this number could change in the future - this is because // we may not have enough bytes to decode even the message length, so the deserialize // could report the number of missing bytes it knows for sure it's going to need, // and, in the future, once it has the entire header, it will report the actual number // of missing bytes, that will most likely be larger). // deserialize above should not return 0! // At the beginning we want to allocate a little more space, // just so we could avoid reallocating it after every byte of the message header!