Example #1
0
        // return the data header from the start of the data file/
        private BlobDataFileHeader GetDataHeader()
        {
            // if the data file is empty create a new header
            if (this.FileSize == 0)
            {
                var header = new BlobDataFileHeader();
                UpdateDataHeader(header);
                this.FileSize = 64;
                return(header);
            }

            BlobDataFileHeader dataHeader = null;

            using (var _fileStreamReader = new FileStream(_dataFilePath, FileMode.OpenOrCreate,
                                                          FileAccess.Read,
                                                          FileShare.Write,
                                                          (int)DiskBufferSizes.Default,
                                                          FileOptions.SequentialScan))
            {
                using (var _binaryReader = new BinaryReader(_fileStreamReader))
                {
                    _binaryReader.BaseStream.Position = 0;
                    var headerBytes = _binaryReader.ReadBytes(64);
                    dataHeader = BlobDataFileHeader.Parse(headerBytes);
                }
            }

            return(dataHeader);
        }
Example #2
0
        /// <summary>
        /// Parse the 64 Byte array into Data Header object.
        /// </summary>
        /// <param name="bytes">The 64 Byte array.</param>
        public static BlobDataFileHeader Parse(byte[] bytes)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("byte array is required.");
            }
            if (bytes.Length != 64)
            {
                throw new ArgumentException("byte array of length 64 is required.");
            }

            // If the system architecture is little-endian (that is, little end first),
            // reverse the byte array.
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            var dataHeader = new BlobDataFileHeader();

            dataHeader.RecordCount     = BitConverter.ToInt64(bytes, 0);
            dataHeader.LastRecordId    = BitConverter.ToInt64(bytes, 8);
            dataHeader.LargestRecordId = BitConverter.ToInt64(bytes, 16);
            return(dataHeader);
        }
Example #3
0
        // update the data header located at the start of the data file.
        private void UpdateDataHeader(BlobDataFileHeader dataHeader)
        {
            var headerBytes = dataHeader.GetBytes();

            using (var _fileStreamWriter = new FileStream(_dataFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read,
                                                          (int)DiskBufferSizes.Default, FileOptions.SequentialScan))
            {
                using (var _binaryWriter = new BinaryWriter(_fileStreamWriter))
                {
                    _binaryWriter.BaseStream.Position = 0;
                    _binaryWriter.Write(headerBytes);
                    _binaryWriter.Flush();
                }
            }
        }