Solved

Working in memory

Posted on 2004-08-18
11
415 Views
Last Modified: 2007-12-19
I don't know exactly how to phrase this question.  Is there some way to be able to work with streams strictly in memory instead of having to physically write them to the hard drive?  Ex.  I am working on an application that makes use of some .wma audio files.  Instead of each file having to write and delete from the hard drive, which of course will slow down the application, is there some way to write the files into memory, work with them and then write the final output to the hard drive.  My coworker is using a zip tool that instead of having to unzip the files to the hard drive, it would be very helpful to unzip into memory.  Is there some way to accomplish this?  Is there a tutorial on working in memory somewhere?????

Thanks,
Kendal
0
Comment
Question by:gvector1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 250 total points
ID: 11832175
 byte[] buffer;
 
  System.IO.FileStream fileStream;
  fileStream = new System.IO.FileStream(SOURCE_FILE, System.IO.FileMode.Open);
  using(fileStream)
  {
    buffer = new byte[fileStream.Length];
    fileStream.Read(buffer, 0, buffer.Length);
  }

  // You now have the file loaded into the byte array
  // named buffer.  You can work with this array, or
  // if you can use this buffer to build a Stream
  // in memory:
 
  System.IO.MemoryStream memoryStream;
  memoryStream = new System.IO.MemoryStream(buffer);

  // Work with buffer, or memoryStream
  // ...

  // Then to save:

  System.IO.FileStream saveStream;
  saveStream = new System.IO.FileStream(TARGET_FILE, System.IO.FileMode.Create);
  using(saveStream)
  {
    // If you were editing buffer:
    saveStream.Write(buffer, 0, buffer.Length);

    // OR

    // If you were editing memoryStream:
    saveStream.Write(memoryStream.ToArray(), 0, (int)memoryStream.Length);
  }
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 11835931
A memory stream would be in memory.
If you want to map a file on disk into memory you could use filemapping but it's not directly supported by the .net framework.
0
 

Author Comment

by:gvector1
ID: 11851896
Is there any way to basically do the same thing with a compressed file.  Just say a zipped file.  The reason being is that there is an application that catalogs medical image files into a database.  This medical image is a dicom file that is compressed.  The file contains header information that must be extracted to load into our database.  The way we are doing it now is having to use an activex control to decompress the file into another file on the hard drive and then reading the header information.  The activex control function that decompresses the file takes and input file and an output file as a parameter.  Is there basically any way to decompress files to memory instead of a physical file on disk?????????????

Thanks
Kendal
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 10

Expert Comment

by:eternal_21
ID: 11853669
You probably can't override the internal workings of you ActiveX control, but you could switch to another compression utility that allows you to work in memory (such as #ziplib http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx).
0
 

Author Comment

by:gvector1
ID: 11853718
I will look into that tool.  I believe I have used it in the past to my satisfaction.  I will just have to determine whether it can decompress a dicom file(the format that these medical files are compressed to).  Would you happen to know?

Thanks,
Kendal
0
 
LVL 4

Expert Comment

by:Eran_R
ID: 11873306
First, I must say that from my knowledge of DICOM files (I have some experience in medical imaging as well),
Their headers are usually not compressed (if these files comply with the DICOM standards) - Only the pixel data is compressed.
So, basically, if all you need is the headers, you can read them without decompressing the whole file.


If not, here's a crazy idea for a solution that COULD work (it's a little out there...)
What you could "theoretically" do is as follows: (The ActiveX implementation might not enable using this technique)

Create a named pipe. Named pipes have special "filenames" to open them
a named pipe has a filename that mapps to it: \\.\pipe\PipeName
(Look up CreateNamedPipe in the MSDN)

Named pipes don't have .NET framework support built in, you will need to do some PInvokes.

You could create such a named pipe, call the ActiveX passing the named pipe as a parameter, and read/write on the other end of the pipe depending on whether you want to use it as a source or destination "file". Then you would achieve what you wanted: decompress into memory.

Important Note:
Pipes have various I/O modes including blocking vs non-blocking. You may have to create a separate thread to read from the pipe that the ActiveX writes to for example (especially if the decompressed data is big).

Like I said, It's quite "out there" ;)

Hope this helps,
Eran
0
 

Author Comment

by:gvector1
ID: 11874475
Let me elaborate a little more on this situation.  This is not exactly my project, but my coworkers.  I have had a little doings with this.  What we have are compressed dicom files on a PACS server.  Once files are sent to the PACS server, it automatically compresses the files.  The current application's purpose is to go through these files and catalog them into a database.  The database needs certain information that is only available in the header file of the dicom file.  So currently the application uses an activex control called ZLibTool to decompress the file over the network to the local machine.  Once decompressed, the application uses another activex control called ezdicom to open the uncompressed dicom file and access the header information.  The header information is actually embedded into the dicom file.  So in an attempt to speed up the application and not have to uncompress the file over the network to the local machine, I was looking for a way to uncompress into memory and then access that dicom files header information.  So far I think I might be able to make the ZipLib(posted above) work for the decompression into memory, I am still working on that.  Next would be the ezdicom activex control.  I think I am going to have to stick with that control to access the header information as I cannot find any other open source utility that can do that.  The only problem ezdicom presents is that a service does not want to allow activex controls.  I did find a workaround though.  Add a form to the service, add the ezdicom control to the form, instantiate the form, and never show the form.  Therefore I have access to the control through the form and the form will never show.  How does all of that sound???

Thanks,
Kendal
0
 
LVL 4

Expert Comment

by:Eran_R
ID: 11878076
Underneath it all, an ActiveX is merely a COM object that you should be able to use even without a form.

It is possible however that the ezDicom specifically requires a hosting WINDOW in order to operate.
Using a form as a solution sounds ok... (if simple COM doesn't work)
0
 

Author Comment

by:gvector1
ID: 11881032
If I was to try it without a form, how could I access the COM object?  The only way so far I have been able to use it is to drag and drop the activex control from the toolbar, when I do the axezdicommax reference is added.  There are 2 references to the ezdicom tool, ezdicommax and axezdicommax.  I can manually add ezdicommax, but the only way the axezdicommax is added is when I drag and drop the control.  That is the only time that I can access the methods of the control that allow for header extraction.  What do you think?????

Thanks,
Kendal
0
 
LVL 4

Assisted Solution

by:Eran_R
Eran_R earned 250 total points
ID: 11882060
Simple. In your project goto Add Reference...

Select the COM tab and select the "ezDicomMax library" from the list.

Once you've done that, a namespace called "ezDicomMax" will be created which contains all the types in the ocx.
Specifically for ezDicom, you can use the following code to use the COM object:

ezDICOMax.ezDICOMX ez = new ezDICOMax.ezDICOMXClass();
ez.DCMfilename = @"c:\image.dcm";

and so on...

All this code shoudl also work without a form.
0
 

Author Comment

by:gvector1
ID: 11883776
Thank you both for the info.  Eternal, you answered the part of the question about working and unzipping in memory, and Eran, you answered my part about the DICOM files.  Excellent information and examples.  I have gladly split the points between the two of you.  

Thanks again for all of the help.
Kendal
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question