Beispiel #1
0
        /// <summary>
        /// Create a new data block instance from a byte array from data found within the container file.
        /// </summary>
        /// <param name="fs">The FileStream instance to use.</param>
        /// <param name="blockSize">The block size, in bytes.</param>
        /// <param name="ba">The byte array containing the full block.</param>
        /// <param name="logging">Instance of LoggingModule to use for logging events.</param>
        /// <returns>A populated DataBlock object.</returns>
        public static DataBlock FromBytes(FileStream fs, int blockSize, byte[] ba, LoggingModule logging)
        {
            try
            {
                if (ba == null || ba.Length < 1)
                {
                    throw new ArgumentNullException(nameof(ba));
                }
                if (ba.Length < 64)
                {
                    throw new ArgumentException("Byte array has length less than 64");
                }
                if (blockSize < 4096)
                {
                    throw new ArgumentOutOfRangeException("Block size must be greater than or equal to 4096");
                }
                if (blockSize % 4096 != 0)
                {
                    throw new ArgumentOutOfRangeException("Block size must be evenly divisible by 4096");
                }
                if (fs == null)
                {
                    throw new ArgumentNullException(nameof(fs));
                }

                if (logging != null)
                {
                    logging.Log(LoggingModule.Severity.Debug, "FromBytes converting " + ba.Length + " bytes to DataBlock");
                }

                DataBlock ret = new DataBlock(fs, blockSize, null, logging);
                ret.Signature  = new byte[4];
                ret.Logging    = logging;
                ret.Filestream = fs;

                byte[] temp;

                Buffer.BlockCopy(ba, 0, ret.Signature, 0, 4);

                temp = new byte[4];
                Buffer.BlockCopy(ba, 8, temp, 0, 4);
                ret.Signature = temp;

                temp = new byte[8];
                Buffer.BlockCopy(ba, 4, temp, 0, 8);
                ret.ParentBlock = BitConverter.ToInt64(temp, 0);

                temp = new byte[8];
                Buffer.BlockCopy(ba, 12, temp, 0, 8);
                ret.ChildBlock = BitConverter.ToInt32(temp, 0);

                temp = new byte[4];
                Buffer.BlockCopy(ba, 20, temp, 0, 4);
                ret.DataLength = BitConverter.ToInt32(temp, 0);

                if (logging != null)
                {
                    logging.Log(LoggingModule.Severity.Debug, "FromBytes reading data " + ret.DataLength + " bytes from position 64");
                }
                temp = new byte[ret.DataLength];
                Buffer.BlockCopy(ba, 64, temp, 0, ret.DataLength);
                ret.Data = temp;

                return(ret);
            }
            catch (Exception e)
            {
                if (logging != null)
                {
                    logging.LogException("DataBlock", "FromBytes", e);
                }
                else
                {
                    LoggingModule.ConsoleException("DataBlock", "FromBytes", e);
                }
                throw;
            }
        }