Hi,
Can anyone please tell me if there is any reason why when i am streaming
from a webrequest (decompressing on route) to a file on the hard drive, i
would be getting an exception of Filestream error
System.StackOve rflowException every time the file gets to 8580Kbs.
Code is basically a modified Microsoft example, it errors on the line
Dim ar As IAsyncResult = _
responseStream. BeginRead(rs.Bu fferRead, 0, BUFFER_SIZE, _
New AsyncCallback(A ddressOf ReadCallBack), rs)
Full code for assembly below
Imports System
Imports System.Net
Imports System.Threadin g
Imports System.Text
Imports System.IO
Imports ICSharpCode.Sha rpZipLib.GZip
' The RequestState class passes data across async calls.
Public Class RequestState
Public RequestData As New StringBuilder(" ")
Public BufferRead(1024 ) As Byte
Public Request As HttpWebRequest
Public ResponseStream As Stream
Public OutputStream As Stream
' Create Decoder for appropriate encoding type.
Public StreamDecode As Decoder = Encoding.UTF8.G etDecoder()
Public Sub New()
Request = Nothing
ResponseStream = Nothing
OutputStream = Nothing
End Sub
End Class
' ClientGetAsync issues the async request.
Public Class WebRequest
Shared allDone As New ManualResetEven t(False)
Const BUFFER_SIZE As Integer = 1024
' The delegate must have the same signature as the method
' you want to call asynchronously.
Public Delegate Sub AsyncDelegate(B yVal HttpSite As Uri, ByVal
OutputStream As Stream)
Shared Sub GetAsync(ByVal HttpSite As Uri, ByVal OutputStream As Stream)
' Create the request object.
Dim wreq As HttpWebRequest = _
CType(System.Ne t.WebRequest.Cr eate(HttpSite), HttpWebRequest)
' Create the state object.
Dim rs As RequestState = New RequestState
' Put the request into the state so it can be passed around.
rs.Request = wreq
rs.OutputStream = OutputStream
' Issue the async request.
Dim r As IAsyncResult = _
CType(wreq.Begi nGetResponse( _
New AsyncCallback(A ddressOf RespCallback), rs), IAsyncResult)
' Wait until the ManualResetEven t is set so that the application
' does not exit until after the callback is called.
allDone.WaitOne ()
End Sub
Shared Sub RespCallback(By Val ar As IAsyncResult)
' Get the RequestState object from the async result.
Dim rs As RequestState = CType(ar.AsyncS tate, RequestState)
' Get the HttpWebRequest from RequestState.
Dim req As HttpWebRequest = rs.Request
' Call EndGetResponse, which returns the HttpWebResponse object
' that came from the request issued above.
Dim resp As HttpWebResponse = _
CType(req.EndGe tResponse(ar), HttpWebResponse )
' Start reading data from the respons stream.
Dim ResponseStream As Stream
Dim GzipResponseStr eam As GZipInputStream
If resp.ContentEnc oding = "x-gzip" Then
GzipResponseStr eam = New GZipInputStream (resp.GetRespon seStream)
ResponseStream = CType(GzipRespo nseStream, Stream)
Else
ResponseStream = resp.GetRespons eStream()
End If
' Store the reponse stream in RequestState to read
' the stream asynchronously.
rs.ResponseStre am = ResponseStream
' Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead.
Dim iarRead As IAsyncResult = _
ResponseStream. BeginRead(rs.Bu fferRead, 0, BUFFER_SIZE, _
New AsyncCallback(A ddressOf ReadCallBack), rs)
End Sub
Shared Sub ReadCallBack(By Val asyncResult As IAsyncResult)
' Get the RequestState object from the AsyncResult.
Dim rs As RequestState = CType(asyncResu lt.AsyncState, RequestState)
' Retrieve the ResponseStream that was set in RespCallback.
Dim responseStream As Stream = rs.ResponseStre am
' Read rs.BufferRead to verify that it contains data.
Dim read As Integer = responseStream. EndRead(asyncRe sult)
If read > 0 Then
' Append the recently read data to the OutputStream
' object contained in RequestState.
rs.OutputStream .Write(rs.Buffe rRead, 0, read)
' Continue reading data until responseStream. EndRead
' returns -1.
Dim ar As IAsyncResult = _
responseStream. BeginRead(rs.Bu fferRead, 0, BUFFER_SIZE, _
New AsyncCallback(A ddressOf ReadCallBack), rs)
Else
' Close down the response stream.
responseStream. Close()
' Set the ManualResetEven t so the main thread can exit.
allDone.Set()
End If
Return
End Sub
End Class
Can anyone please tell me if there is any reason why when i am streaming
from a webrequest (decompressing on route) to a file on the hard drive, i
would be getting an exception of Filestream error
System.StackOve rflowException every time the file gets to 8580Kbs.
Code is basically a modified Microsoft example, it errors on the line
Dim ar As IAsyncResult = _
responseStream. BeginRead(rs.Bu fferRead, 0, BUFFER_SIZE, _
New AsyncCallback(A ddressOf ReadCallBack), rs)
Full code for assembly below
Imports System
Imports System.Net
Imports System.Threadin g
Imports System.Text
Imports System.IO
Imports ICSharpCode.Sha rpZipLib.GZip
' The RequestState class passes data across async calls.
Public Class RequestState
Public RequestData As New StringBuilder(" ")
Public BufferRead(1024 ) As Byte
Public Request As HttpWebRequest
Public ResponseStream As Stream
Public OutputStream As Stream
' Create Decoder for appropriate encoding type.
Public StreamDecode As Decoder = Encoding.UTF8.G etDecoder()
Public Sub New()
Request = Nothing
ResponseStream = Nothing
OutputStream = Nothing
End Sub
End Class
' ClientGetAsync issues the async request.
Public Class WebRequest
Shared allDone As New ManualResetEven t(False)
Const BUFFER_SIZE As Integer = 1024
' The delegate must have the same signature as the method
' you want to call asynchronously.
Public Delegate Sub AsyncDelegate(B yVal HttpSite As Uri, ByVal
OutputStream As Stream)
Shared Sub GetAsync(ByVal HttpSite As Uri, ByVal OutputStream As Stream)
' Create the request object.
Dim wreq As HttpWebRequest = _
CType(System.Ne t.WebRequest.Cr eate(HttpSite), HttpWebRequest)
' Create the state object.
Dim rs As RequestState = New RequestState
' Put the request into the state so it can be passed around.
rs.Request = wreq
rs.OutputStream = OutputStream
' Issue the async request.
Dim r As IAsyncResult = _
CType(wreq.Begi nGetResponse( _
New AsyncCallback(A ddressOf RespCallback), rs), IAsyncResult)
' Wait until the ManualResetEven t is set so that the application
' does not exit until after the callback is called.
allDone.WaitOne ()
End Sub
Shared Sub RespCallback(By Val ar As IAsyncResult)
' Get the RequestState object from the async result.
Dim rs As RequestState = CType(ar.AsyncS tate, RequestState)
' Get the HttpWebRequest from RequestState.
Dim req As HttpWebRequest = rs.Request
' Call EndGetResponse, which returns the HttpWebResponse object
' that came from the request issued above.
Dim resp As HttpWebResponse = _
CType(req.EndGe tResponse(ar), HttpWebResponse )
' Start reading data from the respons stream.
Dim ResponseStream As Stream
Dim GzipResponseStr eam As GZipInputStream
If resp.ContentEnc oding = "x-gzip" Then
GzipResponseStr eam = New GZipInputStream (resp.GetRespon seStream)
ResponseStream = CType(GzipRespo nseStream, Stream)
Else
ResponseStream = resp.GetRespons eStream()
End If
' Store the reponse stream in RequestState to read
' the stream asynchronously.
rs.ResponseStre am = ResponseStream
' Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead.
Dim iarRead As IAsyncResult = _
ResponseStream. BeginRead(rs.Bu fferRead, 0, BUFFER_SIZE, _
New AsyncCallback(A ddressOf ReadCallBack), rs)
End Sub
Shared Sub ReadCallBack(By Val asyncResult As IAsyncResult)
' Get the RequestState object from the AsyncResult.
Dim rs As RequestState = CType(asyncResu lt.AsyncState, RequestState)
' Retrieve the ResponseStream that was set in RespCallback.
Dim responseStream As Stream = rs.ResponseStre am
' Read rs.BufferRead to verify that it contains data.
Dim read As Integer = responseStream. EndRead(asyncRe sult)
If read > 0 Then
' Append the recently read data to the OutputStream
' object contained in RequestState.
rs.OutputStream .Write(rs.Buffe rRead, 0, read)
' Continue reading data until responseStream. EndRead
' returns -1.
Dim ar As IAsyncResult = _
responseStream. BeginRead(rs.Bu fferRead, 0, BUFFER_SIZE, _
New AsyncCallback(A ddressOf ReadCallBack), rs)
Else
' Close down the response stream.
responseStream. Close()
' Set the ManualResetEven t so the main thread can exit.
allDone.Set()
End If
Return
End Sub
End Class