Ejemplo n.º 1
0
 CharacterDevice(IByteDevice backend, Text.Encoding encoding)
 {
     this.backend  = backend;
     this.encoding = encoding;
     this.peeked   = new PeekBuffer(async() =>
     {
         // TODO: Could we use this.encoder.Encode(Func<byte?>)?
         char?result = null;
         var next    = await this.backend.Read();
         if (next.HasValue)
         {
             var buffer = new byte[this.encoding.GetSymbolLength(next.Value)];
             buffer[0]  = next.Value;
             for (var i = 1; i < buffer.Length && (next = await this.backend.Read()).HasValue; i++)
             {
                 buffer[i] = next.Value;
             }
             var r = this.encoding.Decode(buffer);
             if (r.Length > 0)
             {
                 result = r[0];
             }
         }
         return(result);
     });
 }
Ejemplo n.º 2
0
 public static ICharacterDevice Wrap(IByteDevice backend, Text.Encoding encoding)
 {
     return(backend.NotNull() ? new CharacterDevice(backend, encoding)
     {
         dontClose = true
     } : null);
 }
Ejemplo n.º 3
0
		protected override void Dispose(bool disposing)
		{
			if (this.backend.NotNull())
			{
				this.backend.Dispose();
				this.backend = null;
			}
			base.Dispose(disposing);
		}
Ejemplo n.º 4
0
		public virtual bool Close() 
		{
			bool result;
			if (result = this.backend.NotNull())
			{
				this.backend.Close();
				this.backend = null;
			}
			return result;
		}
Ejemplo n.º 5
0
        public virtual async Tasks.Task <bool> Close()
        {
            bool result;

            if (result = this.backend.NotNull() && (this.dontClose || await this.backend.Close()))
            {
                this.backend = null;
            }
            return(result);
        }
Ejemplo n.º 6
0
        public static IByteDevice Create(Uri.Locator resource)
        {
            IByteDevice result = ByteDevice.Open(resource, System.IO.FileMode.Create);

            if (result.IsNull() && resource.NotNull())
            {
                System.IO.Directory.CreateDirectory(resource.Path.FolderPath.PlatformPath);
                result = ByteDevice.Open(resource, System.IO.FileMode.Create);
            }
            return(result);
        }
Ejemplo n.º 7
0
        static IByteDevice Open(Uri.Locator resource, System.IO.FileMode mode)
        {
            IByteDevice result = null;

            if (resource.NotNull())
            {
                switch (resource.Scheme)
                {
                case "assembly":
                    result = resource.Authority == "" ? ByteDevice.Open(System.Reflection.Assembly.GetEntryAssembly(), resource.Path) : ByteDevice.Open(System.Reflection.Assembly.Load(new System.Reflection.AssemblyName(resource.Authority)), resource.Path);
                    break;

                case "file":
                    try
                    {
                        System.IO.FileStream stream = System.IO.File.Open(System.IO.Path.GetFullPath(resource.PlatformPath), mode, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
                        if (stream.NotNull())
                        {
                            result = new ByteDevice(stream, resource);
                        }
                    }
                    catch (System.IO.DirectoryNotFoundException)
                    {
                        result = null;
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        result = null;
                    }
                    break;

                case "http":
                case "https":
                    if (mode == System.IO.FileMode.Open)
                    {
                        // TODO: support http and https.
                    }
                    break;
                }
            }
            return(result);
        }
Ejemplo n.º 8
0
 public static ICharacterDevice Open(IByteDevice backend, Text.Encoding encoding)
 {
     return(backend.NotNull() ? new CharacterDevice(backend, encoding) : null);
 }
Ejemplo n.º 9
0
		public static ByteStream Wrap(IByteDevice device)
		{
			return device.NotNull() ? new ByteStream(device) { CatchClose = true } : null;
		}
Ejemplo n.º 10
0
		public static new IByteDevice Open(IByteDevice device)
		{
			return device.NotNull() ? new BufferingByteDevice(device) : null;
		}
Ejemplo n.º 11
0
		public Server(IByteDevice backend)
		{
			this.backend = backend;
			this.AutoFlush = true;
		}
Ejemplo n.º 12
0
		public ByteDevice(IByteDevice backend, Action<byte> onRead, Action<byte> onWrite) :
			this(backend)
		{
			this.OnRead = onRead;
			this.OnWrite = onWrite;
		}
Ejemplo n.º 13
0
		public static ByteDevice Open(IByteDevice backend, Action<byte> onRead, Action<byte> onWrite)
		{
			ByteDevice result = ByteDevice.Open(backend);
			if (result.NotNull())
			{
				result.OnRead += onRead;
				result.OnWrite += onWrite;
			}
			return result;
		}
Ejemplo n.º 14
0
		public static ByteDevice Open(IByteDevice backend)
		{
			return backend.NotNull() ? new ByteDevice(backend) : null;
		}
Ejemplo n.º 15
0
 public static ICharacterDevice Wrap(IByteDevice backend)
 {
     return(CharacterDevice.Wrap(backend, Text.Encoding.Utf8));
 }
Ejemplo n.º 16
0
		public bool Close()
		{
			bool result;
			if (result = this.backend.NotNull() && this.backend.Close())
				this.backend = null;
			return result;
		}
Ejemplo n.º 17
0
		public ByteDevice(IByteDevice backend)
		{
			this.backend = backend;
		}
Ejemplo n.º 18
0
		BufferingByteDevice(IByteDevice backend) :
			base(backend)
		{ }
Ejemplo n.º 19
0
		public static ICharacterDevice Open(IByteDevice backend, System.Text.Encoding encoding)
		{
			return backend.NotNull() ? new CharacterDevice(backend, encoding) : null;
		}
Ejemplo n.º 20
0
		protected ByteDevice(IByteDevice backend)
		{
			this.backend = backend;
		}
Ejemplo n.º 21
0
		public static ICharacterDevice Wrap(IByteDevice backend)
		{
			return CharacterDevice.Wrap(backend, System.Text.Encoding.UTF8);
		}
Ejemplo n.º 22
0
		public static IByteDevice Open(IByteDevice device)
		{
			return device.NotNull() ? new ByteDevice(device) : null;
		}
Ejemplo n.º 23
0
		public static ICharacterDevice Wrap(IByteDevice backend, System.Text.Encoding encoding)
		{
			return backend.NotNull() ? new CharacterDevice(backend, encoding) { Wrapped = true } : null;
		}
Ejemplo n.º 24
0
		ByteStream(IByteDevice backend)
		{
			this.backend = backend;
		}
Ejemplo n.º 25
0
		CharacterDevice(IByteDevice backend, System.Text.Encoding encoding)
		{
			this.backend = backend;
			this.decoder = new Decoder(this.backend, encoding);
			this.encoding = encoding;
		}
Ejemplo n.º 26
0
		public static ByteStream Open(IByteDevice device)
		{
			return device.NotNull() ? new ByteStream(device) : null;
		}
Ejemplo n.º 27
0
		public virtual bool Close()
		{
			bool result;
			if (result = this.backend.NotNull() && (this.Wrapped || this.backend.Close() && this.decoder.Close()))
			{
				this.backend = null;
				this.decoder = null;
			}
			return result;
		}