package anastore.proto;

import anastore.net.AsyncMessage;
import anastore.util.HexDump;
import anastore.util.TimeRange;

/**
 * A reply containing a block
 */
public class BlockRep extends AsyncMessage
{
    public final TimeRange range;
    public final byte[] data;

    /**
     * Construct a reply containing information about a block.  The
     * given TimeRange is allowed to change in the future without
     * affecting the correctness of the message stream, but the data
     * array <i>must not</i> change in the future.
     */
    public BlockRep(TimeRange range, byte[] data)
    {
        // If range is bounded, then it is effectively immutable, so
        // we can reuse and take advantage of back references in the
        // serialization stream.  Otherwise, we must proactively copy
        // it.
        if (range.hasUpperBound())
            this.range = range;
        else
            this.range = new TimeRange(range);
        this.data = data;
    }

    @Override
    public String toString()
    {
        return "BlockRep<range=" + range +
            ",data[" + data.length + "]=[" + HexDump.summarize(data, 5) + "]>";
    }
}
