Mono Class Library: Mono.Unix Namespace

Mono.Unix.UnixStream Class

A System.IO.Stream wrapper over Unix file descriptors. [Edit]

See Also: UnixStream Members

System.IO.Stream
     Mono.Unix.UnixStream

public sealed class UnixStream : System.IO.Stream

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Remarks

Mono.Unix.UnixStream is used for reading from and writing to Unix file descriptors.

Mono.Unix.UnixStream objects may support random access to files using the System.IO.Stream.Seek(long, System.IO.SeekOrigin) method (it depends on whether the underlying file descriptor supports seeking), and the System.IO.Stream.CanSeek properties of Mono.Unix.UnixStream instances encapsulating file descriptors are set depending on the capabilities of the underlying file descriptor. The System.IO.Stream.Seek(long, System.IO.SeekOrigin) method allows the read/write position to be moved to any position within the file. This is done with byte offset reference point parameters. The byte offset is relative to the seek reference point, which can be the beginning, the current position, or the end of the underlying file, as represented by the three values of the System.IO.SeekOrigin enumeration.

If a Mono.Unix.StdioFileStream encapsulates a device that does not support seeking, its System.IO.FileStream.CanSeek property is false.

Note: For additional information, see System.IO.Stream.CanSeek.

The following example demonstrates the use of a Mono.Unix.UnixStream object.

C# Example
using System;
using System.IO;
using Mono.Unix;
using Mono.Unix.Native;

class Directory {
   public static void Main(string[] args) { 
      int fd = Syscall.open ("log.txt",
         OpenFlags.O_WRONLY | OpenFlags.O_CREAT | 
         OpenFlags.O_APPEND | OpenFlags.O_LARGEFILE);
      UnixStream fs = new UnixStream (fd);
      StreamWriter w = new StreamWriter(fs);         

      Log ("Test1", w);
      Log ("Test2", w);
 
      w.Close(); // Close the writer and underlying file.     

      fd = Syscall.open ("log.txt", 
         OpenFlags.O_RDONLY | OpenFlags.O_LARGEFILE);
      fs = new UnixStream(fd);

      StreamReader r = new StreamReader(fs);        
      r.BaseStream.Seek(0, SeekOrigin.Begin);   
      DumpLog (r);
   }

   public static void Log (string logMessage, StreamWriter w) {
      w.Write("Log Entry : ");
      w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
      w.WriteLine(":");
      w.WriteLine(":{0}", logMessage);
      w.WriteLine ("-------------------------------");
      w.Flush();  
   }

   public static void DumpLog (StreamReader r) {
      while (r.Peek() > -1) { // While not at the end of the file, write to standard output.     
         Console.WriteLine(r.ReadLine());
      }

      r.Close();
   }
}

Some example output is

Log Entry : 9:26:21 AM Friday, July 06, 2001
:
:Test1
-------------------------------
Log Entry : 9:26:21 AM Friday, July 06, 2001
:
:Test2
-------------------------------
[Edit]

Requirements

Namespace: Mono.Unix
Assembly: Mono.Posix (in Mono.Posix.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0