⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compressionlibrary.vb

📁 Microsoft Mobile Development Handbook的代码,有C#,VB,C++的
💻 VB
字号:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Web.Services.Protocols
Imports ICSharpCode.SharpZipLib.GZip

<AttributeUsage(AttributeTargets.Method)> _
    Public Class CompressionSoapExtensionAttribute
    Inherits SoapExtensionAttribute

    Private _priority As Integer

    '' <summary>
    '' Returns the type of the object that actually performs the logic associated
    '' with this SOAP extension
    '' </summary>
    Public Overrides ReadOnly Property ExtensionType() As Type
        Get
            Return GetType(CompressionSoapExtension)
        End Get
    End Property

    '' <summary>
    '' The Priority property indicates the order of processing when there are 
    '' several SOAP extensions applied simultaneously
    '' </summary>
    Public Overrides Property Priority() As Integer
        Get
            Return _priority
        End Get
        Set(ByVal Value As Integer)
            _priority = Value
        End Set
    End Property

End Class

Public Class CompressionSoapExtension
    Inherits SoapExtension

    Dim oldStream As Stream
    Dim NewStream As Stream

    Public Overrides Function ChainStream(ByVal stream As Stream) As Stream
        oldStream = stream
        NewStream = New MemoryStream()
        Return NewStream
    End Function

    Public Overrides Function GetInitializer(ByVal methodInfo As LogicalMethodInfo, ByVal attribute As SoapExtensionAttribute) As Object
        Return attribute
    End Function

    Public Overrides Function GetInitializer(ByVal serviceType As Type) As Object
        Return GetType(CompressionSoapExtension)
    End Function

    Public Overrides Sub Initialize(ByVal initializer As Object)
        Dim attribute As CompressionSoapExtensionAttribute = CType(initializer, CompressionSoapExtensionAttribute)
    End Sub

    Public Overrides Sub ProcessMessage(ByVal message As SoapMessage)
        Dim buffer() As Byte = New Byte(2048) {}
        Dim size As Integer

        Select Case message.Stage
            Case SoapMessageStage.AfterSerialize
                ' This is called after a SOAP message is serialized, but before it goes over the wire
                NewStream.Seek(0, SeekOrigin.Begin)
                Dim zipOutputStream As GZipOutputStream = New GZipOutputStream(oldStream)
                size = 2048
                While (True)
                    size = NewStream.Read(buffer, 0, buffer.Length)
                    If size > 0 Then
                        zipOutputStream.Write(buffer, 0, size)
                    Else
                        Exit While
                    End If
                End While
                zipOutputStream.Flush()
                zipOutputStream.Close()
                Exit Select

            Case SoapMessageStage.BeforeDeserialize
                ' This is called when the incoming message has been received
                Dim zipInputStream As GZipInputStream = New GZipInputStream(oldStream)
                size = 2048
                While (True)

                    size = zipInputStream.Read(buffer, 0, buffer.Length)
                    If size > 0 Then
                        NewStream.Write(buffer, 0, size)
                    Else
                        Exit While
                    End If
                End While
                NewStream.Flush()
                NewStream.Seek(0, SeekOrigin.Begin)
                Exit Select
        End Select
    End Sub

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -