By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 2,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Saving MemoryStream/StreamWriter to/from Cache

P: 1
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
Share this Question
Share on Google+
1 Reply


Plater
Expert 5K+
P: 7,872
(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

Post your reply

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