zoukankan      html  css  js  c++  java
  • 一个Stream的封装——WrapStream

    写代码的时候经常遇到要将stream里面的某些函数修改一下,方便打桩或提供新功能。但由于CLR里面提供的Stream大部分都是sealed类型的。这个时候我们往往通过新建一个Stream类来封装现有stream成新stream。

        class WrapStream : Stream
        {
            Stream stream;
            public WrapStream(Stream stream)
            {
                this.stream = stream;
            }

            public override bool CanRead { get { return stream.CanRead; } }

            public override bool CanSeek { get { return stream.CanSeek; } }

            public override bool CanWrite { get { return stream.CanWrite; } }

            public override long Length { get { return stream.Length; } }

            public override long Position
            {
                get { return stream.Position; }
                set { stream.Position = value; }
            }

            public override void Flush()
            {
                stream.Flush();
            }

            public override int Read(byte[] buffer, int offset, int count)
            {
                return stream.Read(buffer, offset, count);
            }

            public override long Seek(long offset, SeekOrigin origin)
            {
                return stream.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                stream.SetLength(value);
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                stream.Write(buffer, offset, count);
            }

            public override void Close()
            {
                stream.Close();
            }

            protected override void Dispose(bool disposing)
            {
                stream.Dispose();
            }
        }

    虽然本身这个实现并不难,但由于.net并没有内置这个wrapstream,每次都要实现有点烦,这里记录一下,以后再用的时候直接继承这个WrapStream,只重写需要特化的部分即可。另外,这个WrapStream可能也不够完善,后续有更正再补充。

     

  • 相关阅读:
    is quoted with ["] which must be escaped when used within the value
    QueryDSL与SpringDataJPA复杂查询
    遍历list,同时remove不符合条件的元素
    解决AnnotationTransactionAttributeSource is only available on Java 1.5 and highe
    Windows系统安装MySQL
    sqlyog导sql文件
    myeclipse导入maven项目
    Invalid 'log4jConfigLocation' parameter: class path resource [log4j.xml] cannot be resolved to URL because it does not exist
    Nginx SSL+tomcat集群,取不到https正确协议
    微信开发之通过代理调试本地项目
  • 原文地址:https://www.cnblogs.com/TianFang/p/2839240.html
Copyright © 2011-2022 走看看