public void write()
    {
      Assert.Throws<ArgumentNullException>(() => StreamExtensions.Write<Stream>(null, Enumerable.Empty<byte>().ToArray()));
      Assert.Throws<ArgumentNullException>(() => Stream.Null.Write((byte[])null));
      Assert.Throws<ArgumentNullException>(() => StreamExtensions.Write<Stream>(null, Stream.Null));
      Assert.Throws<ArgumentNullException>(() => Stream.Null.Write((Stream)null));
      Assert.Throws<ArgumentNullException>(() => StreamExtensions.Write<Stream>(null, "text"));
      Assert.Throws<ArgumentNullException>(() => Stream.Null.Write((string)null));

      var bytes = Guid.NewGuid().ToByteArray();
      var text = bytes.ToString();

      var stream = new MemoryStream();
      Assert.True(ReferenceEquals(stream.Write(bytes), stream));
      stream.Close();
      Assert.True(stream.ToArray().SequenceEqual(bytes));

      var from = new MemoryStream(bytes);
      var to = new MemoryStream();
      Assert.True(ReferenceEquals(to.Write(from), to));
      Assert.True(to.ToArray().SequenceEqual(bytes));
      Assert.Equal(0, from.Bytes().Length);
      Assert.True(from.CanRead);
      Assert.Equal(0, to.Bytes().Length);
      Assert.True(to.CanWrite);
      from.Close();
      to.Close();

      using (var s = new MemoryStream())
      {
        Assert.True(ReferenceEquals(s.Write(string.Empty), s));
        Assert.Equal(string.Empty, s.Text());
      }

      using (var s = new MemoryStream())
      {
        Assert.True(ReferenceEquals(s.Write(text), s));
        Assert.Equal(text, s.Rewind().Text());
      }

      using (var s = new MemoryStream())
      {
        Assert.True(ReferenceEquals(s.Write(text, Encoding.Unicode), s));
        Assert.Equal(text, s.Rewind().Text(encoding: Encoding.Unicode));
      }
    }
    public void as_xml_document()
    {
      Assert.Throws<ArgumentNullException>(() => XmlExtensions.AsXmlDocument(null));
      Assert.Throws<XmlException>(() => Stream.Null.AsXmlDocument());

      const string Xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><article>text</article>";

      using (var stream = new MemoryStream(Xml.Bytes(Encoding.UTF32)))
      {
        Assert.Throws<XmlException>(() => stream.AsXmlDocument());
      }

      using (var stream = new MemoryStream(Xml.Bytes(Encoding.Unicode)))
      {
        Assert.Equal(Xml, stream.AsXmlDocument().String());
        Assert.Equal(0, stream.Bytes().Length);
        Assert.Equal(-1, stream.ReadByte());
      }

      using (var stream = new MemoryStream(Xml.Bytes(Encoding.Unicode)))
      {
        Assert.Equal(Xml, stream.AsXmlDocument(true).String());
        Assert.Throws<ObjectDisposedException>(() => stream.ReadByte());
      }
    }
    public void bytes()
    {
      Assert.Throws<ArgumentNullException>(() => StreamExtensions.Bytes(null));

      Assert.Equal(0, Stream.Null.Bytes().Length);

      var bytes = Guid.NewGuid().ToByteArray();

      var stream = new MemoryStream(bytes);
      Assert.True(stream.Bytes().SequenceEqual(bytes));
      Assert.Equal(-1, stream.ReadByte());
      stream.Close();
      Assert.Throws<ObjectDisposedException>(() => stream.ReadByte());
      
      stream = new MemoryStream(bytes);
      Assert.True(stream.Bytes(true).SequenceEqual(bytes));
      Assert.Throws<ObjectDisposedException>(() => stream.ReadByte());
    }
    public void deflate()
    {
      Assert.Throws<ArgumentNullException>(() => StreamExtensions.Deflate(null, CompressionMode.Compress));
      Assert.Throws<ArgumentNullException>(() => StreamExtensions.Deflate<Stream>(null, Enumerable.Empty<byte>().ToArray()));
      Assert.Throws<ArgumentNullException>(() => Stream.Null.Deflate(null));
      Assert.Throws<ArgumentNullException>(() => StreamExtensions.Deflate(null));

      var bytes = Guid.NewGuid().ToByteArray();

      var stream = new MemoryStream();
      var compressed = new byte[] { };
      using (var deflate = stream.Deflate(CompressionMode.Compress))
      {
        Assert.True(ReferenceEquals(deflate.BaseStream, stream));
        Assert.Throws<InvalidOperationException>(() => deflate.ReadByte());
        deflate.Write(bytes);
      }
      compressed = stream.ToArray();
      Assert.False(compressed.SequenceEqual(bytes));
      Assert.Throws<ObjectDisposedException>(() => stream.ReadByte());

      stream = new MemoryStream(compressed);
      var decompressed = new byte[] { };
      using (var deflate = stream.Deflate(CompressionMode.Decompress))
      {
        Assert.True(ReferenceEquals(deflate.BaseStream, stream));
        decompressed = deflate.Bytes();
      }
      Assert.True(decompressed.SequenceEqual(bytes));
      Assert.Throws<ObjectDisposedException>(() => stream.ReadByte());

      using (var s = new MemoryStream())
      {
        Assert.True(ReferenceEquals(s.Deflate(bytes), s));
        Assert.True(s.ToArray().SequenceEqual(compressed));
        Assert.Equal(0, s.Bytes().Length);
        Assert.True(s.CanRead);
        Assert.True(s.CanWrite);
      }

      using (var s = new MemoryStream(compressed))
      {
        Assert.True(s.Deflate().SequenceEqual(bytes));
        Assert.Equal(0, s.Bytes().Length);
        Assert.True(s.CanRead);
        Assert.True(s.CanWrite);
      }

      Assert.True(new MemoryStream().Deflate(bytes).Rewind().Deflate().SequenceEqual(bytes));
    }
    public void xml_writer_stream()
    {
      Assert.Throws<ArgumentNullException>(() => ((Stream) null).XmlWriter());
      
      var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><article>text</article>";
      using (var stream = new MemoryStream())
      {
        using (var writer = stream.XmlWriter())
        {
          Assert.False(writer.Settings.CloseOutput);
          Assert.Equal(Encoding.UTF8.ToString(), writer.Settings.Encoding.ToString());
          Assert.False(writer.Settings.Indent);
          writer.WriteElementString("article", "text");
        }
        Assert.True(stream.ToArray().SequenceEqual(xml.Bytes(Encoding.UTF8)));
        Assert.Equal(0, stream.Bytes().Length);
        Assert.Equal(-1, stream.ReadByte());

        using (var writer = stream.Rewind().XmlWriter(true))
        {
          Assert.True(writer.Settings.CloseOutput);
          Assert.Equal(Encoding.UTF8.ToString(), writer.Settings.Encoding.ToString());
        }
        Assert.Throws<ObjectDisposedException>(() => stream.ReadByte());
      }

      xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><article>text</article>";
      using (var stream = new MemoryStream())
      {
        using (var writer = stream.XmlWriter(encoding: Encoding.Unicode))
        {
          Assert.False(writer.Settings.CloseOutput);
          Assert.Equal(Encoding.Unicode.ToString(), writer.Settings.Encoding.ToString());
          writer.WriteElementString("article", "text");
        }
        Assert.True(stream.ToArray().SequenceEqual(xml.Bytes(Encoding.Unicode)));
        Assert.Equal(0, stream.Bytes().Length);
        Assert.Equal(-1, stream.ReadByte());

        using (var writer = stream.XmlWriter(true, Encoding.Unicode))
        {
          Assert.True(writer.Settings.CloseOutput);
          Assert.Equal(Encoding.Unicode.ToString(), writer.Settings.Encoding.ToString());
        }
        Assert.Throws<ObjectDisposedException>(() => stream.ReadByte());
      }
    }
    public void xml_reader_stream()
    {
      Assert.Throws<ArgumentNullException>(() => ((Stream)(null)).XmlReader());

      const string Xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><article>text</article>";

      using (var stream = new MemoryStream(Xml.Bytes()))
      {
        var reader = stream.XmlReader();
        Assert.False(reader.Settings.CloseInput);
        Assert.True(reader.Settings.IgnoreComments);
        Assert.True(reader.Settings.IgnoreWhitespace);
        reader.ReadStartElement("article");
        Assert.Equal("text", reader.ReadString());
        reader.ReadEndElement();
        reader.Close();
        Assert.Equal(0, stream.Bytes().Length);
        Assert.Equal(-1, stream.ReadByte());
      }

      using (var stream = new MemoryStream(Xml.Bytes()))
      {
        stream.XmlReader(true).Close();
        //Assert.True(reader.Settings.CloseInput);
        Assert.Throws<ObjectDisposedException>(() => stream.ReadByte());
      }
    }