File: TestAsyncStream.cs

package info (click to toggle)
gtk-sharp2 2.8.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 21,208 kB
  • ctags: 5,843
  • sloc: xml: 245,420; cs: 24,183; sh: 8,547; ansic: 2,739; makefile: 1,582; perl: 1,177
file content (54 lines) | stat: -rw-r--r-- 1,435 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using GLib;
using Gnome.Vfs;
using System;
using System.IO;
using System.Text;

namespace TestGnomeVfs {
	public class TestAsyncStream {
		private static MainLoop loop;
		private static byte[] buffer;
	
		static void Main (string[] args)
		{
			if (args.Length != 1) {
				Console.WriteLine ("Usage: TestAsyncStream <uri>");
				return;
			}
		
			Gnome.Vfs.Vfs.Initialize ();

			VfsStream stream = new VfsStream (args[0], FileMode.Open, true);
			
			UTF8Encoding utf8 = new UTF8Encoding ();
			buffer = new byte[1024];
			int read;
			while ((read = stream.Read (buffer, 0, buffer.Length)) != 0) {
				Console.WriteLine ("read ({0} bytes) : '{1}'",
						   read, utf8.GetString (buffer, 0, read));
			}

			long offset = stream.Seek (0, SeekOrigin.Begin);
			Console.WriteLine ("Offset after seek is {0}", offset);
			
			buffer = new byte[1024];
			IAsyncResult result = stream.BeginRead (buffer, 0, buffer.Length,
								new System.AsyncCallback (OnReadComplete),
								stream);

			loop = new MainLoop ();
			loop.Run ();

			Gnome.Vfs.Vfs.Shutdown ();
		}
		
		private static void OnReadComplete (IAsyncResult result)
		{
			VfsStreamAsyncResult asyncResult = result as VfsStreamAsyncResult;
			Console.WriteLine ("Read completed: {0}, {1}", asyncResult.IsCompleted, asyncResult.NBytes);
			UTF8Encoding utf8 = new UTF8Encoding ();
			Console.WriteLine (utf8.GetString (buffer, 0, buffer.Length));
			loop.Quit ();
		}
	}
}