Monday, April 11, 2011

In Java: is where a way to create a subarray that will point to a portion of a bigger array?

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:

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:

  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.

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.

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

From stackoverflow
  • 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.

    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.

  • 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.

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

    Totophil : Thanks Miguel, but it seems that under the hood this workarounds no 1 and no 2 combined.
  • 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:

    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.

0 comments:

Post a Comment