Пример #1
0
        public static bool EncodeStringLiterals(ReadOnlySpan <string> values, string?separator, Encoding?valueEncoding, Span <byte> destination, out int bytesWritten)
        {
            bytesWritten = 0;

            if (values.Length == 0)
            {
                return(EncodeStringLiteral("", valueEncoding: null, destination, out bytesWritten));
            }
            else if (values.Length == 1)
            {
                return(EncodeStringLiteral(values[0], valueEncoding, destination, out bytesWritten));
            }

            if (destination.Length != 0)
            {
                Debug.Assert(separator != null);
                int valueLength;

                // Calculate length of all parts and separators.
                if (valueEncoding is null || ReferenceEquals(valueEncoding, Encoding.Latin1))
                {
                    valueLength = checked ((int)(values.Length - 1) * separator.Length);
                    foreach (string part in values)
                    {
                        valueLength = checked ((int)(valueLength + part.Length));
                    }
                }
                else
                {
                    valueLength = checked ((int)(values.Length - 1) * valueEncoding.GetByteCount(separator));
                    foreach (string part in values)
                    {
                        valueLength = checked ((int)(valueLength + valueEncoding.GetByteCount(part)));
                    }
                }

                destination[0] = 0;
                if (IntegerEncoder.Encode(valueLength, 7, destination, out int integerLength))
                {
                    Debug.Assert(integerLength >= 1);
                    destination = destination.Slice(integerLength);
                    if (destination.Length >= valueLength)
                    {
                        if (valueEncoding is null)
                        {
                            string value = values[0];
                            EncodeValueStringPart(value, destination);
                            destination = destination.Slice(value.Length);

                            for (int i = 1; i < values.Length; i++)
                            {
                                EncodeValueStringPart(separator, destination);
                                destination = destination.Slice(separator.Length);

                                value = values[i];
                                EncodeValueStringPart(value, destination);
                                destination = destination.Slice(value.Length);
                            }
                        }
                        else
                        {
                            int written = valueEncoding.GetBytes(values[0], destination);
                            destination = destination.Slice(written);

                            for (int i = 1; i < values.Length; i++)
                            {
                                written     = valueEncoding.GetBytes(separator, destination);
                                destination = destination.Slice(written);

                                written     = valueEncoding.GetBytes(values[i], destination);
                                destination = destination.Slice(written);
                            }
                        }

                        bytesWritten = integerLength + valueLength;
                        return(true);
                    }
                }
            }
        /// <summary>
        /// 读取 JSON。
        /// </summary>
        /// <param name="filePath">给定的文件路径。</param>
        /// <param name="type">给定的反序列化对象类型。</param>
        /// <param name="encoding">给定的 <see cref="Encoding"/>(可选)。</param>
        /// <returns>返回反序列化对象。</returns>
        public static object?ReadJson(this string filePath, Type type, Encoding?encoding = null)
        {
            var json = File.ReadAllText(filePath, encoding ?? GlobalDefaults.CurrentEncoding);

            return(JsonSerializer.Deserialize(json, type, _options));
        }
Пример #3
0
 public static void SetConsoleOutputEncoding(Encoding enc) => s_outputEncoding = enc;
Пример #4
0
        /// <summary>
        /// Extracts all of the files in the specified archive to a directory on the file system.
        /// The specified directory must not exist. This method will create all subdirectories and the specified directory.
        /// If there is an error while extracting the archive, the archive will remain partially extracted. Each entry will
        /// be extracted such that the extracted file has the same relative path to the destinationDirectoryName as the entry
        /// has to the archive. The path is permitted to specify relative or absolute path information. Relative path information
        /// is interpreted as relative to the current working directory. If a file to be archived has an invalid last modified
        /// time, the first datetime representable in the Zip timestamp format (midnight on January 1, 1980) will be used.
        /// </summary>
        ///
        /// <exception cref="ArgumentException">sourceArchive or destinationDirectoryName is a zero-length string, contains only whitespace,
        /// or contains one or more invalid characters as defined by InvalidPathChars.</exception>
        /// <exception cref="ArgumentNullException">sourceArchive or destinationDirectoryName is null.</exception>
        /// <exception cref="PathTooLongException">sourceArchive or destinationDirectoryName specifies a path, file name,
        /// or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters,
        /// and file names must be less than 260 characters.</exception>
        /// <exception cref="DirectoryNotFoundException">The path specified by sourceArchive or destinationDirectoryName is invalid,
        /// (for example, it is on an unmapped drive).</exception>
        /// <exception cref="IOException">The directory specified by destinationDirectoryName already exists.
        /// -or- An I/O error has occurred. -or- An archive entry?s name is zero-length, contains only whitespace, or contains one or
        /// more invalid characters as defined by InvalidPathChars. -or- Extracting an archive entry would result in a file destination that is outside the destination directory (for example, because of parent directory accessors). -or- An archive entry has the same name as an already extracted entry from the same archive.</exception>
        /// <exception cref="UnauthorizedAccessException">The caller does not have the required permission.</exception>
        /// <exception cref="NotSupportedException">sourceArchive or destinationDirectoryName is in an invalid format. </exception>
        /// <exception cref="FileNotFoundException">sourceArchive was not found.</exception>
        /// <exception cref="InvalidDataException">The archive specified by sourceArchive: Is not a valid ZipArchive
        /// -or- An archive entry was not found or was corrupt. -or- An archive entry has been compressed using a compression method
        /// that is not supported.</exception>
        ///
        /// <param name="sourceArchiveFileName">The path to the archive on the file system that is to be extracted.</param>
        /// <param name="destinationDirectoryName">The path to the directory on the file system. The directory specified must not exist, but the directory that it is contained in must exist.</param>
        /// <param name="overwriteFiles">True to indicate overwrite.</param>
        /// <param name="entryNameEncoding">The encoding to use when reading or writing entry names in this ZipArchive.
        ///         ///     <para>NOTE: Specifying this parameter to values other than <c>null</c> is discouraged.
        ///         However, this may be necessary for interoperability with ZIP archive tools and libraries that do not correctly support
        ///         UTF-8 encoding for entry names.<br />
        ///         This value is used as follows:</para>
        ///     <para>If <c>entryNameEncoding</c> is not specified (<c>== null</c>):</para>
        ///     <list>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is <em>not</em> set,
        ///         use the current system default code page (<c>Encoding.Default</c>) in order to decode the entry name.</item>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header <em>is</em> set,
        ///         use UTF-8 (<c>Encoding.UTF8</c>) in order to decode the entry name.</item>
        ///     </list>
        ///     <para>If <c>entryNameEncoding</c> is specified (<c>!= null</c>):</para>
        ///     <list>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is <em>not</em> set,
        ///         use the specified <c>entryNameEncoding</c> in order to decode the entry name.</item>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header <em>is</em> set,
        ///         use UTF-8 (<c>Encoding.UTF8</c>) in order to decode the entry name.</item>
        ///     </list>
        ///     <para>Note that Unicode encodings other than UTF-8 may not be currently used for the <c>entryNameEncoding</c>,
        ///     otherwise an <see cref="ArgumentException"/> is thrown.</para>
        /// </param>
        public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName, Encoding?entryNameEncoding, bool overwriteFiles)
        {
            if (sourceArchiveFileName == null)
            {
                throw new ArgumentNullException(nameof(sourceArchiveFileName));
            }

            using (ZipArchive archive = Open(sourceArchiveFileName, ZipArchiveMode.Read, entryNameEncoding))
            {
                archive.ExtractToDirectory(destinationDirectoryName, overwriteFiles);
            }
        }
