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 :)

Community
  • 1
  • 1
Sygmoral
  • 7,021
  • 2
  • 23
  • 31

1 Answers1

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

user3159253
  • 16,836
  • 3
  • 30
  • 56