Esempio n. 1
0
        /// <summary>
        /// Load the configuration file (if it exists).
        /// </summary>
        /// <param name="configuration_file">path to the configuration file</param>
        public static Configuration?LoadConfiguration(string configuration_file)
        {
            XmlSerializer?serializer = new XmlSerializer(typeof(Configuration));
            FileStream?   fileStream = null;

            try
            {
                fileStream = new FileStream(configuration_file, FileMode.Open);
            }
            catch (Exception e)
            {
                DebugUtilities.WriteError("Could not open configuration file; error was: " + e.Message);
                Environment.Exit(1);
            }
            try
            {
                // fileStream/serializer are now nullable; return null if that's the case. (gwyneth 20220127)
                if (fileStream != null && serializer != null)
                {
                    fileStream.Seek(0, SeekOrigin.Begin);
                    return((Configuration?)serializer.Deserialize(fileStream));
                }
            }
            catch (Exception e)
            {
                DebugUtilities.WriteError("Could not parse XML file - error was: " + e.Message);
                Environment.Exit(1);                 // this is overkill... we might be able to deal with most errors using reasonable defaults... (gwyneth 20220127)
            }
            return(null);
        } // end constructor
Esempio n. 2
0
        public async Task DeviceInterfaceCanBeOpenedForAsyncIO()
        {
            FileStream?fileStream = OpenFirstAvailableDeviceInterface();

            if (fileStream is null)
            {
                // it's OK to not have any such devices available
                // this test is just best effort
                return;
            }

            using (fileStream)
            {
                Assert.True(fileStream.CanRead);
                Assert.False(fileStream.CanWrite);
                Assert.False(fileStream.CanSeek); // #54143

                try
                {
                    CancellationTokenSource cts = new(TimeSpan.FromMilliseconds(250));

                    await fileStream.ReadAsync(new byte[4096], cts.Token);
                }
                catch (OperationCanceledException)
                {
                    // most likely there is no data available and the task is going to get cancelled
                    // which is fine, we just want to make sure that reading from devices is supported (#54143)
                }
            }
        }
Esempio n. 3
0
    public static EdgeSpanStream Load(string filePath)
    {
        FileStream?stream = null;

        try
        {
            stream = File.OpenRead(filePath);

            using var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true);
            var readHeader = reader.ReadString();;
            if (readHeader != HeaderText)
            {
                throw new InvalidOperationException($"Unsupported edge span file format: {filePath}");
            }

            var viewPort        = reader.ReadViewPort();
            var computationType = (ComputationType)reader.ReadInt32();
            var count           = reader.ReadInt32();
            Log.Info($"Loaded edge spans with resolution ({viewPort.Resolution.Width:N0}x{viewPort.Resolution.Height:N0}), " +
                     $"area {viewPort.Area}, and {count:N0} edge spans.");
            return(new EdgeSpanStream(viewPort, computationType, count, stream));
        }
        catch (Exception)
        {
            stream?.Dispose();
            throw;
        }
    }
        /// <summary>Updates the collection of files</summary>
        public void SetFiles(IEnumerable <string> filepaths)
        {
            Files.Clear();
            foreach (var f in filepaths)
            {
                Files.Add(new FileInfo(f));
            }

            // Determine the combined file length
            m_total_length = 0;
            m_boundary.Clear();
            foreach (var f in Files)
            {
                m_total_length += f.Length;
                m_boundary.Add(m_total_length);
            }
            m_boundary.Add(m_total_length);             // Add an extra one to avoid extra ifs

            // Reset the file position
            m_position = 0;
            m_fs       = null;
            m_fs_index = -1;

            // Force 'FileStream' to be created so that a lock is held from construction
            var _ = FileStream;
        }
