StreamHelper.cs

来源:互联网 发布:windows截图保存路径 编辑:程序博客网 时间:2024/04/30 06:41
  1. // ==++==
  2. // 
  3. //   
  4. //    Copyright (c) 2002 Microsoft Corporation.  All rights reserved.
  5. //   
  6. //    The use and distribution terms for this software are contained in the file
  7. //    named license.txt, which can be found in the root of this distribution.
  8. //    By using this software in any fashion, you are agreeing to be bound by the
  9. //    terms of this license.
  10. //   
  11. //    You must not remove this notice, or any other, from this software.
  12. //   
  13. // 
  14. // ==--==
  15. //============================================================
  16. //
  17. // File:    StreamHelper.cs
  18. //
  19. // Summary: Helper methods for streams.
  20. //
  21. //===========================================================
  22. using System;
  23. using System.IO;
  24. using System.Runtime.Remoting;
  25. namespace System.Runtime.Remoting.Channels
  26. {
  27.     internal class StreamHelper
  28.     {
  29.         internal static void CopyStream(Stream source, Stream target)
  30.         {
  31.             if (source == null)
  32.                 return;
  33.             // see if this is a ChunkedMemoryStream (we can do a direct write)
  34.             ChunkedMemoryStream chunkedMemStream = source as ChunkedMemoryStream;
  35.             if (chunkedMemStream != null)
  36.             {
  37.                 chunkedMemStream.WriteTo(target);
  38.             }
  39.             else
  40.             {
  41.                 // see if this is a MemoryStream (we can do a direct write)
  42.                 MemoryStream memContentStream = source as MemoryStream;
  43.                 if (memContentStream != null)
  44.                 {
  45.                     memContentStream.WriteTo(target);
  46.                 }
  47.                 else                    
  48.                 {
  49.                     // otherwise, we need to copy the data through an intermediate buffer
  50.                 
  51.                     byte[] buffer = CoreChannel.BufferPool.GetBuffer();
  52.                     int bufferSize = buffer.Length;
  53.                     int readCount = source.Read(buffer, 0, bufferSize);
  54.                     while (readCount > 0)
  55.                     {
  56.                         target.Write(buffer, 0, readCount);
  57.                         readCount = source.Read(buffer, 0, bufferSize);
  58.                     }   
  59.                     CoreChannel.BufferPool.ReturnBuffer(buffer);
  60.                 }
  61.             }
  62.             
  63.         } // CopyStream
  64.         internal static void BufferCopy(byte[] source, int srcOffset, 
  65.                                         byte[] dest, int destOffset,
  66.                                         int count)
  67.         {
  68.             if (count > 8)
  69.             {
  70.                 Buffer.BlockCopy(source, srcOffset, dest, destOffset, count);
  71.             }
  72.             else
  73.             {
  74.                 for (int co = 0; co < count; co++)
  75.                     dest[destOffset + co] = source[srcOffset + co];
  76.             }
  77.         } // BufferCopy
  78.         
  79.     } // StreamHelper
  80. // namespace System.Runtime.Remoting.Channels
 
原创粉丝点击