コード例 #1
0
        private static long EstimateSize(IValue value)
        {
            long size = 0;

            foreach (var chunk in value.EncodeIntoChunks())
            {
                size += chunk.Length;
            }

            return(size);
        }
コード例 #2
0
ファイル: Codec.cs プロジェクト: limebell/bencodex.net
        /// <summary>Encodes a <paramref name="value"/>,
        /// and write it on an <paramref name="output"/> stream.</summary>
        /// <param name="value">A value to encode.</param>
        /// <param name="output">A stream that a value is printed on.</param>
        /// <exception cref="ArgumentException">Thrown when a given
        /// <paramref name="output"/> stream is not writable.</exception>
        public void Encode(IValue value, Stream output)
        {
            if (!output.CanWrite)
            {
                throw new ArgumentException(
                          "stream cannot be written to",
                          nameof(output)
                          );
            }

            foreach (byte[] chunk in value.EncodeIntoChunks())
            {
                output.Write(chunk, 0, chunk.Length);
            }
        }
コード例 #3
0
ファイル: TableSheetsState.cs プロジェクト: x86chi/lib9c
        public TableSheetsState(IDictionary <string, string> sheets) : base(Address)
        {
            TableSheets = sheets.ToImmutableDictionary();
            _serialized = Serialize();

            int ComputeHash(byte[] bytes)
            {
                unchecked
                {
                    var result = 0;
                    foreach (byte b in bytes)
                    {
                        result = result * 31 ^ b;
                    }
                    return(result);
                }
            }

            _hashCode = _serialized
                        .EncodeIntoChunks()
                        .Aggregate(0, (prev, bytes) => prev ^ ComputeHash(bytes));
        }