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); }); }
public static ICharacterDevice Wrap(IByteDevice backend, Text.Encoding encoding) { return(backend.NotNull() ? new CharacterDevice(backend, encoding) { dontClose = true } : null); }
protected override void Dispose(bool disposing) { if (this.backend.NotNull()) { this.backend.Dispose(); this.backend = null; } base.Dispose(disposing); }
public virtual bool Close() { bool result; if (result = this.backend.NotNull()) { this.backend.Close(); this.backend = null; } return result; }
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); }
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); }
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); }
public static ICharacterDevice Open(IByteDevice backend, Text.Encoding encoding) { return(backend.NotNull() ? new CharacterDevice(backend, encoding) : null); }
public static ByteStream Wrap(IByteDevice device) { return device.NotNull() ? new ByteStream(device) { CatchClose = true } : null; }
public static new IByteDevice Open(IByteDevice device) { return device.NotNull() ? new BufferingByteDevice(device) : null; }
public Server(IByteDevice backend) { this.backend = backend; this.AutoFlush = true; }
public ByteDevice(IByteDevice backend, Action<byte> onRead, Action<byte> onWrite) : this(backend) { this.OnRead = onRead; this.OnWrite = onWrite; }
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; }
public static ByteDevice Open(IByteDevice backend) { return backend.NotNull() ? new ByteDevice(backend) : null; }
public static ICharacterDevice Wrap(IByteDevice backend) { return(CharacterDevice.Wrap(backend, Text.Encoding.Utf8)); }
public bool Close() { bool result; if (result = this.backend.NotNull() && this.backend.Close()) this.backend = null; return result; }
public ByteDevice(IByteDevice backend) { this.backend = backend; }
BufferingByteDevice(IByteDevice backend) : base(backend) { }
public static ICharacterDevice Open(IByteDevice backend, System.Text.Encoding encoding) { return backend.NotNull() ? new CharacterDevice(backend, encoding) : null; }
protected ByteDevice(IByteDevice backend) { this.backend = backend; }
public static ICharacterDevice Wrap(IByteDevice backend) { return CharacterDevice.Wrap(backend, System.Text.Encoding.UTF8); }
public static IByteDevice Open(IByteDevice device) { return device.NotNull() ? new ByteDevice(device) : null; }
public static ICharacterDevice Wrap(IByteDevice backend, System.Text.Encoding encoding) { return backend.NotNull() ? new CharacterDevice(backend, encoding) { Wrapped = true } : null; }
ByteStream(IByteDevice backend) { this.backend = backend; }
CharacterDevice(IByteDevice backend, System.Text.Encoding encoding) { this.backend = backend; this.decoder = new Decoder(this.backend, encoding); this.encoding = encoding; }
public static ByteStream Open(IByteDevice device) { return device.NotNull() ? new ByteStream(device) : null; }
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; }