Пример #5
0
 /// <summary>
 /// Writes the <paramref name="content"/> string to the HTTP response.
 /// <para>
 /// This is an alias for <see cref="Text(string, string?, Encoding?)"/>.
 /// </para>
 /// </summary>
 /// <param name="content">The content to write to the response.</param>
 /// <param name="contentType">The content type (MIME type).</param>
 /// <param name="contentEncoding">The content encoding.</param>
 /// <returns>The created <see cref="IResult"/> object for the response.</returns>
 /// <remarks>
 /// If encoding is provided by both the 'charset' and the <paramref name="contentEncoding"/> parameters, then
 /// the <paramref name="contentEncoding"/> parameter is chosen as the final encoding.
 /// </remarks>
 public static IResult Content(string content, string?contentType = null, Encoding?contentEncoding = null)
 => Text(content, contentType, contentEncoding);
Пример #6
0
 private static SourceText BuildRecoverableTreeText(SyntaxTree tree, Encoding?encoding, CancellationToken cancellationToken)
 {
     // build text from root, so recoverable tree won't cycle.
     return(tree.GetRoot(cancellationToken).GetText(encoding));
 }
Пример #7
0
        public static bool EncodeValueString(ReadOnlySpan <string> values, string?separator, Encoding?valueEncoding, Span <byte> buffer, out int length)
        {
            if (values.Length == 1)
            {
                return(EncodeValueString(values[0], valueEncoding, buffer, out length));
            }

            if (values.Length == 0)
            {
                // TODO: this will be called with a string array from HttpHeaderCollection. Can we ever get a 0-length array from that? Assert if not.
                return(EncodeValueString(string.Empty, valueEncoding: null, buffer, out length));
            }

            if (buffer.Length > 0)
            {
                Debug.Assert(separator != null);
                int valueLength;
                if (valueEncoding is null || ReferenceEquals(valueEncoding, Encoding.Latin1))
                {
                    valueLength = separator.Length * (values.Length - 1);
                    foreach (string part in values)
                    {
                        valueLength += part.Length;
                    }
                }
                else
                {
                    valueLength = valueEncoding.GetByteCount(separator) * (values.Length - 1);
                    foreach (string part in values)
                    {
                        valueLength += valueEncoding.GetByteCount(part);
                    }
                }

                buffer[0] = 0;
                if (IntegerEncoder.Encode(valueLength, 7, buffer, out int nameLength))
                {
                    buffer = buffer.Slice(nameLength);
                    if (buffer.Length >= valueLength)
                    {
                        if (valueEncoding is null)
                        {
                            string value = values[0];
                            EncodeValueStringPart(value, buffer);
                            buffer = buffer.Slice(value.Length);

                            for (int i = 1; i < values.Length; i++)
                            {
                                EncodeValueStringPart(separator, buffer);
                                buffer = buffer.Slice(separator.Length);

                                value = values[i];
                                EncodeValueStringPart(value, buffer);
                                buffer = buffer.Slice(value.Length);
                            }
                        }
                        else
                        {
                            int written = valueEncoding.GetBytes(values[0], buffer);
                            buffer = buffer.Slice(written);

                            for (int i = 1; i < values.Length; i++)
                            {
                                written = valueEncoding.GetBytes(separator, buffer);
                                buffer  = buffer.Slice(written);

                                written = valueEncoding.GetBytes(values[i], buffer);
                                buffer  = buffer.Slice(written);
                            }
                        }

                        length = nameLength + valueLength;
                        return(true);
                    }
                }
            }
 /// <summary>
 /// Initializes a new instance of the System.IO.StreamReader class for the specified stream based on the specified character encoding, byte order mark detection option, and buffer size, and optionally leaves the stream open.
 /// </summary>
 /// <param name="stream">The stream to be read.</param>
 /// <param name="encoding">The character encoding to use.</param>
 /// <param name="detectEncodingFromByteOrderMarks">Indicates whether to look for byte order marks at the beginning of the file.</param>
 /// <param name="bufferSize">The minimum buffer size.</param>
 /// <param name="leaveOpen">true to leave the stream open after the System.IO.StreamReader object is disposed; otherwise, false.</param>
 public CsvStreamReader(Stream stream, Encoding?encoding = null, bool detectEncodingFromByteOrderMarks = true, int bufferSize = -1, bool leaveOpen = false) : base(stream, encoding, detectEncodingFromByteOrderMarks, bufferSize, leaveOpen)
 {
 }
Пример #9
0
 public static MemoryStream ToMemoryStream(this string value, bool writable = false, Encoding?encoding = default)
 {
     encoding = encoding ?? Encoding.UTF8;
     return(new MemoryStream(encoding.GetBytes(value), writable));
 }
Пример #10
0
        public static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, ReadOnlySpan <string> values, string valueSeparator, Encoding?valueEncoding, Span <byte> destination, out int bytesWritten)
        {
            if (EncodeNameString(name, destination, out int nameLength) && EncodeValueString(values, valueSeparator, valueEncoding, destination.Slice(nameLength), out int valueLength))
            {
                bytesWritten = nameLength + valueLength;
                return(true);
            }

            bytesWritten = 0;
            return(false);
        }
