The Labs \ Source Viewer \ SSCLI \ System.IO \ UnmanagedMemoryStreamWrapper

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 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. ** Class:  UnmanagedMemoryStreamWrapper
  18. **
  19. ** Purpose: Create a Memorystream over an UnmanagedMemoryStream
  20. **
  21. ===========================================================*/
  22. using System;
  23. using System.Runtime.InteropServices;
  24. using System.Security.Permissions;
  25. namespace System.IO
  26. {
  27.     // Needed for backwards compatibility with V1.x usages of the
  28.     // ResourceManager, where a MemoryStream is now returned as an
  29.     // UnmanagedMemoryStream from ResourceReader.
  30.     internal sealed class UnmanagedMemoryStreamWrapper : MemoryStream
  31.     {
  32.         private UnmanagedMemoryStream _unmanagedStream;
  33.        
  34.         internal UnmanagedMemoryStreamWrapper(UnmanagedMemoryStream stream)
  35.         {
  36.             _unmanagedStream = stream;
  37.         }
  38.        
  39.         public override bool CanRead {
  40.             get { return _unmanagedStream.CanRead; }
  41.         }
  42.        
  43.         public override bool CanSeek {
  44.             get { return _unmanagedStream.CanSeek; }
  45.         }
  46.        
  47.         public override bool CanWrite {
  48.             get { return _unmanagedStream.CanWrite; }
  49.         }
  50.        
  51.         protected override void Dispose(bool disposing)
  52.         {
  53.             try {
  54.                 if (disposing)
  55.                     _unmanagedStream.Close();
  56.             }
  57.             finally {
  58.                 base.Dispose(disposing);
  59.             }
  60.         }
  61.        
  62.         public override void Flush()
  63.         {
  64.             _unmanagedStream.Flush();
  65.         }
  66.        
  67.         public override byte[] GetBuffer()
  68.         {
  69.             throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_MemStreamBuffer"));
  70.         }
  71.        
  72.         public override int Capacity {
  73.             get { return (int)_unmanagedStream.Capacity; }
  74.             set {
  75.                 throw new IOException(Environment.GetResourceString("IO.IO_FixedCapacity"));
  76.             }
  77.         }
  78.        
  79.         public override long Length {
  80.             get { return _unmanagedStream.Length; }
  81.         }
  82.        
  83.         public override long Position {
  84.             get { return _unmanagedStream.Position; }
  85.             set { _unmanagedStream.Position = value; }
  86.         }
  87.        
  88.         public override int Read(        [In(), Out()]
  89. byte[] buffer, int offset, int count)
  90.         {
  91.             return _unmanagedStream.Read(buffer, offset, count);
  92.         }
  93.        
  94.         public override int ReadByte()
  95.         {
  96.             return _unmanagedStream.ReadByte();
  97.         }
  98.        
  99.        
  100.         public override long Seek(long offset, SeekOrigin loc)
  101.         {
  102.             return _unmanagedStream.Seek(offset, loc);
  103.         }
  104.        
  105.         unsafe public override byte[] ToArray()
  106.         {
  107.             if (!_unmanagedStream._isOpen)
  108.                 __Error.StreamIsClosed();
  109.             if (!_unmanagedStream.CanRead)
  110.                 __Error.ReadNotSupported();
  111.            
  112.             byte[] buffer = new byte[_unmanagedStream.Length];
  113.             Buffer.memcpy(_unmanagedStream.Pointer, 0, buffer, 0, (int)_unmanagedStream.Length);
  114.             return buffer;
  115.         }
  116.        
  117.         public override void Write(byte[] buffer, int offset, int count)
  118.         {
  119.             _unmanagedStream.Write(buffer, offset, count);
  120.         }
  121.        
  122.         public override void WriteByte(byte value)
  123.         {
  124.             _unmanagedStream.WriteByte(value);
  125.         }
  126.        
  127.         // Writes this MemoryStream to another stream.
  128.         unsafe public override void WriteTo(Stream stream)
  129.         {
  130.             if (!_unmanagedStream._isOpen)
  131.                 __Error.StreamIsClosed();
  132.             if (!_unmanagedStream.CanRead)
  133.                 __Error.ReadNotSupported();
  134.             if (stream == null)
  135.                 throw new ArgumentNullException("stream", Environment.GetResourceString("ArgumentNull_Stream"));
  136.            
  137.             byte[] buffer = ToArray();
  138.            
  139.             stream.Write(buffer, 0, buffer.Length);
  140.         }
  141.     }
  142. }

Developer Fusion