writehtml方法中使用到的一个类

来源:互联网 发布:字符型数组java 编辑:程序博客网 时间:2024/05/29 18:46

/********************************
 * 微软自身的接口声明
 * 由于找不到可用的Interface声明
 * 只好祭出Reflector,反编译Windows.Forms
 * 需要的朋友可以不用辛苦的自己搞了
 * ******************************/
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;

namespace HtmlEditor {


  [StructLayout(LayoutKind.Sequential)]
  public class STATSTG {
    [MarshalAs(UnmanagedType.LPWStr)]
    public string pwcsName;
    public int type;
    [MarshalAs(UnmanagedType.I8)]
    public long cbSize;
    [MarshalAs(UnmanagedType.I8)]
    public long mtime;
    [MarshalAs(UnmanagedType.I8)]
    public long ctime;
    [MarshalAs(UnmanagedType.I8)]
    public long atime;
    [MarshalAs(UnmanagedType.I4)]
    public int grfMode;
    [MarshalAs(UnmanagedType.I4)]
    public int grfLocksSupported;
    public int clsid_data1;
    [MarshalAs(UnmanagedType.I2)]
    public short clsid_data2;
    [MarshalAs(UnmanagedType.I2)]
    public short clsid_data3;
    [MarshalAs(UnmanagedType.U1)]
    public byte clsid_b0;
    [MarshalAs(UnmanagedType.U1)]
    public byte clsid_b1;
    [MarshalAs(UnmanagedType.U1)]
    public byte clsid_b2;
    [MarshalAs(UnmanagedType.U1)]
    public byte clsid_b3;
    [MarshalAs(UnmanagedType.U1)]
    public byte clsid_b4;
    [MarshalAs(UnmanagedType.U1)]
    public byte clsid_b5;
    [MarshalAs(UnmanagedType.U1)]
    public byte clsid_b6;
    [MarshalAs(UnmanagedType.U1)]
    public byte clsid_b7;
    [MarshalAs(UnmanagedType.I4)]
    public int grfStateBits;
    [MarshalAs(UnmanagedType.I4)]
    public int reserved;
    public STATSTG() {
    }
  }
  [SuppressUnmanagedCodeSecurity]
  internal class UnsafeNativeMethods {
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713")]
      public interface IPersistStreamInit {
      void GetClassID(out Guid pClassID);
      [PreserveSig]
      int IsDirty();
      void Load([In, MarshalAs(UnmanagedType.Interface)] IStream pstm);
      void Save([In, MarshalAs(UnmanagedType.Interface)] IStream pstm, [In, MarshalAs(UnmanagedType.Bool)] bool fClearDirty);
      void GetSizeMax([Out, MarshalAs(UnmanagedType.LPArray)] long pcbSize);
      void InitNew();
    }
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0000000C-0000-0000-C000-000000000046")]
      public interface IStream {
      int Read(IntPtr buf, int len);
      int Write(IntPtr buf, int len);
      [return: MarshalAs(UnmanagedType.I8)]
      long Seek([In, MarshalAs(UnmanagedType.I8)] long dlibMove, int dwOrigin);
      void SetSize([In, MarshalAs(UnmanagedType.I8)] long libNewSize);
      [return: MarshalAs(UnmanagedType.I8)]
      long CopyTo([In, MarshalAs(UnmanagedType.Interface)] IStream pstm, [In, MarshalAs(UnmanagedType.I8)] long cb, [Out, MarshalAs(UnmanagedType.LPArray)] long[] pcbRead);
      void Commit(int grfCommitFlags);
      void Revert();
      void LockRegion([In, MarshalAs(UnmanagedType.I8)] long libOffset, [In, MarshalAs(UnmanagedType.I8)] long cb, int dwLockType);
      void UnlockRegion([In, MarshalAs(UnmanagedType.I8)] long libOffset, [In, MarshalAs(UnmanagedType.I8)] long cb, int dwLockType);
      void Stat([Out] STATSTG pStatstg, int grfStatFlag);
      [return: MarshalAs(UnmanagedType.Interface)]
      IStream Clone();
    }

    // Methods
    public UnsafeNativeMethods() {
    }

    public class ComStreamFromDataStream : IStream {
      // Methods
      protected ComStreamFromDataStream() {
        this.virtualPosition = -1;
      }

      public ComStreamFromDataStream(Stream dataStream) {
        this.virtualPosition = -1;
        if (dataStream == null) {
          throw new ArgumentNullException("dataStream");
        }
        this.dataStream = dataStream;
      }