Пример #11
0
        public static string SaveResultFile(TestContext testContext, string fileName, string contents, string?testOutputPath = null, Encoding?encoding = null)
        {
            var filePath = GetResultFilePath(testContext, fileName, testOutputPath);

            if (encoding is not null)
            {
                File.WriteAllText(filePath, contents, encoding);
            }
            else
            {
                File.WriteAllText(filePath, contents);
            }

            return(filePath);
        }
Пример #12
0
        /// <summary>Retrieve the raw body as a string from the Request.Body stream.</summary>
        /// <param name="request">Request instance to apply to.</param>
        /// <param name="encoding">Optional - Encoding, defaults to UTF8.</param>
        /// <returns>The <see cref="Task" />.</returns>
        /// <exception cref="ArgumentException">Stream does not support reading.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="request" /> is <see langword="null" />.</exception>
        /// <exception cref="InvalidOperationException">The reader is currently in use by a previous read operation.</exception>
        /// <exception cref="ObjectDisposedException">The stream has been disposed.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The number of characters is larger than <see cref="int.MaxValue"></see>.</exception>
        public static Task <string> GetRawBodyStringAsync([NotNull] this HttpRequest request, Encoding?encoding = null)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            encoding ??= Encoding.UTF8;
            using var reader = new StreamReader(request.Body, encoding);
            return(reader.ReadToEndAsync());
        }
