简体   繁体   English

Java NIO-MappedByteBuffer的截断

[英]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: 我可以看到两种方法:

  1. Use some sort of a marker denoting the end of the actual data, while file size can be bigger (due to the fact that I keep it slightly bigger for appending reasons). 使用某种表示实际数据结尾的标记,而文件大小可以更大(由于出于附加原因我将其保留得稍大一些)。 This might be tricky to come up with some sort of byte sequence that would be unique. 提出某种唯一的字节序列可能会很棘手。
  2. Always make sure that the file we read in is "full" ie if that file is 2000 bytes, that means there are 10 messages in it and nothing more. 始终确保我们读取的文件为“完整”,即,如果该文件为2000字节,则意味着其中有10条消息,仅此而已。 That is even trickier I think, as we would have to constantly truncate the file size and with open memory buffer that is quite cumbersome (one cannot truncate the file size with open memory mapped file) 我认为这甚至更棘手,因为我们将不得不不断地截断文件大小并使用相当大的开放内存缓冲区(无法使用开放内存映射文件来截断文件大小)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM