English 中文(简体)
In-memory version of Java s FileChannel
原标题:

I m in the process of making some changes to a library that I m using. In order to reduce memory usage the library is writing its temporary data to disk instead of keeping it in memory. However, for my usage scenario it is more efficient to keep it in memory. It also has some concurrency issues, because it has constant names for its temp-files and can therefore not run concurrently in different threads (because the threads will corrupt each other s data).

I therefore need to change the library so that it keeps all its data in memory instead. I have not originally written the library and I m therefore not very comfortable with making drastic changes to the code. I d therefore like to do this with as little refactoring as possible. The code which writes to disk is quite simple. Here s a (somewhat simplified) example:

final FileChannel fileChannel = this.randomAccessFile.getChannel();
fileChannel.position(calculatePosition());
while (blockData.hasRemaining())
{
 fileChannel.write(blockData);
}

The reading of a block is very similar (i.e. it uses a FileChannel, which it gets from a RandomAccessFile).

It feels like the easiest solution would be if I can use some implementation of FileChannel, which maps to a place in memory instead of to a file. I know I can map a file to a place in memory using FileChannel s map-method. However, that is the other way around. That gives me a "memory-API" to a file. I want a FileChannel-interface against some memory. Are there any available implementations of this?

最佳回答

Java 7 will allow pluggable filesystems, so you ll be able to use a memory based filesystem and keep the current FileChannel API. Short of that you re likely to have to change the API you use to Commons-VFS as Adrian suggests or use memory only structures.

EDIT per the 2023 update to this question, asking if this can be done without external libraries... I suspect this is the wrong question, because FileChannel will always specifically be connected to a file object.

FileChannel consists mostly of a set of implemented interfaces...

In particular, you probably are interested in the functionality of ByteChannel or ReadableByteChannel or SeekableByteChannel, all of which have fairly small footprints. It would be pretty easy to create a ByteBufferChannel that implements one of those.

Just write your code expecting a FileChannel to instead use the actual java Interface you care about, and you can pass either a FileChannel or the proposed ByteBufferChannel in interchangably.

问题回答

Maybe you can use an in-memory filesystem, such as Apache Commons VFS.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签