4

In short: I want to cast a byte array or String into a File object, so that I can use this File object as a parameter for a method whose signature I can not change. I want to achieve this in-memory, without ever touching a drive.

I have read many similar questions here, including suggestions about using File.createTempFile, but I get the impression we're still creating physical files then, even if it's only temporary?

In full: I am helping with a very complex data recovery project. We now have a, let's say, disk image of over 400GB, which is a collection of 2MB-sections. Some of the files I want to recover begin in one, and end in another 2MB-section. We have an inventory of where we've seen beginnings and endings, so now I want to stitch beginnings and endings together, and then check whether the result is a valid file, in some propretary format. (this is all background information, let's please not get into that!)

I have a java class (that I can't change) that can read and therefore verify these files. So I want to simply feed my put-together-file into this method. If it is succesful, I move on to the next "begin-part", otherwise I try to match the begin-part with a different end-part.

I want to keep the 'stitched together files' in memory (not write it to temporary files) because it's probably clear that I need high efficiency. This is a last resort; indeed, we need to match many thousands of beginnings with many thousands of endings. But again, let's just keep to the question :)

7
  • 4
    A java.io.File only represents a path and filename, not the actual content of the file itself. Commented Nov 21, 2014 at 20:07
  • 3
    If your API takes a java.io.File, the only simple way I can think of is to use a RAM drive... Commented Nov 21, 2014 at 20:07
  • Hm, I suppose I will indeed need a RAM drive... sounds like there will be no other way if File can only ever point to 'an actual file'. Thanks! Commented Nov 21, 2014 at 20:09
  • What operating system is this? Commented Nov 21, 2014 at 20:11
  • I'm writing the program on Windows 7. I don't have easy access to another platform to work on... but I see there are many free utilities to create a RAM drive on Windows, too. Commented Nov 21, 2014 at 20:15

1 Answer 1

4

If your Java class accepts File that is, file paths in a file system, then yes, you have to create a file in a file system and then feed the Java class with the file path. You may speed up things by creating the file in RAM drive but nevertheless it's a file. If the class also accepts InputStream (well designed classes do), then you can use ByteArrayInputStream. Another possible interface of the class to accept input data is Java NIO machinery with ReadableByteChannel being the NIO analog for an abstract java.io.InputStream

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.