Esempio n. 5
0
    public virtual void ReadModule()
    {
        tempAssembly = $"{AssemblyFilePath}.tmp";
        File.Copy(AssemblyFilePath, tempAssembly, true);

        if (debugReaderProvider != null && DebugSymbols != DebugSymbolsType.Embedded)
        {
            tempSymbols = $"{pdbPath}.tmp";
            if (File.Exists(pdbPath))
            {
                File.Copy(pdbPath, tempSymbols, true);
                SymbolStream = FileEx.OpenRead(tempSymbols);
            }
        }

        var readerParameters = new ReaderParameters
        {
            AssemblyResolver     = assemblyResolver,
            ReadSymbols          = SymbolStream != null || DebugSymbols == DebugSymbolsType.Embedded,
            SymbolReaderProvider = debugReaderProvider,
            SymbolStream         = SymbolStream,
        };

        ModuleDefinition = ModuleDefinition.ReadModule(tempAssembly, readerParameters);
    }
        /// <summary>
        /// Used by the 2 Create factory method groups.  A null fileHandle specifies that the
        /// memory mapped file should not be associated with an existing file on disk (i.e. start
        /// out empty).
        /// </summary>

        private static SafeMemoryMappedFileHandle CreateCore(
            FileStream?fileStream, string?mapName, HandleInheritability inheritability,
            MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity)
        {
            SafeFileHandle?fileHandle = fileStream != null ? fileStream.SafeFileHandle : null;

            Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(inheritability);


            SafeMemoryMappedFileHandle handle = fileHandle != null?
                                                Interop.CreateFileMapping(fileHandle, ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName) :
                                                    Interop.CreateFileMapping(new IntPtr(-1), ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName);

            int errorCode = Marshal.GetLastWin32Error();

            if (!handle.IsInvalid)
            {
                if (errorCode == Interop.Errors.ERROR_ALREADY_EXISTS)
                {
                    handle.Dispose();
                    throw Win32Marshal.GetExceptionForWin32Error(errorCode);
                }
            }
            else // handle.IsInvalid
            {
                handle.Dispose();
                throw Win32Marshal.GetExceptionForWin32Error(errorCode);
            }

            return(handle);
        }
Esempio n. 7
0
        internal static bool TryOpen(string path, out FileStream?stream)
        {
            if (IsWindows)
            {
                var handle = CreateFile(path, ACCESS_MASK.GenericRight.GENERIC_READ, FileShare.FILE_SHARE_READ, (SECURITY_ATTRIBUTES?)null, CreationDisposition.OPEN_EXISTING, CreateFileFlags.FILE_ATTRIBUTE_NORMAL, SafeObjectHandle.Null);

                if (!handle.IsInvalid)
                {
                    var fileHandle = new SafeFileHandle(handle.DangerousGetHandle(), ownsHandle: true);
                    handle.SetHandleAsInvalid();
                    stream = new FileStream(fileHandle, System.IO.FileAccess.Read);
                    return(true);
                }
                else
                {
                    stream = null;
                    return(false);
                }
            }
            else
            {
                if (!File.Exists(path))
                {
                    stream = null;
                    return(false);
                }

                stream = File.OpenRead(path);
                return(true);
            }
        }
