Exemplo n.º 1
0
        /// <summary>
        /// Encode this histogram in compressed form into a <see cref="ByteBuffer"/>.
        /// </summary>
        /// <param name="source">The histogram to encode</param>
        /// <param name="targetBuffer">The buffer to write to</param>
        /// <returns>The number of bytes written to the buffer</returns>
        public static int EncodeIntoCompressedByteBuffer(this HistogramBase source, ByteBuffer targetBuffer)
        {
            int neededCapacity = source.GetNeededByteBufferCapacity();
            var intermediateUncompressedByteBuffer = ByteBuffer.Allocate(neededCapacity);

            source.Encode(intermediateUncompressedByteBuffer, HistogramEncoderV2.Instance);

            int initialTargetPosition = targetBuffer.Position;

            targetBuffer.PutInt(source.GetCompressedEncodingCookie());
            int compressedContentsHeaderPosition = targetBuffer.Position;

            targetBuffer.PutInt(0); // Placeholder for compressed contents length
            var compressedDataLength = targetBuffer.CompressedCopy(intermediateUncompressedByteBuffer, targetBuffer.Position);

            targetBuffer.PutInt(compressedContentsHeaderPosition, compressedDataLength); // Record the compressed length
            int bytesWritten = compressedDataLength + 8;

            targetBuffer.Position = (initialTargetPosition + bytesWritten);
            return(bytesWritten);
        }