Пример #13
0
        /// <summary>
        /// Initializes a new instance of ZipArchive on the given stream in the specified mode, specifying whether to leave the stream open.
        /// </summary>
        /// <exception cref="ArgumentException">The stream is already closed. -or- mode is incompatible with the capabilities of the stream.</exception>
        /// <exception cref="ArgumentNullException">The stream is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">mode specified an invalid value.</exception>
        /// <exception cref="InvalidDataException">The contents of the stream could not be interpreted as a Zip file. -or- mode is Update and an entry is missing from the archive or is corrupt and cannot be read. -or- mode is Update and an entry is too large to fit into memory.</exception>
        /// <param name="stream">The input or output stream.</param>
        /// <param name="mode">See the description of the ZipArchiveMode enum. Read requires the stream to support reading, Create requires the stream to support writing, and Update requires the stream to support reading, writing, and seeking.</param>
        /// <param name="leaveOpen">true to leave the stream open upon disposing the ZipArchive, otherwise false.</param>
        /// <param name="entryNameEncoding">The encoding to use when reading or writing entry names in this ZipArchive.
        ///         ///     <para>NOTE: Specifying this parameter to values other than <c>null</c> is discouraged.
        ///         However, this may be necessary for interoperability with ZIP archive tools and libraries that do not correctly support
        ///         UTF-8 encoding for entry names.<br />
        ///         This value is used as follows:</para>
        ///     <para><strong>Reading (opening) ZIP archive files:</strong></para>
        ///     <para>If <c>entryNameEncoding</c> is not specified (<c>== null</c>):</para>
        ///     <list>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is <em>not</em> set,
        ///         use the current system default code page (<c>Encoding.Default</c>) in order to decode the entry name.</item>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header <em>is</em> set,
        ///         use UTF-8 (<c>Encoding.UTF8</c>) in order to decode the entry name.</item>
        ///     </list>
        ///     <para>If <c>entryNameEncoding</c> is specified (<c>!= null</c>):</para>
        ///     <list>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is <em>not</em> set,
        ///         use the specified <c>entryNameEncoding</c> in order to decode the entry name.</item>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header <em>is</em> set,
        ///         use UTF-8 (<c>Encoding.UTF8</c>) in order to decode the entry name.</item>
        ///     </list>
        ///     <para><strong>Writing (saving) ZIP archive files:</strong></para>
        ///     <para>If <c>entryNameEncoding</c> is not specified (<c>== null</c>):</para>
        ///     <list>
        ///         <item>For entry names that contain characters outside the ASCII range,
        ///         the language encoding flag (EFS) will be set in the general purpose bit flag of the local file header,
        ///         and UTF-8 (<c>Encoding.UTF8</c>) will be used in order to encode the entry name into bytes.</item>
        ///         <item>For entry names that do not contain characters outside the ASCII range,
        ///         the language encoding flag (EFS) will not be set in the general purpose bit flag of the local file header,
        ///         and the current system default code page (<c>Encoding.Default</c>) will be used to encode the entry names into bytes.</item>
        ///     </list>
        ///     <para>If <c>entryNameEncoding</c> is specified (<c>!= null</c>):</para>
        ///     <list>
        ///         <item>The specified <c>entryNameEncoding</c> will always be used to encode the entry names into bytes.
        ///         The language encoding flag (EFS) in the general purpose bit flag of the local file header will be set if and only
        ///         if the specified <c>entryNameEncoding</c> is a UTF-8 encoding.</item>
        ///     </list>
        ///     <para>Note that Unicode encodings other than UTF-8 may not be currently used for the <c>entryNameEncoding</c>,
        ///     otherwise an <see cref="ArgumentException"/> is thrown.</para>
        /// </param>
        /// <exception cref="ArgumentException">If a Unicode encoding other than UTF-8 is specified for the <code>entryNameEncoding</code>.</exception>
        public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding?entryNameEncoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            EntryNameAndCommentEncoding = entryNameEncoding;
            Stream?extraTempStream = null;

            try
            {
                _backingStream = null;

                // check stream against mode
                switch (mode)
                {
                case ZipArchiveMode.Create:
                    if (!stream.CanWrite)
                    {
                        throw new ArgumentException(SR.CreateModeCapabilities);
                    }
                    break;

                case ZipArchiveMode.Read:
                    if (!stream.CanRead)
                    {
                        throw new ArgumentException(SR.ReadModeCapabilities);
                    }
                    if (!stream.CanSeek)
                    {
                        _backingStream  = stream;
                        extraTempStream = stream = new MemoryStream();
                        _backingStream.CopyTo(stream);
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                    break;

                case ZipArchiveMode.Update:
                    if (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)
                    {
                        throw new ArgumentException(SR.UpdateModeCapabilities);
                    }
                    break;

                default:
                    // still have to throw this, because stream constructor doesn't do mode argument checks
                    throw new ArgumentOutOfRangeException(nameof(mode));
                }

                _mode = mode;
                if (mode == ZipArchiveMode.Create && !stream.CanSeek)
                {
                    _archiveStream = new PositionPreservingWriteOnlyStreamWrapper(stream);
                }
                else
                {
                    _archiveStream = stream;
                }
                _archiveStreamOwner = null;
                if (mode == ZipArchiveMode.Create)
                {
                    _archiveReader = null;
                }
                else
                {
                    _archiveReader = new BinaryReader(_archiveStream);
                }
                _entries               = new List <ZipArchiveEntry>();
                _entriesCollection     = new ReadOnlyCollection <ZipArchiveEntry>(_entries);
                _entriesDictionary     = new Dictionary <string, ZipArchiveEntry>();
                _readEntries           = false;
                _leaveOpen             = leaveOpen;
                _centralDirectoryStart = 0; // invalid until ReadCentralDirectory
                _isDisposed            = false;
                _numberOfThisDisk      = 0; // invalid until ReadCentralDirectory
                _archiveComment        = Array.Empty <byte>();

                switch (mode)
                {
                case ZipArchiveMode.Create:
                    _readEntries = true;
                    break;

                case ZipArchiveMode.Read:
                    ReadEndOfCentralDirectory();
                    break;

                case ZipArchiveMode.Update:
                default:
                    Debug.Assert(mode == ZipArchiveMode.Update);
                    if (_archiveStream.Length == 0)
                    {
                        _readEntries = true;
                    }
                    else
                    {
                        ReadEndOfCentralDirectory();
                        EnsureCentralDirectoryRead();
                        foreach (ZipArchiveEntry entry in _entries)
                        {
                            entry.ThrowIfNotOpenable(needToUncompress: false, needToLoadIntoMemory: true);
                        }
                    }
                    break;
                }
            }
            catch
            {
                if (extraTempStream != null)
                {
                    extraTempStream.Dispose();
                }

                throw;
            }
        }
