473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Filestream error System.StackOverflowException

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
Jul 21 '05 #1
0 1788

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
5
by: Jesee | last post by:
I am reading Jeffrey Richter's book "Applied Microsoft .NET Framework programming",i came across "Exception handing". Page 405 says "If the stack overflow occurs within the CLR itself,your...
2
by: Anders Both | last post by:
In a system with asynkronius socket and different collection, there are some times throw a System.StackOverflowException . I cannot really figur out why , Can someone say something clever about...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
5
by: Jason MacKenzie | last post by:
I have a production critical windows forms application. I have try catch blocks everywhere to handle every eventuality. However occasionally, every couple of months, the application crashes. Its...
0
by: Martyn Wynne | last post by:
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...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
9
by: Tim_Mac | last post by:
hi, i'm not sure if i have chosen the best approach, but it seemed quite good to me. i have a collection class, containing business objects. the collection class is static and remains in-memory...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.