在Java中:是否有一种方法可以创建指向较大数组的一部分的子数组?

时间:2022-03-24 22:59:54

Learning Java, so be gentle please. Ideally I need to create an array of bytes that will point to a portion of a bigger array:

学习Java,所以请温柔。理想情况下,我需要创建一个字节数组,指向一个更大数组的一部分:

byte[] big  = new byte[1000];

// C-style code starts
load(file,big);

byte[100] sub = big + 200; 

// C-style code ends

I know this is not possible in Java and there are two work-arounds that come to mind and would include:

我知道这在Java中是不可能的,我想到了两个解决办法,其中包括:

  1. Either copying portion of big into sub iterating through big.

    将大的部分复制到子遍历中。

  2. Or writting own class that will take a reference to big + offset + size and implementing the "subarray" through accessor methods using big as the actual underlying data structure.

    或者,通过使用big作为实际底层数据结构的访问器方法来引用大型+偏移量+大小和实现“子数组”的类。

The task I am trying to solve is to load a file into memory an then gain read-only access to the records stored withing the file through a class. The speed is paramount, hence ideally I'd like to avoid copying or accessor methods. And since I'm learning Java, I'd like to stick with it.

我要解决的任务是将文件加载到内存中,然后通过类获得对存储在文件中的记录的只读访问。速度非常重要,因此理想情况下,我希望避免复制或访问方法。因为我正在学习Java,所以我想继续学习它。

Any other alternatives I've got? Please do ask questions if I didn't explain the task well enough.

我还有别的选择吗?如果我没有很好地解释这项任务,请提出问题。

4 个解决方案

#1


21  

Creating an array as a "view" of an other array is not possible in Java. But you could use java.nio.ByteBuffer, which is basically the class you suggest in work-around #2. For instance:

在Java中不可能将数组创建为其他数组的“视图”。但是您可以使用java.nio。ByteBuffer,它基本上是您建议用于第2类的类。例如:

ByteBuffer subBuf = ByteBuffer.wrap(big, 200, 100).slice().asReadOnlyBuffer();

No copying involved (some object creation, though). As a standard library class, I'd also assume that ByteBuffer is more likely to receive special treatment wrt. "JIT" optimizations by the JVM than a custom one.

不涉及复制(尽管有一些对象创建)。作为一个标准的库类,我还假设ByteBuffer更可能接收特殊的wrt处理。JVM的“JIT”优化,而不是自定义优化。

#2


3  

If you want to read a file fast and with low-level access, check the java nio stuff. Here's an example from java almanac.

如果希望快速读取文件并进行低级别访问,请检查java nio内容。这里有一个来自java年鉴的例子。

You can use a mapped byte buffer to navigate within the file content.

可以使用映射的字节缓冲区在文件内容中导航。

#3


1  