      private void ActualizeVirtualPosition() {
        if (this.virtualPosition != -1) {
          if (this.virtualPosition > this.dataStream.Length) {
            this.dataStream.SetLength(this.virtualPosition);
          }
          this.dataStream.Position = this.virtualPosition;
          this.virtualPosition = -1;
        }
      }

      public IStream Clone() {
        ComStreamFromDataStream.NotImplemented();
        return null;
      }

      public void Commit(int grfCommitFlags) {
        this.dataStream.Flush();
        this.ActualizeVirtualPosition();
      }

      public long CopyTo(IStream pstm, long cb, long[] pcbRead) {
        int num1 = 0x1000;
        IntPtr ptr1 = Marshal.AllocHGlobal(num1);
        if (ptr1 == IntPtr.Zero) {
          throw new OutOfMemoryException();
        }
        long num2 = 0;
        try {
          while (num2 < cb) {
            int num3 = num1;
            if ((num2 + num3) > cb) {
              num3 = (int) (cb - num2);
            }
            int num4 = this.Read(ptr1, num3);
            if (num4 == 0) {
              goto Label_006C;
            }
            if (pstm.Write(ptr1, num4) != num4) {
              throw ComStreamFromDataStream.EFail("Wrote an incorrect number of bytes");
            }
            num2 += num4;
          }
        }
        finally {
          Marshal.FreeHGlobal(ptr1);
        }
        Label_006C:
          if ((pcbRead != null) && (pcbRead.Length > 0)) {
            pcbRead[0] = num2;
          }
        return num2;
      }

      protected static ExternalException EFail(string msg) {
        ExternalException exception1 = new ExternalException(msg, -2147467259);
        throw exception1;
      }

      public Stream GetDataStream() {
        return this.dataStream;
      }

      public void LockRegion(long libOffset, long cb, int dwLockType) {
      }

      protected static void NotImplemented() {
        ExternalException exception1 = new ExternalException("Not implemented.", -2147467263);
        throw exception1;
      }

      public int Read(IntPtr buf, int length) {
        byte[] buffer1 = new byte[length];
        int num1 = this.Read(buffer1, length);
        Marshal.Copy(buffer1, 0, buf, length);
        return num1;
      }

      public int Read(byte[] buffer, int length) {
        this.ActualizeVirtualPosition();
        return this.dataStream.Read(buffer, 0, length);
      }

      public void Revert() {
        ComStreamFromDataStream.NotImplemented();
      }

      public long Seek(long offset, int origin) {
        long num1 = this.virtualPosition;
        if (this.virtualPosition == -1) {
          num1 = this.dataStream.Position;
        }
        long num2 = this.dataStream.Length;
        switch (origin) {
          case 0: {
            if (offset > num2) {
              this.virtualPosition = offset;
              break;
            }
            this.dataStream.Position = offset;
            this.virtualPosition = -1;
            break;
          }
          case 1: {
            if ((offset + num1) > num2) {
              this.virtualPosition = offset + num1;
              break;
            }
            this.dataStream.Position = num1 + offset;
            this.virtualPosition = -1;
            break;
          }
          case 2: {
            if (offset > 0) {
              this.virtualPosition = num2 + offset;
              break;
            }
            this.dataStream.Position = num2 + offset;
            this.virtualPosition = -1;
            break;
          }
        }
        if (this.virtualPosition != -1) {
          return this.virtualPosition;
        }
        return this.dataStream.Position;
      }

      public void SetSize(long value) {
        this.dataStream.SetLength(value);
      }

      public void Stat(STATSTG pstatstg, int grfStatFlag) {
        pstatstg.type = 2;
        pstatstg.cbSize = this.dataStream.Length;
        pstatstg.grfLocksSupported = 2;
      }

      public void UnlockRegion(long libOffset, long cb, int dwLockType) {
      }

      public int Write(IntPtr buf, int length) {
        byte[] buffer1 = new byte[length];
        Marshal.Copy(buf, buffer1, 0, length);
        return this.Write(buffer1, length);
      }

      public int Write(byte[] buffer, int length) {
        this.ActualizeVirtualPosition();
        this.dataStream.Write(buffer, 0, length);
        return length;
      }


      // Fields
      protected Stream dataStream;
      private long virtualPosition;
    }
  }
}

 

原创粉丝点击