public ByteBuffer serialize () {

        final int size = uncroppedSize();
        if (size == 0) return ByteBuffer.allocate(0);// empty data segment

        // calculate sense length
        int senseLength;
        if (senseData == null)
            senseLength = 0;
        else
            senseLength = senseData.size();

        // allocate buffer of appropriate size
        final ByteBuffer buffer = ByteBuffer.allocate(size);

        // write sense length field
        ReadWrite.writeTwoByteInt(buffer,// buffer
                senseLength,// value
                0);// index

        // write sense data
        if (senseData != null) senseData.serialize(buffer, SENSE_LENGTH_FIELD_LENGTH);

        // write and SCSI response data
        if (responseData != null) responseData.serialize(buffer, SENSE_LENGTH_FIELD_LENGTH + senseLength);

        if (allocationLength > 0 && buffer.capacity() > allocationLength) {
            /*
             * If the allocation length variable is valid and the response data segment is larger than the data-in
             * buffer allocated by the initiator, limit the number of bytes returned.
             */
            buffer.position(0);
            buffer.limit(allocationLength);
            final ByteBuffer croppedBuffer = ByteBuffer.allocate(allocationLength);
            croppedBuffer.put(buffer);
            return croppedBuffer;
        }

        if (LOGGER.isDebugEnabled()) LOGGER.debug("SCSI Response Data Segment:\n" + Debug.byteBufferToString(buffer));

        return buffer;
    }
public ByteBuffer serialize ( ) { final int size = uncroppedSize ( ) ; if ( size == 0 ) return ByteBuffer . allocate ( 0 ) ; int senseLength ; if ( senseData == null ) senseLength = 0 ; else senseLength = senseData . size ( ) ; final ByteBuffer buffer = ByteBuffer . allocate ( size ) ; ReadWrite . writeTwoByteInt ( buffer , senseLength , 0 ) ; if ( senseData != null ) senseData . serialize ( buffer , SENSE_LENGTH_FIELD_LENGTH ) ; if ( responseData != null ) responseData . serialize ( buffer , SENSE_LENGTH_FIELD_LENGTH + senseLength ) ; if ( allocationLength > 0 && buffer . capacity ( ) > allocationLength ) { buffer . position ( 0 ) ; buffer . limit ( allocationLength ) ; final ByteBuffer croppedBuffer = ByteBuffer . allocate ( allocationLength ) ; croppedBuffer . put ( buffer ) ; return croppedBuffer ; } if ( LOGGER . isDebugEnabled ( ) ) LOGGER . debug ( "SCSI Response Data Segment:\n" + Debug . byteBufferToString ( buffer ) ) ; return buffer ; }
Returns a {@link ByteBuffer} containing a serialized representation of this object.

@return a {@link ByteBuffer} containing a serialized representation of this object