Пример #14
0
 internal GoogleCsvWriter(Encoding?textEncoding) : base(null, textEncoding)
 {
 }
Пример #15
0
        public static bool EncodeLiteralHeaderFieldWithStaticNameReference(int index, string value, Encoding?valueEncoding, Span <byte> destination, out int bytesWritten)
        {
            // Requires at least two bytes (one for name reference header, one for value length)
            if (destination.Length >= 2)
            {
                destination[0] = 0b01010000;
                if (IntegerEncoder.Encode(index, 4, destination, out int headerBytesWritten))
                {
                    destination = destination.Slice(headerBytesWritten);

                    if (EncodeValueString(value, valueEncoding, destination, out int valueBytesWritten))
                    {
                        bytesWritten = headerBytesWritten + valueBytesWritten;
                        return(true);
                    }
                }
            }

            bytesWritten = 0;
            return(false);
        }
Пример #16
0
 internal static CsvWriter GetInstance(CsvCompatibility platform, IFormatProvider?formatProvider, Encoding?textEncoding) => platform switch
 {
 public static void AddCybtansFormatter(this IMvcBuilder builder, Encoding?encoding = null)
 {
     builder.AddMvcOptions(options => options.AddCytansFormatter(encoding));
 }
Пример #18
0
        private (SourceText?Source, bool?HasDocument) TryGetPdbMatchingSourceText(string sourceFilePath, Encoding?encoding, Project project)
        {
            bool?hasDocument = TryReadSourceFileChecksumFromPdb(sourceFilePath, project, out var symChecksum, out var algorithm);

            if (hasDocument != true)
            {
                return(Source : null, hasDocument);
            }

            try
            {
                using var fileStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);

                // We must use the encoding of the document as determined by the IDE (the editor).
                // This might differ from the encoding that the compiler chooses, so if we just relied on the compiler we
                // might end up updating the committed solution with a document that has a different encoding than
                // the one that's in the workspace, resulting in false document changes when we compare the two.
                var sourceText   = SourceText.From(fileStream, encoding, checksumAlgorithm: algorithm);
                var fileChecksum = sourceText.GetChecksum();

                if (fileChecksum.SequenceEqual(symChecksum))
                {
                    return(sourceText, hasDocument);
                }

                EditAndContinueWorkspaceService.Log.Write("Checksum differs for source file '{0}'", sourceFilePath);
                return(Source : null, hasDocument);
            }
            catch (Exception e)
            {
                EditAndContinueWorkspaceService.Log.Write("Error calculating checksum for source file '{0}': '{1}'", sourceFilePath, e.Message);
                return(Source : null, HasDocument : null);
            }
        }
 public static void AddCytansFormatter(this MvcOptions options, Encoding?encoding = null)
 {
     options.InputFormatters.Add(new BinaryInputFormatter(encoding ?? BinarySerializer.DefaultEncoding));
     options.OutputFormatters.Add(new BinaryOutputFormatter(encoding ?? BinarySerializer.DefaultEncoding));
     options.FormatterMappings.SetMediaTypeMappingForFormat("cybtans", MediaTypeHeaderValue.Parse(BinarySerializer.MEDIA_TYPE));
 }
