[英]Java NIO - truncation of MappedByteBuffer
I am trying to use MappedMemoryBuffer
to store/delete portions of byte from the file. 我正在尝试使用MappedMemoryBuffer
存储/从文件中删除字节的一部分。 Atm, I use the following approach: I have a position in the buffer marking the end of the data in the file (buffer is a bit bigger than the file to accommodate for appending). Atm,我使用以下方法:我在缓冲区中有一个位置,用于标记文件中数据的结尾(缓冲区比文件大一点,可以容纳追加内容)。 Each data chunk is of fixed length, let's say 200 bytes. 每个数据块的长度都是固定的,比方说200个字节。
My first concern is "marking" of the end of the file. 我首先关心的是文件末尾的“标记”。 I can see two approaches to that: 我可以看到两种方法:
That's why I would lean more towards one, unless anyone knows a better approach? 这就是为什么除非有人知道更好的方法,否则我会更倾向于一个?
Now, while appending is very simple, the deletion of random data chunk is not that straightforward. 现在,虽然附加非常简单,但是删除随机数据块并不是那么简单。 What I was thinking of doing was to shift the data to overwrite the deleted message. 我当时想做的是转移数据以覆盖已删除的消息。 I simply move all the bytes to the right of message to the beginning of it. 我只是将所有字节移动到消息的右边到它的开头。
In general, is that the right strategy, or would anyone suggest something else or improvements to the concept? 总的来说,这是正确的策略,还是有人会提出其他建议或对该概念进行改进?
You could use a custom wrapper class over a FileChannel
(or File
or whatever, as long as you know it's your file) and make it provide mappings, or even write mappings by itself. 您可以在FileChannel
(或File
或其他任何东西,只要您知道它是文件)上使用自定义包装器类,并使其提供映射,甚至可以自己编写映射。
In this class, track the size. 在此类中,跟踪尺寸。 Make it implement Closeable
. 使它实现Closeable
。 And when you .close()
it, unmap everything and truncate/expand to the appropriate size. 当.close()
时,取消映射所有内容并截断/扩展为适当的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.