473,378 Members | 1,439 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.

Saving MemoryStream/StreamWriter to/from Cache

Hi
I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework.
Ok, we have a series of reports that get run via a report filter screen, and whilst each report is being generated, it populates a memorystream/streamwriter. This has always been done this way but I am trying to update/improve and generally speed things up.
The variables are declared as follows:
Expand|Select|Wrap|Line Numbers
  1. MemoryStream vStream = new MemoryStream();
  2. StreamWriter vWriter = new StreamWriter(vStream);
  3.  
and during processing, lines of data are written to the writer like this:
Expand|Select|Wrap|Line Numbers
  1. vWriteLine = "Job Level, Individuals, Companies, Lower Decile, Lower Quartile, Median, Upper Quartile, Upper Decile, Lower Decile, Lower Quartile, Median, Upper Quartile, Upper Decile";
  2. vWriter.WriteLine(vWriteLine);
  3.  
If the user then selects the 'download' button once the report has displayed, it runs the whole report code again (therefore duplicating time/effort), and then at the end it does the following:
Expand|Select|Wrap|Line Numbers
  1. if (ReportType == "DownLoad")
  2. {
  3.     vWriter.Flush();
  4.     vStream.Seek(0, System.IO.SeekOrigin.Begin);
  5.     DownloadRawStream(vStream, "text/plain", "Output.csv");
  6. }
  7.  
where DownloadRawStream is written as follows:
Expand|Select|Wrap|Line Numbers
  1. protected void DownloadRawStream( Stream aSource, string aContentType, string aOutFilename ) 
  2. {
  3.       Response.ContentType = aContentType;
  4.       Response.AppendHeader( "Content-Disposition", "attachment; filename=" + aOutFilename + ";");
  5.       int vBuffSize = 2048;
  6.       byte[] vBuffer = new byte[vBuffSize];
  7.       int vBytesRead;
  8.       bool vFinished = false;
  9.       while( ! vFinished ) {
  10.         vBytesRead = aSource.Read( vBuffer, 0, vBuffSize );
  11.         if ( vBytesRead > 0 ) 
  12.         {
  13.           Response.OutputStream.Write( vBuffer, 0, vBytesRead );
  14.         }
  15.         vFinished = ( vBytesRead < vBuffSize );
  16.       }
  17.       Response.End();
  18.     }
  19.  
I have figured out the saving/retreiving data into/from cache, but cannot figure out how to adapt my existing code to save the completed report in the cache, and only when the user selects the 'download report' button to take the cached data and write it to the output file. I also realise that I will probably need to adapt my download procedure.
FYI There could be more than one person at a time using the reports, and the reports will be different for each user, based on their selections, so the cached data needs to be unique to the user?
As I am new to using the cache (until now usually just use session objects), I was wondering if what I want to achieve is possible.
This is what i have tried to do, but guess i just want someone who knows about this to tell me if I'm on the right path, or if I've gone wrong, how to fix it!
Expand|Select|Wrap|Line Numbers
  1. // flush the writer...
  2. vWriter.Flush();
  3. // write to stream...
  4. vStream.Seek(0, System.IO.SeekOrigin.Begin);
  5. // set buffer size...
  6. int vBuffSize = 2048;
  7. // create buffer array...
  8. byte[] vBuffer = new byte[vBuffSize];
  9. // define and initialise marker...
  10. bool vFinished = false;
  11. // output to buffer
  12. while (!vFinished)
  13. {
  14.     // get the number of bytes read...
  15.     int vBytesRead = vStream.Read(vBuffer, 0, vBuffSize);
  16.     // set boolean variable...
  17.     vFinished = (vBytesRead < vBuffSize);
  18. }
  19. // try and get the existing report...
  20. byte[] vCache = Cache.Get("Report1") as byte[];
  21. // if found, remove it...
  22. if (vCache != null) Cache.Remove("Report1");
  23. // insert report into cache...
  24. Cache.Insert("Report1", vBuffer);
  25.  
When the button is clicked to download the report:
Expand|Select|Wrap|Line Numbers
  1. // get report from cache...
  2. byte[] vBuffer = Cache.Get("Report1") as byte[];
  3. // output to CSV...
  4. Response.ContentType = "text/plain";
  5. Response.AppendHeader( "Content-Disposition", "attachment; filename=Output.csv;");
  6. Response.OutputStream.Write(vBuffer, 0, vBuffer.Length);
  7. Response.End;
  8.  
Any help/advice gratefully received.
Regards
Martin
Feb 25 '08 #1
1 5711
Plater
7,872 Expert 4TB
(I don't know why this thread is closed, it seems like a perfectly legit question, so I re-opened it)

I am unsure why you are writing string data to a stream(actually two streams), seems like you could just as easily just use the string itself. (Actually it would probably be better to use the StringBuilder class?).
Feb 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Bob Rock | last post by:
Hello, I've noticed that after calling the Close method on a StreamWriter, I get exceptions on any operation I might request on the associated stream (with a message "Cannot access a closed...
4
by: Kai Bohli | last post by:
Hi all ! This message has been posted on .net.general, but I reposts it here due to lack of replies. This is my first day with Visual Studio and C#, and I'm trying to send "raw data" to the...
7
by: Donovan | last post by:
I can't believe this is causing me as much difficulty as it is, but I have an Infragistics UltraTreeview control that I want to persist whatever the user has in the tree. It has a method SaveAsXml...
2
by: Reshma Prabhu | last post by:
hello, I am trying to do an xsl tranformation from an XML file into another xml file. I want the output file to be in MemoryStream so that my dataset can direclty read xml using...
2
by: Bruce Ramsey | last post by:
We have a web application written in ASP.NET using C#. The application renders Maps using the SVG (scalable vector graphics) standard. Normally the SVG is sent direct to the user from the Aspx...
4
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
2
by: Mikus Sleiners | last post by:
I'm trying to write new stream from string and i can't figure out why my memory stream instance is null after i have writen to it with stream writer. Here is an example. MemoryStream stream =...
5
by: BLUE | last post by:
I insert a string in cache in a property set and I retrieve that string in the get. I retrieve my string with Read web method and I insert it with Write web method: to try cache I do Read,...
2
by: aa49 | last post by:
Hi all, I was trying to unzip a file into the memory stream instead of writing it to text file. The problem I am facing is, the 13 characters in the file's first line are missing other than that...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.