Esempio n. 8
0
        public async Task <IActionResult> Upload(IFormFileCollection files)
        {
            var folder = new DirectoryInfo(Path.Combine(environment.ContentRootPath, "media", DateTime.Now.ToString("yyMM")));

            folder.Create();
            var status = new List <(string name, bool success)>(capacity: files.Count);

            foreach (IFormFile file in files)
            {
                FileInfo   physical = new FileInfo(Path.Combine(folder.FullName, file.FileName));
                FileStream?stream   = null;
                try
                {
                    stream = new FileStream(physical.FullName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
                    await file.CopyToAsync(stream);

                    status.Add((file.FileName, true));
                }
                catch (IOException)
                {
                    status.Add((file.FileName, false));
                }
                finally
                {
                    if (stream != null)
                    {
                        await stream.DisposeAsync();
                    }
                }
            }

            return(View("Uploaded", new MediaUploadViewModel(status)));
        }
Esempio n. 9
0
 /// <inheritdoc />
 public new void Dispose()
 {
     lock (_lock) {
         _openStream?.Dispose();
         _openStream = null;
     }
 }
        internal static bool TryOpen(string path, out FileStream?stream)
        {
            if (IsWindows)
            {
                var handle = PInvoke.CreateFile(path, FILE_ACCESS_FLAGS.FILE_GENERIC_READ, FILE_SHARE_MODE.FILE_SHARE_READ, lpSecurityAttributes: null, FILE_CREATION_DISPOSITION.OPEN_EXISTING, FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, null);

                if (!handle.IsInvalid)
                {
                    var fileHandle = new SafeFileHandle(handle.DangerousGetHandle(), ownsHandle: true);
                    handle.SetHandleAsInvalid();
                    stream = new FileStream(fileHandle, System.IO.FileAccess.Read);
                    return(true);
                }
                else
                {
                    stream = null;
                    return(false);
                }
            }
            else
            {
                if (!File.Exists(path))
                {
                    stream = null;
                    return(false);
                }

                stream = File.OpenRead(path);
                return(true);
            }
        }
Esempio n. 11
0
        public static Task <List <GlobalFindResult> > Find(string rootDirectory, string text, string[] extensions)
        {
            ConcurrentBag <GlobalFindResult> res = new();

            Parallel.ForEach(extensions, (ex) =>
            {
                foreach (string?file in Directory.GetFiles(rootDirectory, ex, SearchOption.AllDirectories))
                {
                    using FileStream? f   = File.OpenRead(file);
                    int line              = 1;
                    using StreamReader sr = new(f);
                    string?lineText       = null;
                    while ((lineText = sr.ReadLine()) != null)
                    {
                        if (lineText.Contains(text, System.StringComparison.InvariantCultureIgnoreCase))
                        {
                            res.Add(new GlobalFindResult(file, line, lineText, text));
                        }
                        line++;
                    }
                }
            });

            return(Task.FromResult(res.OrderBy(p => p.FileName).ThenBy(p => p.LineNumber).ToList()));
        }
Esempio n. 12
0
        /// <summary>
        /// 把对象序列化到二进制文件中
        /// </summary>
        /// <param name="objToSave">欲保存的对象</param>
        /// <param name="filename">包含路径的文件名</param>
        public static void Serialize(object objToSave, string filename)
        {
            FileStream?fs = null;

            try {
                fs = new FileStream(filename, FileMode.Create);
                Serialize(objToSave, fs);
                DebugHelper.WriteLine("into binary file: " + filename);
            }
            catch (Exception ex) {
                fs?.Close();
                fs?.Dispose();
                fs = null;
                try {
                    File.Delete(filename);
                }
                finally {
                    throw ex;
                }
            }
            finally {
                fs?.Close();
                fs?.Dispose();
            }
        }
        /// <summary>Initializes the memory-mapped file handle.</summary>
        /// <param name="fileStream">The underlying file stream; may be null.</param>
        /// <param name="ownsFileStream">Whether this SafeHandle is responsible for Disposing the fileStream.</param>
        /// <param name="inheritability">The inheritability of the memory-mapped file.</param>
        /// <param name="access">The access for the memory-mapped file.</param>
        /// <param name="options">The options for the memory-mapped file.</param>
        /// <param name="capacity">The capacity of the memory-mapped file.</param>
        internal SafeMemoryMappedFileHandle(
            FileStream?fileStream, bool ownsFileStream, HandleInheritability inheritability,
            MemoryMappedFileAccess access, MemoryMappedFileOptions options,
            long capacity)
            : base(ownsHandle: true)
        {
            Debug.Assert(!ownsFileStream || fileStream != null, "We can only own a FileStream we're actually given.");

            // Store the arguments.  We'll actually open the map when the view is created.
            _fileStream     = fileStream;
            _ownsFileStream = ownsFileStream;
            _inheritability = inheritability;
            _access         = access;
            _options        = options;
            _capacity       = capacity;

            IntPtr handlePtr;

            if (fileStream != null)
            {
                bool           ignored = false;
                SafeFileHandle handle  = fileStream.SafeFileHandle;
                handle.DangerousAddRef(ref ignored);
                _fileStreamHandle = handle;
                handlePtr         = handle.DangerousGetHandle();
            }
            else
            {
                handlePtr = IntPtr.MaxValue;
            }

            SetHandle(handlePtr);
        }
Esempio n. 14
0
    public async ValueTask RunAsync([Option("storage", new char[] { 's' })] string storageDirectoryPath, [Option("listen", new char[] { 'l' })] string listenAddress, [Option("verbose", new char[] { 'v' })] bool verbose = false)
    {
        try
        {
            DirectoryHelper.CreateDirectory(storageDirectoryPath);

            SetLogsDirectory(Path.Combine(storageDirectoryPath, "logs"));
            if (verbose)
            {
                ChangeLogLevel(NLog.LogLevel.Trace);
            }

            _lockFileStream = new FileStream(Path.Combine(storageDirectoryPath, "lock"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1, FileOptions.DeleteOnClose);

            _logger.Info("Starting...");
            _logger.Info("AssemblyInformationalVersion: {0}", Assembly.GetExecutingAssembly().GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion);

            await Runner.EventLoopAsync(Path.Combine(storageDirectoryPath, "db"), OmniAddress.Parse(listenAddress), this.Context.CancellationToken);
        }
        catch (Exception e)
        {
            _logger.Error(e);
        }
        finally
        {
            _logger.Info("Stopping...");
            NLog.LogManager.Shutdown();

            _lockFileStream?.Dispose();
        }
    }
Esempio n. 15
0
        private Result TryCreateLockFile()
        {
            void CreateLockFileStream()
            {
                lockStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);
                lockStream.Write(BitConverter.GetBytes(CurrentProcessID), 0, sizeof(int));
                lockStream.Flush(true);
            }

            try{
                CreateLockFileStream();
                return(Result.Success);
            }catch (DirectoryNotFoundException) {
                try{
                    CreateLockFileStream();
                    return(Result.Success);
                }catch {
                    ReleaseLockFileStream();
                    return(Result.Fail);
                }
            }catch (IOException) {
                return(Result.HasProcess);
            }catch {
                ReleaseLockFileStream();
                return(Result.Fail);
            }
        }
Esempio n. 16
0
        // </ThrowException>

        // <TestFinally>
        static void TestFinally()
        {
            FileStream?file = null;
            //Change the path to something that works on your machine.
            FileInfo fileInfo = new System.IO.FileInfo("./file.txt");

            try
            {
                file = fileInfo.OpenWrite();
                file.WriteByte(0xF);
            }
            finally
            {
                // Closing the file allows you to reopen it immediately - otherwise IOException is thrown.
                file?.Close();
            }

            try
            {
                file = fileInfo.OpenWrite();
                Console.WriteLine("OpenWrite() succeeded");
            }
            catch (IOException)
            {
                Console.WriteLine("OpenWrite() failed");
            }
        }
Esempio n. 17
0
        private void StartNewFile()
        {
            var    now  = DateTime.UtcNow;
            string path = Configuration.Path;
            string fileName;

            if (Configuration.SortByDate)
            {
                path = Path.Combine(path, "Log-" + now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
                string file = now.ToString("yyyy-MM-dd-HH-mm", CultureInfo.InvariantCulture);
                fileName = Path.Combine(path, "Nmea-" + file + ".txt");
            }
            else
            {
                string file = now.ToString("yyyy-MM-dd-HH-mm", CultureInfo.InvariantCulture);
                fileName = Path.Combine(path, "Nmea-" + file + ".txt");
            }

            if (_logFile != null && _textWriter != null)
            {
                _textWriter.Flush();
                _textWriter.Close();
                _logFile.Close();
                _textWriter.Dispose();
                _logFile.Dispose();
                _textWriter = null;
                _logFile    = null;
            }

            Directory.CreateDirectory(path);
            _logFile    = new FileStream(fileName, FileMode.Append, FileAccess.Write);
            _textWriter = new StreamWriter(_logFile);
        }
        internal XmlWriter CreateWriter(string outputFileName)
        {
            if (outputFileName == null)
            {
                throw new ArgumentNullException(nameof(outputFileName));
            }

            // need to clone the settigns so that we can set CloseOutput to true to make sure the stream gets closed in the end
            XmlWriterSettings newSettings = this;

            if (!newSettings.CloseOutput)
            {
                newSettings             = newSettings.Clone();
                newSettings.CloseOutput = true;
            }

            FileStream?fs = null;

            try
            {
                // open file stream
                fs = new FileStream(outputFileName, FileMode.Create, FileAccess.Write, FileShare.Read, 0x1000, _useAsync);

                // create writer
                return(newSettings.CreateWriter(fs));
            }
            catch
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
                throw;
            }
        }
        async Task ICrossProcessSemaphore.WaitAsync()
        {
            await LocalSemaphore.WaitAsync().ConfigureAwait(false);

            bool success = false;

            try {
                while (true)
                {
                    try {
                        lock (LocalSemaphore) {
                            if (FileLock != null)
                            {
                                throw new ArgumentNullException(nameof(FileLock));
                            }

                            EnsureFileExists();

                            FileLock = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
                            success  = true;

                            return;
                        }
                    } catch (IOException) {
                        await Task.Delay(SpinLockDelay).ConfigureAwait(false);
                    }
                }
            } finally {
                if (!success)
                {
                    LocalSemaphore.Release();
                }
            }
        }
Esempio n. 20
0
 public TiffFileStreamContentSource(string fileName, bool preferAsync)
 {
     _fileName    = fileName ?? throw new ArgumentNullException(nameof(fileName));
     _preferAsync = preferAsync;
     _lock        = new object();
     _fileStream  = GetOrCreateStream(preferAsync);
 }
Esempio n. 21
0
        internal static unsafe bool TryOpen(ReadOnlySpan <char> path, [NotNullWhen(true)] out FileStream?stream)
        {
            if (IsWindows)
            {
                var handle = CreateFile(path, ACCESS_MASK.GenericRight.GENERIC_READ, FileShare.FILE_SHARE_READ, null, CreationDisposition.OPEN_EXISTING, CreateFileFlags.FILE_ATTRIBUTE_NORMAL, SafeObjectHandle.Null);

                if (!handle.IsInvalid)
                {
                    var fileHandle = new SafeFileHandle(handle.DangerousGetHandle(), ownsHandle: true);
                    handle.SetHandleAsInvalid();
                    stream = new FileStream(fileHandle, System.IO.FileAccess.Read);
                    return(true);
                }
                else
                {
                    stream = null;
                    return(false);
                }
            }
            else
            {
                // Make sure to trim the trailing \0
                string fullPath = GetUtf16String(path.Slice(0, path.Length - 1));

                if (!File.Exists(fullPath))
                {
                    stream = null;
                    return(false);
                }

                stream = File.OpenRead(fullPath);
                return(true);
            }
        }
Esempio n. 22
0
        public DirSuperBackup(DirSuperBackupOptions?options = null)
        {
            try
            {
                this.Options = options ?? new DirSuperBackupOptions();
                this.Lfs     = new LargeFileSystem(new LargeFileSystemParams(this.Fs));
                this.LfsUtf8 = new Utf8BomFileSystem(new Utf8BomFileSystemParam(this.Lfs));

                if (Options.InfoLogFileName._IsFilled())
                {
                    InfoLogFileObj    = this.LfsUtf8.OpenOrCreateAppend(Options.InfoLogFileName, flags: FileFlags.AutoCreateDirectory | FileFlags.BackupMode | FileFlags.LargeFs_AppendWithoutCrossBorder);
                    InfoLogFileStream = InfoLogFileObj.GetStream(true);
                    InfoLogWriter     = new StreamWriter(InfoLogFileStream);
                }

                if (Options.ErrorLogFileName._IsFilled())
                {
                    ErrorLogFileObj    = this.LfsUtf8.OpenOrCreateAppend(Options.ErrorLogFileName, flags: FileFlags.AutoCreateDirectory | FileFlags.BackupMode | FileFlags.LargeFs_AppendWithoutCrossBorder);
                    ErrorLogFileStream = ErrorLogFileObj.GetStream(true);
                    ErrorLogWriter     = new StreamWriter(ErrorLogFileStream);
                }

                WriteLogAsync(DirSuperBackupLogType.Error, "------------------")._GetResult();
                WriteLogAsync(DirSuperBackupLogType.Error, "Start")._GetResult();
            }
            catch
            {
                this._DisposeSafe();
                throw;
            }
        }
Esempio n. 23
0
        /// <summary>Returns the file stream appropriate for byte offset 'offset'</summary>
        public FileStream?FileStreamAtOffset(long offset)
        {
            var fidx = FileIndexAtOffset(offset);

            if (m_fs != null && fidx == m_fs_index)
            {
                return(m_fs);
            }
            if (m_fs != null)
            {
                m_fs.Dispose();
            }

            m_fs_index = fidx;
            System.Diagnostics.Debug.Assert(m_fs_index >= -1 && m_fs_index <= Files.Count);

            if (m_fs_index < 0 || Files.Count == 0)
            {
                m_fs = null;
            }
            else if (m_fs_index >= Files.Count)
            {
                m_fs          = new FileStream(Files[Files.Count - 1].FullName, FileMode.Open, FileAccess.Read, FileShare, FileBufferSize, FileOptions);
                m_fs.Position = m_fs.Length;
            }
            else
            {
                m_fs     = new FileStream(Files[m_fs_index].FullName, FileMode.Open, FileAccess.Read, FileShare, FileBufferSize, FileOptions);
                Position = Position;
            }
            return(m_fs);
        }
        internal void SetUnmanagedStructures(FileStream?fileStream, byte[]?preBuffer, byte[]?postBuffer, bool doDisconnect)
        {
            _fileStream   = fileStream;
            _doDisconnect = doDisconnect;

            int buffsNumber = 0;

            if (preBuffer != null && preBuffer.Length > 0)
            {
                ++buffsNumber;
            }

            if (postBuffer != null && postBuffer.Length > 0)
            {
                ++buffsNumber;
            }

            object[]? objectsToPin = null;
            if (buffsNumber != 0)
            {
                objectsToPin = new object[buffsNumber];

                if (preBuffer != null && preBuffer.Length > 0)
                {
                    objectsToPin[--buffsNumber] = preBuffer;
                }

                if (postBuffer != null && postBuffer.Length > 0)
                {
                    objectsToPin[--buffsNumber] = postBuffer;
                }
            }

            base.SetUnmanagedStructures(objectsToPin);
        }
Esempio n. 25
0
        /// <summary>
        /// Attempt to acquire the lock by opening the lock file with FileShare.None.  Sets "Stream"
        /// and returns true if successful, returns false if the lock is already held by another
        /// thread or process.  Guard must be held when calling this routine.
        /// </summary>
        internal bool TryLockFile()
        {
            Debug.Assert(Stream is null);
            FileStream?stream = null;

            try
            {
                stream = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                // On some targets, the file locking used to implement FileShare.None may not be
                // atomic with opening/creating the file.   This creates a race window when another
                // thread holds the lock and is just about to unlock: we may be able to open the
                // file here, then the other thread unlocks and deletes the file, and then we
                // acquire the lock on our file handle - but the actual file is already deleted.
                // To close this race, we verify that the file does in fact still exist now that
                // we have successfully acquired the locked FileStream.   (Note that this check is
                // safe because we cannot race with an other attempt to create the file since we
                // hold the guard, and after the FileStream constructor returned we can no race
                // with file deletion because we hold the lock.)
                if (!File.Exists(FilePath))
                {
                    // To simplify the logic, we treat this case as "unable to acquire the lock"
                    // because it we caught another process while it owned the lock and was just
                    // giving it up.  If the caller retries, we'll likely acquire the lock then.
                    stream.Dispose();
                    return(false);
                }
            }
            catch (Exception)
            {
                stream?.Dispose();
                return(false);
            }
            Stream = stream;
            return(true);
        }
Esempio n. 26
0
        private int LoadTextureFromFile(string filePath)
        {
            FileStream?stream = File.OpenRead(filePath);

            using (Image <Rgba32> image = Image.Load <Rgba32>(stream))
            {
                int width  = image.Width;
                int height = image.Height;

                int[] data = new int[width * height];

                // Just simple bit manipulation to convert RGBA to ABGR.
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        Rgba32 color = image[x, y];
                        data[(y * width) + x] = (color.A << 24) | (color.B << 16) | (color.G << 8) | (color.R << 0);
                    }
                }

                GL.CreateTextures(TextureTarget.Texture2D, 1, out int result);

                GL.TextureStorage2D(result, 1, SizedInternalFormat.Rgba8, width, height);
                GL.TextureParameter(result, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                GL.TextureParameter(result, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
                GL.TextureParameter(result, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
                GL.TextureParameter(result, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);

                GL.TextureSubImage2D(result, 0, 0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, data);

                return(result);
            }
        }
Esempio n. 27
0
        internal ResponseStreamAsyncResult(ResponseBody responseStream, FileStream fileStream, long offset,
                                           long count, bool chunked, CancellationToken cancellationToken)
            : this(responseStream, cancellationToken)
        {
            var boundHandle = responseStream.RequestContext.Server.RequestQueue.BoundHandle;

            _fileStream = fileStream;

            if (count == 0)
            {
                _dataChunks = null;
                _overlapped = new SafeNativeOverlapped(boundHandle,
                                                       boundHandle.AllocateNativeOverlapped(IOCallback, this, null));
            }
            else
            {
                _dataChunks = new HttpApiTypes.HTTP_DATA_CHUNK[chunked ? 3 : 1];

                object[] objectsToPin = new object[_dataChunks.Length];
                objectsToPin[_dataChunks.Length - 1] = _dataChunks;

                var chunkHeaderBuffer = new ArraySegment <byte>();
                if (chunked)
                {
                    chunkHeaderBuffer                      = Helpers.GetChunkHeader(count);
                    _dataChunks[0].DataChunkType           = HttpApiTypes.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
                    _dataChunks[0].fromMemory.BufferLength = (uint)chunkHeaderBuffer.Count;
                    objectsToPin[0] = chunkHeaderBuffer.Array !;

                    _dataChunks[1].DataChunkType       = HttpApiTypes.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromFileHandle;
                    _dataChunks[1].fromFile.offset     = (ulong)offset;
                    _dataChunks[1].fromFile.count      = (ulong)count;
                    _dataChunks[1].fromFile.fileHandle = _fileStream.SafeFileHandle.DangerousGetHandle();
                    // Nothing to pin for the file handle.

                    _dataChunks[2].DataChunkType           = HttpApiTypes.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
                    _dataChunks[2].fromMemory.BufferLength = (uint)Helpers.CRLF.Length;
                    objectsToPin[1] = Helpers.CRLF;
                }
                else
                {
                    _dataChunks[0].DataChunkType       = HttpApiTypes.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromFileHandle;
                    _dataChunks[0].fromFile.offset     = (ulong)offset;
                    _dataChunks[0].fromFile.count      = (ulong)count;
                    _dataChunks[0].fromFile.fileHandle = _fileStream.SafeFileHandle.DangerousGetHandle();
                }

                // This call will pin needed memory
                _overlapped = new SafeNativeOverlapped(boundHandle,
                                                       boundHandle.AllocateNativeOverlapped(IOCallback, this, objectsToPin));

                if (chunked)
                {
                    // These must be set after pinning with Overlapped.
                    _dataChunks[0].fromMemory.pBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(chunkHeaderBuffer.Array !, chunkHeaderBuffer.Offset);
                    _dataChunks[2].fromMemory.pBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(Helpers.CRLF, 0);
                }
            }
        }
Esempio n. 28
0
        public void Close()
        {
            m_stream?.Dispose();
            m_stream = null;

            m_indexBlock = null;
            m_blockCache = null;
        }
Esempio n. 29
0
        /// <summary>
        /// Constructor
        /// </summary>
        public CsvWriter(
            string?fileName,
            CsvConfig csvConfig,
            int estimatedLineLenght,
            FileStream?existingFileStream = null,
            int flushDelay = 200)
        {
            if (!string.IsNullOrEmpty(fileName) && existingFileStream != null)
            {
                throw new Exception("CsvWriter constructor: There was neither an existingFileStream nor a fileName provided.");
            }

            if (existingFileStream != null)
            {
                FileName = existingFileStream.Name;
            }
            else
            {
                FileName = fileName !;
            }
            CsvConfig = csvConfig;
            if (csvConfig.Encoding != Encoding.UTF8)
            {
                throw new Exception($"CsvWriter constructor: Only reading from UTF8 files is supported, but the Encoding was " +
                                    $"{csvConfig.Encoding.EncodingName} for file {fileName}.");
            }

            delimiter         = (byte)csvConfig.Delimiter;
            MaxLineByteLenght = CsvConfig.BufferSize / Csv.ByteBufferToReserveRatio;
            if (estimatedLineLenght * Csv.Utf8BytesPerChar > MaxLineByteLenght)
            {
                throw new Exception($"CsvWriter constructor: BufferSize {CsvConfig.BufferSize} should be at least " +
                                    $"{Csv.ByteBufferToReserveRatio*Csv.Utf8BytesPerChar} times bigger than the estimated length needed of " +
                                    $"{estimatedLineLenght} for file {fileName}.");
            }

            EstimatedLineLenght = estimatedLineLenght;
            tempChars           = new char[50]; //tempChars is only used for formating decimals, which needs maybe 10-30 chars
            FlushDelay          = flushDelay;
            if (existingFileStream is null)
            {
                isFileStreamOwner = true;
                //fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, CsvConfig.BufferSize, FileOptions.SequentialScan | FileOptions.WriteThrough);
                fileStream = new FileStream(fileName !, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, CsvConfig.BufferSize);
            }
            else
            {
                isFileStreamOwner = false;
                fileStream        = existingFileStream;
            }
            //byteArray = new byte[csvConfig.BufferSize];
            //writePos = 0;
            //maxBufferWriteLength = CsvConfig.BufferSize - maxLineLenght;
            byteArray            = new byte[csvConfig.BufferSize + MaxLineByteLenght];
            writePos             = 0;
            maxBufferWriteLength = CsvConfig.BufferSize;
            flushTimer           = new Timer(flushTimerMethod, null, Timeout.Infinite, Timeout.Infinite);
        }
Esempio n. 30
0
 static void CreateFile(ref FileStream?stream, ref BinaryWriter?writer, string filename)
 {
     if (stream is not null || writer is not null)
     {
         throw new InvalidOperationException();
     }
     stream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Delete);
     writer = new BinaryWriter(stream);
 }