Esempio n. 1
0
        /// <summary>
        /// Gets an estimate of a <see cref="FileDataPart"/>.
        /// </summary>
        /// <param name="guid">The <see cref="Guid"/> that should be used in the size estimate.</param>
        /// <param name="start">The start position that should be used in the size estimate.</param>
        /// <param name="size">The size of the data that should be used in the size estimate.</param>
        /// <returns>The estimated size, in bytes, of a serialized <see cref="FileDataPart"/>.</returns>
        internal static int EstimatePartSize(Guid guid, long start, int size)
        {
            var part  = new FileDataPart(guid, start, new byte[size]);
            var bytes = ObjectPacker.Pack(part);

            return(bytes.Length + PacketHeader.HeaderSize(bytes.Length));
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the sizes of all the parts of the file at the given buffer size.
        /// </summary>
        /// <param name="bufferSize">The size of the buffer and amount of data to estimate with.</param>
        /// <returns>
        /// Returns an IEnumerable containing the sizes of all the different parts of the file - including the <see cref="FileData"/> header.
        /// </returns>
        internal IEnumerable <int> GetPartSizes(int bufferSize)
        {
            if (Info == null)
            {
                throw new NullReferenceException("File information not found.");
            }
            if (!Info.Exists)
            {
                throw new FileNotFoundException(Info.FullName);
            }

            var bytes = ObjectPacker.Pack(this);

            yield return(bytes.Length + PacketHeader.HeaderSize(bytes.Length));

            bufferSize -= MetaSize;
            long fileSize = Info.Length;
            long start    = 0;

            while (fileSize > 0)
            {
                if (fileSize > bufferSize)
                {
                    yield return(EstimatePartSize(Guid, start, bufferSize));

                    fileSize -= bufferSize;
                    start    += bufferSize;
                }
                else
                {
                    yield return(EstimatePartSize(Guid, start, (int)fileSize));

                    fileSize = 0;
                }
            }
        }