Exemplo n.º 1
0
 public virtual void TestVerifyChunkedSumsSuccess()
 {
     AllocateDirectByteBuffers();
     FillDataAndValidChecksums();
     NativeCrc32.VerifyChunkedSums(bytesPerChecksum, checksumType.id, checksums, data,
                                   fileName, BasePosition);
 }
Exemplo n.º 2
0
 public virtual void TestVerifyChunkedSumsFail()
 {
     AllocateDirectByteBuffers();
     FillDataAndInvalidChecksums();
     exception.Expect(typeof(ChecksumException));
     NativeCrc32.VerifyChunkedSums(bytesPerChecksum, checksumType.id, checksums, data,
                                   fileName, BasePosition);
 }
Exemplo n.º 3
0
        /// <summary>Verify that the given checksums match the given data.</summary>
        /// <remarks>
        /// Verify that the given checksums match the given data.
        /// The 'mark' of the ByteBuffer parameters may be modified by this function,.
        /// but the position is maintained.
        /// </remarks>
        /// <param name="data">the DirectByteBuffer pointing to the data to verify.</param>
        /// <param name="checksums">
        /// the DirectByteBuffer pointing to a series of stored
        /// checksums
        /// </param>
        /// <param name="fileName">the name of the file being read, for error-reporting</param>
        /// <param name="basePos">the file position to which the start of 'data' corresponds</param>
        /// <exception cref="Org.Apache.Hadoop.FS.ChecksumException">if the checksums do not match
        ///     </exception>
        public virtual void VerifyChunkedSums(ByteBuffer data, ByteBuffer checksums, string
                                              fileName, long basePos)
        {
            if (type.size == 0)
            {
                return;
            }
            if (data.HasArray() && checksums.HasArray())
            {
                VerifyChunkedSums(((byte[])data.Array()), data.ArrayOffset() + data.Position(), data
                                  .Remaining(), ((byte[])checksums.Array()), checksums.ArrayOffset() + checksums.Position
                                      (), fileName, basePos);
                return;
            }
            if (NativeCrc32.IsAvailable())
            {
                NativeCrc32.VerifyChunkedSums(bytesPerChecksum, type.id, checksums, data, fileName
                                              , basePos);
                return;
            }
            int startDataPos = data.Position();

            data.Mark();
            checksums.Mark();
            try
            {
                byte[] buf = new byte[bytesPerChecksum];
                byte[] sum = new byte[type.size];
                while (data.Remaining() > 0)
                {
                    int n = Math.Min(data.Remaining(), bytesPerChecksum);
                    checksums.Get(sum);
                    data.Get(buf, 0, n);
                    summer.Reset();
                    summer.Update(buf, 0, n);
                    int calculated = (int)summer.GetValue();
                    int stored     = (sum[0] << 24 & unchecked ((int)(0xff000000))) | (sum[1] << 16 & unchecked (
                                                                                           (int)(0xff0000))) | (sum[2] << 8 & unchecked ((int)(0xff00))) | sum[3] & unchecked (
                        (int)(0xff));
                    if (calculated != stored)
                    {
                        long errPos = basePos + data.Position() - startDataPos - n;
                        throw new ChecksumException("Checksum error: " + fileName + " at " + errPos + " exp: "
                                                    + stored + " got: " + calculated, errPos);
                    }
                }
            }
            finally
            {
                data.Reset();
                checksums.Reset();
            }
        }