Take a look at the source for java.lang.String (it'll be in the src.zip or src.jar). You will see that they have a an array of cahrs and then an start and end. So, yes, the solution is to use a class to do it.

看看java.lang的源代码。字符串(它将在src中。zip或src.jar)。您将看到它们有一个cahrs数组,然后是开始和结束。所以,是的,解决方案是使用一个类来完成它。

Here is a link to the source online.

这里有一个到在线源代码的链接。

The variables of interest are:

感兴趣的变量是:

  • value
  • 价值
  • offset
  • 抵消
  • count

substring is probably a good method to look at as a starting point.

子字符串可能是一种很好的方法,可以把它看作一个起点。

If you want to read directlry from the file make use of the java.nio.channels.FileChannel class, specifically the map() method - that will let you use memory mapped I/O which will be very fast and use less memory than the copying to arrays.

如果您想从文件中读取directlry,请使用java.nio.channels。FileChannel类,特别是map()方法——将允许您使用内存映射I/O,它将非常快,并且比复制到数组使用更少的内存。

#4


-1  

Reading file to memory is good ass soon file is relatively small. But when you have to deal with huge files keeping them out of memory is a much more desired target.

读取文件到内存很好,很快文件就比较小了。但是,当您不得不处理大型文件时,将它们排除在内存之外是一个更理想的目标。

MappedByteBuffer is that thing you are looking for.

MappedByteBuffer就是你要找的那个。

MappedByteBuffer is A direct byte buffer whose content is a memory-mapped region of a file. The content of a mapped byte buffer can change at any time, for example if the content of the corresponding region of the mapped file is changed by this program or another. Whether or not such changes occur, and when they occur, is operating-system dependent and therefore unspecified.

MappedByteBuffer是直接字节缓冲区,其内容是文件的内存映射区域。映射字节缓冲区的内容可以随时更改,例如,如果映射文件的相应区域的内容被这个程序或另一个程序更改的话。这种变化是否发生,何时发生,取决于操作系统,因此不确定。

#1


21  

Creating an array as a "view" of an other array is not possible in Java. But you could use java.nio.ByteBuffer, which is basically the class you suggest in work-around #2. For instance:

在Java中不可能将数组创建为其他数组的“视图”。但是您可以使用java.nio。ByteBuffer,它基本上是您建议用于第2类的类。例如:

ByteBuffer subBuf = ByteBuffer.wrap(big, 200, 100).slice().asReadOnlyBuffer();

No copying involved (some object creation, though). As a standard library class, I'd also assume that ByteBuffer is more likely to receive special treatment wrt. "JIT" optimizations by the JVM than a custom one.

不涉及复制(尽管有一些对象创建)。作为一个标准的库类,我还假设ByteBuffer更可能接收特殊的wrt处理。JVM的“JIT”优化,而不是自定义优化。

#2


3  

If you want to read a file fast and with low-level access, check the java nio stuff. Here's an example from java almanac.

如果希望快速读取文件并进行低级别访问,请检查java nio内容。这里有一个来自java年鉴的例子。

You can use a mapped byte buffer to navigate within the file content.

可以使用映射的字节缓冲区在文件内容中导航。

#3


1  

Take a look at the source for java.lang.String (it'll be in the src.zip or src.jar). You will see that they have a an array of cahrs and then an start and end. So, yes, the solution is to use a class to do it.

看看java.lang的源代码。字符串(它将在src中。zip或src.jar)。您将看到它们有一个cahrs数组,然后是开始和结束。所以,是的,解决方案是使用一个类来完成它。

Here is a link to the source online.

这里有一个到在线源代码的链接。

The variables of interest are:

感兴趣的变量是:

  • value
  • 价值
  • offset
  • 抵消
  • count

substring is probably a good method to look at as a starting point.

子字符串可能是一种很好的方法,可以把它看作一个起点。

If you want to read directlry from the file make use of the java.nio.channels.FileChannel class, specifically the map() method - that will let you use memory mapped I/O which will be very fast and use less memory than the copying to arrays.

如果您想从文件中读取directlry,请使用java.nio.channels。FileChannel类,特别是map()方法——将允许您使用内存映射I/O,它将非常快,并且比复制到数组使用更少的内存。

#4


-1  

Reading file to memory is good ass soon file is relatively small. But when you have to deal with huge files keeping them out of memory is a much more desired target.

读取文件到内存很好,很快文件就比较小了。但是,当您不得不处理大型文件时,将它们排除在内存之外是一个更理想的目标。

MappedByteBuffer is that thing you are looking for.

MappedByteBuffer就是你要找的那个。

MappedByteBuffer is A direct byte buffer whose content is a memory-mapped region of a file. The content of a mapped byte buffer can change at any time, for example if the content of the corresponding region of the mapped file is changed by this program or another. Whether or not such changes occur, and when they occur, is operating-system dependent and therefore unspecified.

MappedByteBuffer是直接字节缓冲区,其内容是文件的内存映射区域。映射字节缓冲区的内容可以随时更改,例如,如果映射文件的相应区域的内容被这个程序或另一个程序更改的话。这种变化是否发生,何时发生,取决于操作系统,因此不确定。