Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this an off-by-one bug in Java 7?
    primarykey
    data
    text
    <p>I don't know where to seek clarifications and confirmations on Java API documentation and Java code, so I'm doing it here.</p> <p>In the API documentation for <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html"><code>FileChannel</code></a>, I'm finding off-by-one errors w.r.t. to file <code>position</code> and file <code>size</code> in more places than one.</p> <p>Here's just one example. The API documenation for <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#transferFrom%28java.nio.channels.ReadableByteChannel,%20long,%20long%29"><code>transferFrom(...)</code></a> states: </p> <blockquote> <p><em>"If the given position is <strong>greater than</strong> the file's current <strong>size</strong> then no bytes are transferred."</em></p> </blockquote> <p>I confirmed that the OpenJDK code too contains this code...</p> <pre><code>public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { // ... if (position &gt; size()) return 0; // ... } </code></pre> <p>... in file <code>FileChannelImpl.java</code> consistent with the documentation.</p> <p>Now, while the above code snippet and the API documentation appear mutually consistent, I 'feel' that the above should be <strong>'greater than or equal to'</strong> and not merely <strong>'greater than'</strong> because <code>position</code> being a 0-based index into the file's data, reading at <code>position == size()</code> will have no data to return to the caller! (At <code>position == size() - 1</code>, at least 1 byte -- the last byte of the file -- could be returned to the caller.)</p> <p>Here are some other similar instances in the same API documentation page:</p> <ol> <li><p><a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#position%28long%29"><code>position(...)</code></a>: <em>"Setting the position to a value that is <strong>greater than</strong> the file's current size is legal but does not change the size of the file."</em> (Should have been 'greater than or equal to'.)</p></li> <li><p><a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#transferTo%28long,%20long,%20java.nio.channels.WritableByteChannel%29"><code>transferTo(...)</code></a>: <em>" If the given position is <strong>greater than</strong> the file's current size then no bytes are transferred."</em> (Should have been 'greater than or equal to'.)</p></li> <li><p><a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#read%28java.nio.ByteBuffer,%20long%29"><code>read(...)</code></a>: <em>"If the given position is <strong>greater than</strong> the file's current size then no bytes are read."</em> (Should have been 'greater than or equal to'.)</p></li> </ol> <p>Lastly, the documentation section for the return value of <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#read%28java.nio.ByteBuffer,%20long%29"><code>read(...)</code></a> fails to remain even self-consistent with the rest of the documentation. Here's what it states:</p> <blockquote> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#read%28java.nio.ByteBuffer,%20long%29"><code>read(...)</code></a></p> <p>Returns:</p> <p>The number of bytes read, possibly zero, or -1 if the given position is <strong>greater than or equal to</strong> the file's current size</p> </blockquote> <p>So, in this lone instance, I do see them mention the right thing. </p> <p>Overall, I don't know what to make of all of this. If I write my code today matching this documentation, then a future bug fix in Java (code or documentation) will render my code buggy, requiring a fix from my side as well. If I do the right thing myself today with things as they stand today, then my code becomes buggy to start with!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload