Filestream error System.StackOverflowException

M

Martyn Wynne

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.StackOverflowException 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.BufferRead, 0, BUFFER_SIZE, _
New AsyncCallback(AddressOf ReadCallBack), rs)

Full code for assembly below

Imports System
Imports System.Net
Imports System.Threading
Imports System.Text
Imports System.IO
Imports ICSharpCode.SharpZipLib.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.GetDecoder()

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 ManualResetEvent(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(ByVal 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.Net.WebRequest.Create(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.BeginGetResponse( _
New AsyncCallback(AddressOf RespCallback), rs), IAsyncResult)

' Wait until the ManualResetEvent is set so that the application
' does not exit until after the callback is called.
allDone.WaitOne()
End Sub

Shared Sub RespCallback(ByVal ar As IAsyncResult)
' Get the RequestState object from the async result.
Dim rs As RequestState = CType(ar.AsyncState, 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.EndGetResponse(ar), HttpWebResponse)

' Start reading data from the respons stream.

Dim ResponseStream As Stream
Dim GzipResponseStream As GZipInputStream
If resp.ContentEncoding = "x-gzip" Then
GzipResponseStream = New GZipInputStream(resp.GetResponseStream)
ResponseStream = CType(GzipResponseStream, Stream)
Else
ResponseStream = resp.GetResponseStream()
End If

' Store the reponse stream in RequestState to read
' the stream asynchronously.
rs.ResponseStream = ResponseStream

' Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead.
Dim iarRead As IAsyncResult = _
ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, _
New AsyncCallback(AddressOf ReadCallBack), rs)
End Sub

Shared Sub ReadCallBack(ByVal asyncResult As IAsyncResult)
' Get the RequestState object from the AsyncResult.
Dim rs As RequestState = CType(asyncResult.AsyncState, RequestState)

' Retrieve the ResponseStream that was set in RespCallback.
Dim responseStream As Stream = rs.ResponseStream

' Read rs.BufferRead to verify that it contains data.
Dim read As Integer = responseStream.EndRead(asyncResult)
If read > 0 Then
' Append the recently read data to the OutputStream
' object contained in RequestState.
rs.OutputStream.Write(rs.BufferRead, 0, read)

' Continue reading data until responseStream.EndRead
' returns -1.
Dim ar As IAsyncResult = _
responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, _
New AsyncCallback(AddressOf ReadCallBack), rs)
Else
' Close down the response stream.
responseStream.Close()

' Set the ManualResetEvent so the main thread can exit.
allDone.Set()
End If

Return
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top