コード例 #1
0
        /// <summary>
        /// Converts the specified <paramref name="value"/> to a byte array using the provided preferred encoding.
        /// </summary>
        /// <param name="value">The string to be converted.</param>
        /// <param name="setup">The <see cref="EncodingOptions"/> which need to be configured.</param>
        /// <returns>A <b>byte array</b> which is the result of the specified delegate <paramref name="setup"/>.</returns>
        /// <remarks><see cref="EncodingOptions"/> will be initialized with <see cref="EncodingOptions.DefaultPreambleSequence"/> and <see cref="EncodingOptions.DefaultEncoding"/>.</remarks>
        public static byte[] FromString(string value, Action <EncodingOptions> setup = null)
        {
            Validator.ThrowIfNull(value, nameof(value));
            var options = setup.ConfigureOptions();

            byte[] valueInBytes;
            switch (options.Preamble)
            {
            case PreambleSequence.Keep:
                valueInBytes = ByteUtility.CombineByteArrays(options.Encoding.GetPreamble(), options.Encoding.GetBytes(value));
                break;

            case PreambleSequence.Remove:
                valueInBytes = options.Encoding.GetBytes(value);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(setup));
            }
            return(valueInBytes);
        }
コード例 #2
0
        /// <summary>
        /// Converts the given <see cref="Stream"/> to a char array starting from position 0 (when supported).
        /// </summary>
        /// <param name="value">The <see cref="Stream"/> value to be converted.</param>
        /// <param name="setup">The <see cref="EncodingOptions"/> which need to be configured.</param>
        /// <returns>A sequence of characters equivalent to the <see cref="Stream"/> value.</returns>
        /// <remarks><see cref="EncodingOptions"/> will be initialized with <see cref="EncodingOptions.DefaultPreambleSequence"/> and <see cref="EncodingOptions.DefaultEncoding"/>.</remarks>
        public static char[] FromStream(Stream value, Action <EncodingOptions> setup = null)
        {
            Validator.ThrowIfNull(value, nameof(value));
            var options = setup.ConfigureOptions();

            if (options.Encoding.Equals(EncodingOptions.DefaultEncoding))
            {
                options.Encoding = options.DetectEncoding(value);
            }
            byte[] valueInBytes = ByteConverter.FromStream(value);
            switch (options.Preamble)
            {
            case PreambleSequence.Keep:
                break;

            case PreambleSequence.Remove:
                valueInBytes = ByteUtility.RemovePreamble(valueInBytes, options.Encoding);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(options));
            }
            return(options.Encoding.GetChars(valueInBytes));
        }
コード例 #3
0
 /// <summary>
 /// Removes trailing zero information (if any) from the specified <see cref="byte"/> array.
 /// </summary>
 /// <param name="input">The input <see cref="byte"/> array to process.</param>
 /// <returns>A <see cref="byte"/> array without trailing zeros.</returns>
 public static byte[] RemoveTrailingZeros(this byte[] input)
 {
     return(ByteUtility.RemoveTrailingZeros(input));
 }
コード例 #4
0
 /// <summary>
 /// Removes the preamble information (if present) from the specified <see cref="byte"/> array.
 /// </summary>
 /// <param name="input">The input <see cref="byte"/> array to process.</param>
 /// <param name="encoding">The encoding to use when determining the preamble to remove.</param>
 /// <returns>A <see cref="byte"/> array without preamble information.</returns>
 public static byte[] RemovePreamble(this byte[] input, Encoding encoding)
 {
     return(ByteUtility.RemovePreamble(input, encoding));
 }