Пример #20
0
 /// <summary>
 /// Construct tasks for reading source code files (possibly in parallel).
 ///
 /// The constructed syntax trees will be added (thread-safely) to the supplied
 /// list <paramref name="ret"/>.
 /// </summary>
 private static IEnumerable <Action> ReadSyntaxTrees(IEnumerable <string> sources, Analyser analyser, CSharpParseOptions?parseOptions, Encoding?encoding, IList <SyntaxTree> ret)
 {
     return(sources.Select <string, Action>(path => () =>
     {
         try
         {
             using var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
             var st = CSharpSyntaxTree.ParseText(SourceText.From(file, encoding), parseOptions, path);
             lock (ret)
                 ret.Add(st);
         }
         catch (IOException ex)
         {
             lock (analyser)
             {
                 analyser.Logger.Log(Severity.Error, "  Unable to open source file {0}: {1}", path, ex.Message);
                 ++analyser.CompilationErrors;
             }
         }
     }));
 }
Пример #21
0
        private async Task SerializeToStreamAsyncCore(Stream targetStream, bool async, CancellationToken cancellationToken)
        {
            Encoding?targetEncoding = JsonHelpers.GetEncoding(Headers.ContentType?.CharSet);

            // Wrap provided stream into a transcoding stream that buffers the data transcoded from utf-8 to the targetEncoding.
            if (targetEncoding != null && targetEncoding != Encoding.UTF8)
            {
#if NETCOREAPP
                Stream transcodingStream = Encoding.CreateTranscodingStream(targetStream, targetEncoding, Encoding.UTF8, leaveOpen: true);
                try
                {
                    if (async)
                    {
                        await SerializeAsyncHelper(transcodingStream, Value, ObjectType, _jsonSerializerOptions, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        // Have to use Utf8JsonWriter because JsonSerializer doesn't support sync serialization into stream directly.
                        // ToDo: Remove Utf8JsonWriter usage after https://github.com/dotnet/runtime/issues/1574
                        using var writer = new Utf8JsonWriter(transcodingStream);
                        SerializeHelper(writer, Value, ObjectType, _jsonSerializerOptions);
                    }
                }
                finally
                {
                    // Dispose/DisposeAsync will flush any partial write buffers. In practice our partial write
                    // buffers should be empty as we expect JsonSerializer to emit only well-formed UTF-8 data.
                    if (async)
                    {
                        await transcodingStream.DisposeAsync().ConfigureAwait(false);
                    }
                    else
                    {
                        transcodingStream.Dispose();
                    }
                }
#else
                Debug.Assert(async);

                using (TranscodingWriteStream transcodingStream = new TranscodingWriteStream(targetStream, targetEncoding))
                {
                    await SerializeAsyncHelper(transcodingStream, Value, ObjectType, _jsonSerializerOptions, cancellationToken).ConfigureAwait(false);

                    // The transcoding streams use Encoders and Decoders that have internal buffers. We need to flush these
                    // when there is no more data to be written. Stream.FlushAsync isn't suitable since it's
                    // acceptable to Flush a Stream (multiple times) prior to completion.
                    await transcodingStream.FinalWriteAsync(cancellationToken).ConfigureAwait(false);
                }
#endif
            }
            else
            {
                if (async)
                {
                    await SerializeAsyncHelper(targetStream, Value, ObjectType, _jsonSerializerOptions, cancellationToken).ConfigureAwait(false);
                }
                else
                {
#if NETCOREAPP
                    // Have to use Utf8JsonWriter because JsonSerializer doesn't support sync serialization into stream directly.
                    // ToDo: Remove Utf8JsonWriter usage after https://github.com/dotnet/runtime/issues/1574
                    using var writer = new Utf8JsonWriter(targetStream);
                    SerializeHelper(writer, Value, ObjectType, _jsonSerializerOptions);
#else
                    Debug.Fail("Synchronous serialization is only supported since .NET 5.0");
#endif
                }
            }
Пример #22
0
        private static async Task <SourceText> BuildRecoverableTreeTextAsync(SyntaxTree tree, Encoding?encoding, CancellationToken cancellationToken)
        {
            // build text from root, so recoverable tree won't cycle.
            var root = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            return(root.GetText(encoding));
        }
Пример #23
0
        /// <param name="client">The <see cref="IMailClient"/> instance.</param>
        /// <param name="host">The server host.</param>
        /// <param name="port">The server port.</param>
        /// <param name="useSsl">Indicates whether to use ssl.</param>
        /// <inheritdoc cref="IMailClient.Authenticate(Uri, string, string, Encoding)"/>
        public static void Authenticate(this IMailClient client, string host, int port, bool useSsl, string userName, string password, Encoding?encoding = null)
        {
            _ = client ?? throw new ArgumentNullException(nameof(client));
            _ = host ?? throw new ArgumentNullException(nameof(host));
            _ = userName ?? throw new ArgumentNullException(nameof(userName));
            _ = password ?? throw new ArgumentNullException(nameof(password));

            Uri uri = new UriBuilder(useSsl ? $"{client.Protocol}s" : client.Protocol, host, port).Uri;

            client.Authenticate(uri, userName, password, encoding);
        }
Пример #24
0
 /// <summary>
 /// Extracts all of the files in the specified archive to a directory on the file system.
 /// The specified directory must not exist. This method will create all subdirectories and the specified directory.
 /// If there is an error while extracting the archive, the archive will remain partially extracted. Each entry will
 /// be extracted such that the extracted file has the same relative path to the destinationDirectoryName as the entry
 /// has to the archive. The path is permitted to specify relative or absolute path information. Relative path information
 /// is interpreted as relative to the current working directory. If a file to be archived has an invalid last modified
 /// time, the first datetime representable in the Zip timestamp format (midnight on January 1, 1980) will be used.
 /// </summary>
 ///
 /// <exception cref="ArgumentException">sourceArchive or destinationDirectoryName is a zero-length string, contains only whitespace,
 /// or contains one or more invalid characters as defined by InvalidPathChars.</exception>
 /// <exception cref="ArgumentNullException">sourceArchive or destinationDirectoryName is null.</exception>
 /// <exception cref="PathTooLongException">sourceArchive or destinationDirectoryName specifies a path, file name,
 /// or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters,
 /// and file names must be less than 260 characters.</exception>
 /// <exception cref="DirectoryNotFoundException">The path specified by sourceArchive or destinationDirectoryName is invalid,
 /// (for example, it is on an unmapped drive).</exception>
 /// <exception cref="IOException">The directory specified by destinationDirectoryName already exists.
 /// -or- An I/O error has occurred. -or- An archive entry?s name is zero-length, contains only whitespace, or contains one or
 /// more invalid characters as defined by InvalidPathChars. -or- Extracting an archive entry would result in a file destination that is outside the destination directory (for example, because of parent directory accessors). -or- An archive entry has the same name as an already extracted entry from the same archive.</exception>
 /// <exception cref="UnauthorizedAccessException">The caller does not have the required permission.</exception>
 /// <exception cref="NotSupportedException">sourceArchive or destinationDirectoryName is in an invalid format. </exception>
 /// <exception cref="FileNotFoundException">sourceArchive was not found.</exception>
 /// <exception cref="InvalidDataException">The archive specified by sourceArchive: Is not a valid ZipArchive
 /// -or- An archive entry was not found or was corrupt. -or- An archive entry has been compressed using a compression method
 /// that is not supported.</exception>
 ///
 /// <param name="sourceArchiveFileName">The path to the archive on the file system that is to be extracted.</param>
 /// <param name="destinationDirectoryName">The path to the directory on the file system. The directory specified must not exist, but the directory that it is contained in must exist.</param>
 /// <param name="entryNameEncoding">The encoding to use when reading or writing entry names in this ZipArchive.
 ///         ///     <para>NOTE: Specifying this parameter to values other than <c>null</c> is discouraged.
 ///         However, this may be necessary for interoperability with ZIP archive tools and libraries that do not correctly support
 ///         UTF-8 encoding for entry names.<br />
 ///         This value is used as follows:</para>
 ///     <para>If <c>entryNameEncoding</c> is not specified (<c>== null</c>):</para>
 ///     <list>
 ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is <em>not</em> set,
 ///         use the current system default code page (<c>Encoding.Default</c>) in order to decode the entry name.</item>
 ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header <em>is</em> set,
 ///         use UTF-8 (<c>Encoding.UTF8</c>) in order to decode the entry name.</item>
 ///     </list>
 ///     <para>If <c>entryNameEncoding</c> is specified (<c>!= null</c>):</para>
 ///     <list>
 ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is <em>not</em> set,
 ///         use the specified <c>entryNameEncoding</c> in order to decode the entry name.</item>
 ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header <em>is</em> set,
 ///         use UTF-8 (<c>Encoding.UTF8</c>) in order to decode the entry name.</item>
 ///     </list>
 ///     <para>Note that Unicode encodings other than UTF-8 may not be currently used for the <c>entryNameEncoding</c>,
 ///     otherwise an <see cref="ArgumentException"/> is thrown.</para>
 /// </param>
 public static void ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName, Encoding?entryNameEncoding) =>
 ExtractToDirectory(sourceArchiveFileName, destinationDirectoryName, entryNameEncoding: entryNameEncoding, overwriteFiles: false);
Пример #25
0
 /// <inheritdoc cref="Authenticate(IMailClient, string, int, bool, string, string, Encoding)"/>
 public static void Authenticate(this IMailClient client, string host, string userName, string password, Encoding?encoding = null) =>
 Authenticate(client, host, port: 0, useSsl: false, userName, password, encoding);
Пример #26
0
        public static bool EncodeLiteralHeaderFieldWithoutIndexingNewName(string name, ReadOnlySpan <string> values, string separator, Encoding?valueEncoding, Span <byte> destination, out int bytesWritten)
        {
            // From https://tools.ietf.org/html/rfc7541#section-6.2.2
            // ------------------------------------------------------
            //   0   1   2   3   4   5   6   7
            // +---+---+---+---+---+---+---+---+
            // | 0 | 0 | 0 | 0 |       0       |
            // +---+---+-----------------------+
            // | H |     Name Length (7+)      |
            // +---+---------------------------+
            // |  Name String (Length octets)  |
            // +---+---------------------------+
            // | H |     Value Length (7+)     |
            // +---+---------------------------+
            // | Value String (Length octets)  |
            // +-------------------------------+

            if ((uint)destination.Length >= 3)
            {
                destination[0] = 0;
                if (EncodeLiteralHeaderName(name, destination.Slice(1), out int nameLength) &&
                    EncodeStringLiterals(values, separator, valueEncoding, destination.Slice(1 + nameLength), out int valueLength))
                {
                    bytesWritten = 1 + nameLength + valueLength;
                    return(true);
                }
            }

            bytesWritten = 0;
            return(false);
        }
Пример #27
0
        /// <param name="client">The <see cref="IMailClient"/> instance.</param>
        /// <param name="host">The server host.</param>
        /// <param name="port">The server port.</param>
        /// <param name="useSsl">Indicates whether to use ssl.</param>
        /// <inheritdoc cref="IMailClient.AuthenticateAsync(Uri, string, string, Encoding, CancellationToken)"/>
        public static Task AuthenticateAsync(this IMailClient client, string host, int port, bool useSsl, string userName, string password, Encoding?encoding = null, CancellationToken cancellationToken = default)
        {
            _ = client ?? throw new ArgumentNullException(nameof(client));
            _ = host ?? throw new ArgumentNullException(nameof(host));
            _ = userName ?? throw new ArgumentNullException(nameof(userName));
            _ = password ?? throw new ArgumentNullException(nameof(password));

            Uri uri = new UriBuilder(useSsl ? $"{client.Protocol}s" : client.Protocol, host, port).Uri;

            return(client.AuthenticateAsync(uri, userName, password, encoding, cancellationToken));
        }
        /// <summary>
        /// 读取 JSON。
        /// </summary>
        /// <typeparam name="T">指定的反序列化类型。</typeparam>
        /// <param name="filePath">给定的文件路径。</param>
        /// <param name="encoding">给定的 <see cref="Encoding"/>(可选)。</param>
        /// <returns>返回反序列化对象。</returns>
        public static T?ReadJson <T>(this string filePath, Encoding?encoding = null)
        {
            var json = File.ReadAllText(filePath, encoding ?? GlobalDefaults.CurrentEncoding);

            return(JsonSerializer.Deserialize <T>(json, _options));
        }
Пример #29
0
 /// <inheritdoc cref="AuthenticateAsync(IMailClient, string, int, bool, string, string, Encoding, CancellationToken)"/>
 public static Task AuthenticateAsync(this IMailClient client, string host, string userName, string password, Encoding?encoding = null, CancellationToken cancellationToken = default) =>
 AuthenticateAsync(client, host, port: 0, useSsl: false, userName, password, encoding, cancellationToken);
 /// <summary>
 /// Specifies the desired character encoding for the query results.
 /// </summary>
 /// <param name="encoding">An <see cref="Encoding" /> enum.</param>
 /// <returns>
 /// A reference to the current <see cref="IQueryRequest" /> for method chaining.
 /// </returns>
 /// <remarks>
 /// Optional.
 /// </remarks>
 public IQueryRequest Encoding(Encoding encoding)
 {
     _encoding = encoding;
     return this;
 }
Пример #31
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StreamLog"/> class.
 /// </summary>
 /// <param name="output"></param>
 /// <param name="encoding"></param>
 public StreamLog(Stream output, Encoding?encoding = null)
 {
     _output = new StreamWriter(output, encoding ?? Encoding.UTF8);
 }