Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:27:34+00:00 2026-06-17T18:27:34+00:00

I am porting some Objective C code. I need to create a list (I

  • 0

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);  
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:27:36+00:00Added an answer on June 17, 2026 at 6:27 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm porting some code from other language to Ruby, in my Class I need
I'm porting some TCP server/client code to Netty. One question, the server will handle
I'm in the process of porting some code from Objective C to C++. I'm
Im just porting some Objective-C code to MonoTouch but I got stuck here UIColor
I'm porting some Objective C code to Java. One of the C methods looks
I'm busy porting some MySQL specific code to Postgresql in order to use it
I am porting some code from native C++ to C# and I need to
I am just porting some old code: #define NewArrayOnHeap(TYPE, COUNT, HEAP, NEWPTR, ERROR) \
While porting some Windows C++ code to iOS, I need to provide an implementation
I'm porting some OSX code to iOS and need to access the bytes of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.