Simon
Simon

Reputation: 14472

Create a list from a byte array

I am porting some Objective C code.

I need to create a list (I will use any type of list, whatever makes sense) of objects. The class looks like this:

class Foo{
    double fooValue1;
    double fooValue2;
    double fooValue3;
    double fooValue4;
}

I am reading a binary file into a byte array which works fine. The byte array is a list of 4 doubles. So if there are 100 instances of Foo in the file, the byte array has 3200 bytes. i.e. 4 * 8 * 100.

in Objective C, a variable is declared as a pointer to an array of type Foo. It is very fast to load that array by simply copying the bytes. The statement to do this is:

[byteArray getBytes:fooData range:range]

where range is an NSRange instance with location=0 and length=length of the byte array, byteArray is the raw byte[] read from the file and fooData is the pointer to the target array.

At the moment, I'm looping through the byte array, creating a new instance of Foo for each 32 bytes, then assigning the fields by converting each 8 bytes into doubles. For thousands of objects, this is slow.

I'm targeting API 8 so Arrays.copyOf is not available to me although if it offers a good solution, I would consider changing the target.

The question is; Is there a way to create a list in a similar "one statement" fashion to Objective C?

Thanks

[EDIT] Here's the final code I used based on Peter's answer. I should add that the file actually contains a list of lists, separated with headers. The result of parsing the file is an array of byte arrays.

ArrayList<SCGisPointData> pointData = new ArrayList<SCGisPointData>();
SCGisPointData thisPointdata;

for (byte[] ring : linearRings) {

    DoubleBuffer buffer = ByteBuffer.wrap(ring).order(ByteOrder.nativeOrder()).asDoubleBuffer().asReadOnlyBuffer();

    thisPointdata= new SCGisPointData ();

    while (buffer.hasRemaining()) {
        thisPointdata = new SCGisPointData();
        thisPointdata.longitude = buffer.get();
        thisPointdata.latitude = buffer.get();
        thisPointdata.sinLatitude = buffer.get();
        thisPointdata.cosLatitude = buffer.get();
        pointData.add(thisPointdata);  
    }

}

Upvotes: 0

Views: 311

Answers (2)

Perception
Perception

Reputation: 80593

Take a look at MappedByteBuffer. Available on Android and lets you:

  • Map a file as a buffer of bytes
  • Read double values directly from said buffer.

You will have to be careful to select the right endianess. Here is some sample code (note that you the API will do the heavy lifting of 'converting' the byte buffer into a double buffer, and that you can retrieve multiple doubles from the buffer in one go):

final String fileName = "/some/path/data.txt";
final MappedByteBuffer byteBuffer = new FileInputStream(fileName)
    .getChannel()
    .map(MapMode.READ_ONLY, 0, 3200);
final DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer();

double[] swap = new double[4];
doubleBuffer.get(swap);

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533472

There is no way in pure Java to magically assume one type can be turning into a different type. BTW You can do this in HotSpot using Unsafe but I don't believe you can use that in Android which is perhaps for the best.

You can speed up translation by using a direct ByteBuffer with native byte ordering. This minimises the overhead, but is not as fast in Android as it is in HotSpot (as the later can turn these into intrinsic methods)

I would time how long this takes as an estimate

// or use a MappedByteBuffer from a file
ByteBuffer bb = ByteBuffer.allocateDirect(3200).order(ByteOrder.nativeOrder());
while(bb.remaining() > 0) {
   float f= bb.getFloat();
}

Note: this won't run long enough to even be JITted to native code.

ByteBuffer bb = ByteBuffer.allocateDirect(3200).order(ByteOrder.nativeOrder());
for (int i = 0; i < 12; i++) {
    long start = System.nanoTime();
    bb.clear(); // simulate we have 3200 bytes to read.
    int count = 0;
    while (bb.remaining() > 0) {
        float f = bb.getFloat();
        count++;
    }
    long time = System.nanoTime() - start;
    System.out.printf("Took %,d micro-seconds to read %,d float values%n", time / 1000, count);
}

on HotSpot Java 7 prints

Took 960 micro-seconds to read 800 float values
Took 141 micro-seconds to read 800 float values
Took 99 micro-seconds to read 800 float values
Took 101 micro-seconds to read 800 float values
Took 100 micro-seconds to read 800 float values
Took 102 micro-seconds to read 800 float values
Took 115 micro-seconds to read 800 float values
Took 127 micro-seconds to read 800 float values
Took 108 micro-seconds to read 800 float values
Took 79 micro-seconds to read 800 float values
Took 78 micro-seconds to read 800 float values
Took 79 micro-seconds to read 800 float values

Upvotes: 2

Related Questions