Encode() public method

Encode this histogram into a ByteBuffer
public Encode ( ByteBuffer targetBuffer, IEncoder encoder ) : int
targetBuffer ByteBuffer The buffer to encode into
encoder IEncoder The encoder to use
return int
コード例 #1
0
        private static HistogramBase EncodeDecode(HistogramBase source)
        {
            var targetBuffer = ByteBuffer.Allocate(source.GetNeededByteBufferCapacity());

            source.Encode(targetBuffer, EncoderV2);
            targetBuffer.Position = 0;
            return(HistogramEncoding.DecodeFromByteBuffer(targetBuffer, 0));
        }
コード例 #2
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(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);
        }