Beispiel #1
0
        // CopyTo allocates a temporary buffer. As we're already pooling buffers,
        // we might as well provide a CopyTo that makes use of that instead of
        // using Stream.CopyTo (which allocates a buffer of its own, even if it
        // does so efficiently).
        public static void ReusableCopyTo(this Stream input, Stream destination, ReusableMemoryStream tmpBuffer)
        {
            tmpBuffer.SetLength(81920);
            var buffer = tmpBuffer.GetBuffer();
            int read;

            while ((read = input.Read(buffer, 0, buffer.Length)) != 0)
            {
                destination.Write(buffer, 0, read);
            }
        }
Beispiel #2
0
        private static void CheckCrc(Crc32 crcAlgo, int crc, ReusableMemoryStream stream, long crcStartPos, long crcLength = -1)
        {
            var length      = crcLength == -1 ? stream.Position - crcStartPos : crcLength;
            var computedCrc = (int)crcAlgo.ComputeForStream(stream, crcStartPos, length);

            if (computedCrc != crc)
            {
                throw new CrcException(
                          string.Format("Corrupt message: CRC32 does not match. Calculated {0} but got {1}", computedCrc,
                                        crc));
            }
        }
Beispiel #3
0
        public static uint Compute(ReusableMemoryStream stream, long start, long size)
        {
            var crc = DefaultSeed;

            var buffer = stream.GetBuffer();

            for (var i = start; i < start + size; ++i)
            {
                crc = (crc >> 8) ^ _defaultTable[buffer[i] ^ crc & 0xff];
            }

            return(~crc);
        }
Beispiel #4
0
        private uint ComputeForStream(ReusableMemoryStream stream, long start, long size)
        {
            var crc = Seed;

            var buffer = stream.GetBuffer();

            for (var i = start; i < start + size; ++i)
            {
                crc = (crc >> 8) ^ Table[buffer[i] ^ crc & 0xff];
            }

            return(~crc);
        }
Beispiel #5
0
 /// <summary>
 /// Compute the CRC-32 of the byte sequence using Castagnoli polynomial values.
 /// This alternate CRC-32 is often used to compute the CRC as it is often yield
 /// better chances to detect errors in larger payloads.
 ///
 /// In particular, it is used to compute the CRC of a RecordBatch in newer versions
 /// of the Kafka protocol.
 /// </summary>
 /// <param name="stream">byte stream</param>
 /// <param name="start">start offset of the byte sequence</param>
 /// <param name="size">size of the byte sequence</param>
 /// <returns></returns>
 public static uint ComputeCastagnoli(ReusableMemoryStream stream, long start, long size) =>
 CastagnoliCrc32.ComputeForStream(stream, start, size);
Beispiel #6
0
 /// <summary>
 /// Compute the CRC-32 of the byte sequence using IEEE standard polynomial values.
 /// This is the regular "internet" CRC.
 /// </summary>
 /// <param name="stream">byte stream</param>
 /// <param name="start">start offset of the byte sequence</param>
 /// <param name="size">size of the byte sequence</param>
 /// <returns></returns>
 public static uint Compute(ReusableMemoryStream stream, long start, long size) =>
 DefaultCrc32.ComputeForStream(stream, start, size);
Beispiel #7
0
 internal static void CheckCrc(int crc, ReusableMemoryStream stream, long crcStartPos)
 {
     CheckCrc(DefaultCrc32, crc, stream, crcStartPos);
 }
Beispiel #8
0
 internal static void CheckCrcCastagnoli(int crc, ReusableMemoryStream stream, long crcStartPos, long crcLength = -1)
 {
     CheckCrc(CastagnoliCrc32, crc, stream, crcStartPos, crcLength);
 }