I'm currently converting a Delphi method used for random binary file access into Java. The Delphi procedure uses:
TSingleArray = Array[0..MAXSIZE] of Single
...
procedure GetLinkValues(const LinkVar: Integer; const TimePeriod: Integer; var Value: PSingleArray);
...
BlockRead(Fout, Value^, Nlinks*SizeOf(Single));
To read an array of bytes into an array of Single. Is there an equivalent way of doing this in Java without iterating through the array?
I’m currently using
List<Float> l = new ArrayList<Float>();
…
for (int i = 0 ; i < nLinks ; i++ )
l.add( resultsFile.readFloat());
But I’m concerned about speed. Endianness is not a problem.
You can create an array like this on the stack, which is mostly when you have smaller arrays: float myarray[12]; it is created in the scope and destroyed when that scope is left.
byteValue() is a built-in method in Java that returns the value of this Float as a byte(by casting to a byte). Basically it used for narrowing primitive conversion of Float type to a byte value.
What is a float Array in Java ? A float is a Java data type which can store floating point numbers (i.e., numbers that can have a fractional part). Only 4 bytes are used to store float numbers giving a value range of -3.4028235E+38 to 3.4028235E+38.
Have you profiled the code and actually found it to be a problem? Something is going to have to loop... are you really sure this is a bottleneck in your code?
Having said all that, you should be able to use a FloatBuffer which I suspect does what you want. Unfortunately Sun's JavaDoc is down, so I can't easily link to or check the documentation at the minute.
To use a FloatBuffer, you'd probably want to:
I'm not particularly familiar/comfortable with java.nio, so I hope this is all correct - but it's likely to be pretty fiddly. Your current loop is almost certainly simpler, so I strongly suggest you check the performance of that first! You might want to wrap your current FileInputStream in a BufferedInputStream, btw.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With