Example #1
0
 protected override void Dispose(bool disposing)
 {
     if (IsOpen)
     {
         IOPrimitives.CloseFile(fileHandle);
         fileHandle = null;
     }
 }
Example #2
0
        /// <inheritdoc/>
        public override void Write(byte[] buffer, int offset, int count)
        {
            CheckReadArgs(buffer, offset, count);
            EnsureWriteable();
            ulong bytesWritten = IOPrimitives.WriteToFile(&buffer[offset], (ulong)count, fileHandle);

            if (bytesWritten < (ulong)count)
            {
                throw new IOException("Wrote only " + bytesWritten + " of " + count + " bytes.");
            }
        }
Example #3
0
        /// <inheritdoc/>
        public override long Seek(long offset, SeekOrigin origin)
        {
            EnsureOpen();
            var error = IOPrimitives.FileSeek(fileHandle, offset, (int)origin);

            if (error != 0)
            {
                throw new IOException(
                          "Cannot seek to position at offset '" + offset +
                          "' from the " + SeekOriginToString(origin) +
                          ". Error code: '" + error + "'.");
            }
            return(Position);
        }
Example #4
0
        private static void *OpenFile(string name, string mode)
        {
            IntPtr nameCStr = Marshal.StringToHGlobalAnsi(name);
            IntPtr modeCStr = Marshal.StringToHGlobalAnsi(mode);

            void *handle = IOPrimitives.OpenFile(
                (byte *)nameCStr.ToPointer(),
                (byte *)modeCStr.ToPointer());

            Marshal.FreeHGlobal(modeCStr);
            Marshal.FreeHGlobal(nameCStr);

            return(handle);
        }
Example #5
0
        public static void HandleFatalException(Exception ex)
        {
            // We can't use the Console class here (because it is
            // defined in a library that depends on this one).
            // Instead, we'll use IO primitives, which have the
            // added advantage of being leaner.
            var errorMessage = Marshal.StringToHGlobalAnsi(
                "error: a fatal exception was thrown." + NewLine +
                "Exception message: " + ex.Message + NewLine);

            IOPrimitives.WriteCStringToFile(
                (byte *)errorMessage.ToPointer(),
                IOPrimitives.StandardErrorFile);

            Marshal.FreeHGlobal(errorMessage);
        }
Example #6
0
 public void WhenConvertingTextToWindowsLineBreaks()
 {
     IOPrimitives.convertTextToWindowsLineBreaks("\rMy text\n\ris from different os versions\r\nbut thats ok\treally")
     .ShouldEqual("\r\nMy text\r\n\r\nis from different os versions\r\nbut thats ok\treally");
 }
Example #7
0
 /// <inheritdoc/>
 public override int Read(byte[] buffer, int offset, int count)
 {
     CheckReadArgs(buffer, offset, count);
     EnsureReadable();
     return((int)IOPrimitives.ReadFromFile(&buffer[offset], (ulong)count, fileHandle));
 }
Example #8
0
 /// <inheritdoc/>
 public override void Flush()
 {
     EnsureOpen();
     IOPrimitives.FlushFile(fileHandle);
 }