DataInputStream sometimes reads NaN instead of a float
<p>Bit of a Java newb. Writing an STL file viewer. I'm reading floats from a binary file using a DataInputStream but randomly getting NaN on some values. The file, when opened on a PC in STL file readers seems to be intact, but the NaN values I'm getting mean quite a few of the facets in the file are corrupted. What circumstances could lead to a DataInputStream returning NaN?</p>

<p>My code seems to work fine, it is just these random NaNs which are driving me crazy. Here's a truncated version of the reading code - all pretty straightforward I think. Any help is much appreciated:</p>

<pre><code>public static int GetFacetBinary(DataInputStream in, ArrayList al, ArrayList aln) {

        &lt;snip&gt;

        // read 1st vertex
        fxr = in.readFloat();
        f1x = swap(fxr);
        al.add(f1x);
        if (fminx &gt; f1x) fminx = f1x;
        if (fmaxx &lt; f1x) fmaxx = f1x;

        fyr = in.readFloat();
        f1y = swap(fyr);
        al.add(f1y);
        if (fminy &gt; f1y) fminy = f1y;
        if (fmaxy &lt; f1y) fmaxy = f1y;

        fzr = in.readFloat();
        f1z = swap(fzr);
        al.add(f1z);
        if (fminz &gt; f1z) fminz = f1z;
        if (fmaxz &lt; f1z) fmaxz = f1z;

        iVertCount++;
                    &lt;snip&gt;
</code></pre>

<p>Edit: Found a workaround but still no idea why readFloat occasionally returns NaN. I'd still be interested in an answer to that question. Now I read four bytes, bitshift them, convert that to a float and it works perfectly:</p>

<pre><code>in.readFully(fourbytes);
v1  = ((fourbytes[3]&amp;0xff) &lt;&lt; 24) | ((fourbytes[2]&amp;0xff) &lt;&lt; 16) | 
      ((fourbytes[1]&amp;0xff) &lt;&lt; 8) | ((fourbytes[0]&amp;0xff) &lt;&lt; 0);
f1x = Float.intBitsToFloat(v1);
</code></pre>

<p>And I was doing the shift later anyway to convert endianness, which is now not needed, so I guess it isn't a lot slower.</p>
