protected override void Dispose(bool disposing) { if (IsOpen) { IOPrimitives.CloseFile(fileHandle); fileHandle = null; } }
/// <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."); } }
/// <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); }
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); }
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); }
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"); }
/// <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)); }
/// <inheritdoc/> public override void Flush() { EnsureOpen(); IOPrimitives.FlushFile(fileHandle); }