From: Lew on
Please do not top-post.
Please do cite the author of posts you quote.


Paul Cager wrote:
>> I haven't checked the API (http://java.sun.com/javase/6/docs/api/
>> index.html?java/nio/package-summary.html) but I seem to remember that
>> NIO allows you to read floats etc in the byte order of your choice.
>> That might avoid the need to do all the bit twiddling yourself.
>

Mikee
> Currently part of the environment I'm working in is constrained to 1.5
> but I'lll check 1.6 out for future ref.
>

Why is that relevant?

Paul suggested NIO, which was introduced in Java 1.4.

You really should read the Javadocs.
<http://java.sun.com/j2se/1.5.0/docs/api/java/nio/package-
summary.html>

--
Lew
From: Nigel Wade on
On Wed, 09 Jun 2010 10:14:29 +0000, Thomas Pornin wrote:

>
> You _might_ correct your code by using Float.float.toRawIntBits()
> instead of Float.floatToIntBits(), but even if it works, it is
> non-robust (it assumes that DataInputStream also does things that way).
> A more correct way of doing things is to read the bytes as an "int"
> directly, which basically removes steps 2 and 3 altogether. This would
> look like this:
>
> System.out.print(Float.intBitsToFloat(swap(in.readInt())) + " ");
>
>

I prefer to load the data into a byte array, then use a ByteBuffer to
access the data directly from memory in the "correct" format. Everything
necessary is already available in the standard API classes, no manual bit
shifting, byte-swapping etc. is required.

The basic scenario is:

create a ByteBuffer
set it to ByteBuffer.LITTLE_ENDIAN

loop:

read block of data into byte[]
wrap ByteBuffer around byte[]
read data from ByteBuffer

E.g. this is an extract of some code which is run routinely here, and
reads little-endian data files:

DataInputStream dataIn;
short fitRecordLength;
short inxRecordLength;
....

// read the fit and inx lengths (this is little endian)
byte[] lengthBytes = new byte[4];
dataIn.readFully( lengthBytes );

// wrap the byte array in a ByteBuffer, this allows
// reading of little endian data
ByteBuffer bb = ByteBuffer.wrap( lengthBytes );
bb.order( ByteOrder.LITTLE_ENDIAN );

fitRecordLength = bb.getShort();
inxRecordLength = bb.getShort();
....

ByteBuffer dataBuffer;
byte[] bufferArray;
bufferArray = new byte[fitRecordLength];
dataBuffer = ByteBuffer.wrap( bufferArray );
dataBuffer.order( ByteOrder.LITTLE_ENDIAN );

dataIn.readFully( bufferArray );
dataBuffer.position( 0 );

int recordNumber = dataBuffer.getInt();


--
Nigel Wade

From: Roedy Green on
On Wed, 9 Jun 2010 00:17:53 -0700 (PDT), Mikee
<mikee.read(a)googlemail.com> wrote, quoted or indirectly quoted someone
who said :

>
>-13.73482 , 20.689 , -99.99999 , 20.157 ,1 9.454

those are not floats. Those are characters.

Floats are binary. You can't create them with a text editor.

You can WRITE the file with floats. See
http://mindprod.com/applet/fileio.html

Then you can read them with DataInputStream.

Your data look like a CSV file. See
http://mindprod.com/jgloss/csv.html
for how to read them.

--
Roedy Green Canadian Mind Products
http://mindprod.com

Have you ever noticed that any computer search in the movies, is always linear, with, for example, candidate fingerprints flashing up on the screen one after another? The public is still under the delusion that electronic files are microscopic filing cabinets made out of tiny wires or magnetic patches inside the computer. Most lay people are surprised that it is easy for a computer to file things simultaneously by a dozen different schemes, and that they can have any report printed in any number of different sorted orders. With physical files, they are limited to one ordering/access.
From: Mikee on
On Jun 9, 2:54 pm, Nigel Wade <n...(a)ion.le.ac.uk> wrote:

> I prefer to load the data into a byte array, then use a ByteBuffer to
> access the data directly from memory in the "correct" format. Everything
> necessary is already available in the standard API classes, no manual bit
> shifting, byte-swapping etc. is required.
>

Hi Nige

Thanks that method works well.

Mike
From: Mikee on
On Jun 9, 2:34 pm, Lew <l...(a)lewscanon.com> wrote:

> > Currently part of the environment I'm working in is constrained to 1.5
> > but I'lll check 1.6 out for future ref.
>
> Why is that relevant?

Saw the 1.6 in the URL and rushed to a wrong conclusion.

Mike
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: Basic custom layout manager question
Next: Sending SMS