How to create a new java.io.File in memory? [duplicate]
How can I create new File
(from java.io
) in memory, not on the hard disk?
I am using the Java language. I don't want to save the file on the hard drive.
I'm faced with a bad API (java.util.jar.JarFile
). It's expecting File file
of String filename
. I have no file (only byte[]
content) and can create temporary file, but it's not beautiful solution. I need to validate the digest of a signed jar.
byte[] content = getContent();
File tempFile = File.createTempFile("tmp", ".tmp");
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(archiveContent);
JarFile jarFile = new JarFile(tempFile);
Manifest manifest = jarFile.getManifest();
Any examples of how to achieve getting manifest without creating a temporary file would be appreciated.
Solution 1:
How can I create new File (from java.io) in memory , not in the hard disk?
Maybe you are confusing File
and Stream
:
- A
File
is an abstract representation of file and directory pathnames. Using aFile
object, you can access the file metadata in a file system, and perform some operations on files on this filesystem, like delete or create the file. But theFile
class does not provide methods to read and write the file contents. - To read and write from a file, you are using a
Stream
object, likeFileInputStream
orFileOutputStream
. These streams can be created from aFile
object and then be used to read from and write to the file.
You can create a stream based on a byte buffer which resides in memory, by using a ByteArrayInputStream
and a ByteArrayOutputStream
to read from and write to a byte buffer in a similar way you read and write from a file. The byte
array contains the "File's" content. You do not need a File
object then.
Both the File...
and the ByteArray...
streams inherit from java.io.OutputStream
and java.io.InputStream
, respectively, so that you can use the common superclass to hide whether you are reading from a file or from a byte array.
Solution 2:
It is not possible to create a java.io.File
that holds its content in (Java heap) memory *.
Instead, normally you would use a stream. To write to a stream, in memory, use:
OutputStream out = new ByteArrayOutputStream();
out.write(...);
But unfortunately, a stream can't be used as input for java.util.jar.JarFile
, which as you mention can only use a File
or a String
containing the path to a valid JAR file. I believe using a temporary file like you currently do is the only option, unless you want to use a different API.
If you are okay using a different API, there is conveniently a class in the same package, named JarInputStream
you can use. Simply wrap your archiveContent
array in a ByteArrayInputStream
, to read the contents of the JAR and extract the manifest:
try (JarInputStream stream = new JarInputStream(new ByteArrayInputStream(archiveContent))) {
Manifest manifest = stream.getManifest();
}
*) It's obviously possible to create a full file-system that resides in memory, like a RAM-disk, but that would still be "on disk" (and not in Java heap memory) as far as the Java process is concerned.
Solution 3:
You could use an in-memory filesystem, such as Jimfs
Here's a usage example from their readme:
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path foo = fs.getPath("/foo");
Files.createDirectory(foo);
Path hello = foo.resolve("hello.txt"); // /foo/hello.txt
Files.write(hello, ImmutableList.of("hello world"), StandardCharsets.UTF_8);
Solution 4:
I think temporary file can be another solution for that.
File tempFile = File.createTempFile(prefix, suffix, null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(byteArray);
There is a an answer about that here.