使用仅使用Java内存的自定义文件实现的最简单方法是什么?

时间:2020-12-18 04:29:35

I have a code base that makes extensive use of files to represent a single data object. I have refactored the code so that the data object is now an explicit java object that mostly hides interaction with the underlying file system. However, we use a few external tools (like Weka), that read and write files.

我有一个代码库,广泛使用文件来表示单个数据对象。我重构了代码,以便数据对象现在是一个显式的java对象,它主要隐藏与底层文件系统的交互。但是,我们使用一些外部工具(如Weka)来读写文件。

Ideally, I would like to have a subclass of File that I can pass to these other libraries that is backed by an in- memory array of bytes -- either for reading or writing.

理想情况下,我希望有一个File的子类,我可以传递给这些由内存中的字节数组支持的其他库 - 用于读取或写入。

My google searches turned up memory-mapped files, but this is not the problem I have as I don't want any actual file on the file system. Are there any solutions already out there, or should I just subclass File myself and over-ride all the methods that refer to the real file system?

我的谷歌搜索打开了内存映射文件,但这不是我的问题,因为我不想在文件系统上有任何实际文件。有没有任何解决方案,或者我应该自己继承File并覆盖所有引用真实文件系统的方法?

4 个解决方案

#1


This is generally why you should never write methods to take a File unless it's really necessary. Do your libraries not provide methods that take an arbitrary InputStream? In this case, it's trivial to pass a ByteArrayInputStream or any input stream that reads from memory.

这通常是为什么你永远不应该编写方法来获取文件,除非它确实是必要的。您的库不提供采用任意InputStream的方法吗?在这种情况下,传递ByteArrayInputStream或从内存中读取的任何输入流都是微不足道的。

The problem with overriding File is it's unlikely that you can override it in a way that will help you. For example, if the library opens it with a FileInputStream, then where the bytes come from at that point is really controlled by the FileInputStream implementation, not by your File.

覆盖文件的问题是,您不太可能以对您有帮助的方式覆盖它。例如,如果库使用FileInputStream打开它,那么来自该点的字节实际上由FileInputStream实现控制,而不是由您的File控制。

#2


java.io.File is essentially a wrapper around a String that represent a file path (it should have been a final class, but there you go). So you are out of luck with that approach. You can, of course, create temporary files and files on "RAM discs".

java.io.File本质上是一个String的包装器,它代表一个文件路径(它应该是一个最终的类,但你去了)。所以你对这种方法运气不好。当然,您可以在“RAM光盘”上创建临时文件和文件。

#3


you should rather look out for in-memory implementation of OutputStreams/InputStreams such as ByteArrayOutputStream with its toByteArray() method, or the ByteArrayInputStream with the byte[] constructor. if your data is represented more complex, you might implement the interface directly.

你应该注意内存实现OutputStreams / InputStreams,例如ByteArrayOutputStream及其toByteArray()方法,或ByteArrayInputStream with byte []构造函数。如果您的数据表示更复杂,您可以直接实现该接口。

#4


You have to find out what File methods are being used by your 3rd party and provide the custom implementation for them.

您必须找出第三方正在使用的File方法,并为它们提供自定义实现。

Is almost sure you won't be able to succeed because the java.io.File object is not in charge of the reading/writing of the File content.

几乎可以肯定你将无法成功,因为java.io.File对象不负责读取/写入文件内容。

The best approach would be to refactor the code so it don't depend on the File object but in the byte array returned by that file.

最好的方法是重构代码,使其不依赖于File对象,而是依赖于该文件返回的字节数组。

Creating a subclass of file is possible.

可以创建文件的子类。

public class InMemoryFile extends File { 
    public InMemody() { 
         super( "/dev/null" );
    }
    // find out what methods should be overwritten ?????
}

#1


This is generally why you should never write methods to take a File unless it's really necessary. Do your libraries not provide methods that take an arbitrary InputStream? In this case, it's trivial to pass a ByteArrayInputStream or any input stream that reads from memory.

这通常是为什么你永远不应该编写方法来获取文件,除非它确实是必要的。您的库不提供采用任意InputStream的方法吗?在这种情况下,传递ByteArrayInputStream或从内存中读取的任何输入流都是微不足道的。

The problem with overriding File is it's unlikely that you can override it in a way that will help you. For example, if the library opens it with a FileInputStream, then where the bytes come from at that point is really controlled by the FileInputStream implementation, not by your File.

覆盖文件的问题是,您不太可能以对您有帮助的方式覆盖它。例如,如果库使用FileInputStream打开它,那么来自该点的字节实际上由FileInputStream实现控制,而不是由您的File控制。

#2


java.io.File is essentially a wrapper around a String that represent a file path (it should have been a final class, but there you go). So you are out of luck with that approach. You can, of course, create temporary files and files on "RAM discs".

java.io.File本质上是一个String的包装器,它代表一个文件路径(它应该是一个最终的类,但你去了)。所以你对这种方法运气不好。当然,您可以在“RAM光盘”上创建临时文件和文件。

#3


you should rather look out for in-memory implementation of OutputStreams/InputStreams such as ByteArrayOutputStream with its toByteArray() method, or the ByteArrayInputStream with the byte[] constructor. if your data is represented more complex, you might implement the interface directly.

你应该注意内存实现OutputStreams / InputStreams,例如ByteArrayOutputStream及其toByteArray()方法,或ByteArrayInputStream with byte []构造函数。如果您的数据表示更复杂,您可以直接实现该接口。

#4


You have to find out what File methods are being used by your 3rd party and provide the custom implementation for them.

您必须找出第三方正在使用的File方法,并为它们提供自定义实现。

Is almost sure you won't be able to succeed because the java.io.File object is not in charge of the reading/writing of the File content.

几乎可以肯定你将无法成功,因为java.io.File对象不负责读取/写入文件内容。

The best approach would be to refactor the code so it don't depend on the File object but in the byte array returned by that file.

最好的方法是重构代码,使其不依赖于File对象,而是依赖于该文件返回的字节数组。

Creating a subclass of file is possible.

可以创建文件的子类。

public class InMemoryFile extends File { 
    public InMemody() { 
         super( "/dev/null" );
    }
    // find out what methods should be overwritten ?????
}