<p>The <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInput.html#skipBytes(int)" rel="noreferrer">Sun Documentation for DataInput.skipBytes</a> states that it "makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes. However, it may skip over some smaller number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility."</p>

<ol>
<li><p>Other than reaching end of file, why might <code>skipBytes()</code> not skip the right number of bytes? (The <code>DataInputStream</code> I am using will either be wrapping a <code>FileInputStream</code> or a <code>PipedInputStream</code>.)</p></li>
<li><p>If I definitely want to skip n bytes and throw an <code>EOFException</code> if this causes me to go to the end of the file, should I use <code>readFully()</code> and ignore the resulting byte array? Or is there a better way?</p></li>
</ol>

[{'answer_id': 51393, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 4, 'selected': True, 'text': "<p>1) There might not be that much data available to read (the other end of the pipe might not have sent that much data yet), and the implementing class might be non-blocking (i.e. it will just return what it can, rather than waiting for enough data to fulfil the request).</p>\n\n<p>I don't know if any implementations actually behave in this way, however, but the interface is designed to permit it.</p>\n\n<p>Another option is simply that the file gets closed part-way through the read.</p>\n\n<p>2) Either readFully() (which will always wait for enough input or else fail) or call skipBytes() in a loop.  I think the former is probably better, unless the array is truly vast.</p>\n"}, {'answer_id': 51613, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 1, 'selected': False, 'text': '<p>Josh Bloch has publicised this recently. It is consistent in that InputStream.read is not guaranteed to read as many bytes as it could. However, it is utterly pointless as an API method. InputStream should probably also have readFully.</p>\n'}, {'answer_id': 51828, 'author': 'Simon Nickerson', 'author_id': 4728, 'author_profile': 'https://Stackoverflow.com/users/4728', 'pm_score': 2, 'selected': False, 'text': '<p>It turns out that readFully() adds more performance overhead than I was willing to put up with.</p>\n\n<p>In the end I compromised: I call skipBytes() once, and if that returns fewer than the right number of bytes, I call readFully() for the remaining bytes.</p>\n'}, {'answer_id': 3977877, 'author': 'Will', 'author_id': 328178, 'author_profile': 'https://Stackoverflow.com/users/328178', 'pm_score': 2, 'selected': False, 'text': '<p>I came across this problem today.  It was reading off a network connection on a virtual machine so I imagine there could be a number of reasons for this happening. I solved it by simply forcing the input stream to skip bytes until it had skipped the number of bytes I wanted it to:</p>\n\n<pre><code>int byteOffsetX = someNumber; //n bytes to skip\nint nSkipped = 0;\n\nnSkipped = in.skipBytes(byteOffsetX);\nwhile (nSkipped &lt; byteOffsetX) {\n    nSkipped = nSkipped + in.skipBytes(byteOffsetX - nSkipped);\n}\n</code></pre>\n'}, {'answer_id': 61251234, 'author': 'Sergei Tachenov', 'author_id': 540312, 'author_profile': 'https://Stackoverflow.com/users/540312', 'pm_score': 0, 'selected': False, 'text': "<p>According to the docs, <code>readFully()</code> is the only way that both works and <em>guaranteed</em> to work.</p>\n\n<p>The actual Oracle implementation is... confusing:</p>\n\n<pre><code>public final int skipBytes(int n) throws IOException {\n    int total = 0;\n    int cur = 0;\n\n    while ((total&lt;n) &amp;&amp; ((cur = (int) in.skip(n-total)) &gt; 0)) {\n        total += cur;\n    }\n\n    return total;\n}\n</code></pre>\n\n<p>Why call <code>skip()</code> in a loop when <code>skipBytes()</code> has essentially the same contract as <code>skip()</code>? It would make perfect sense to implement it this way if both of the following were true: <code>skipBytes()</code> guaranteed to skip less than requested <em>only</em> on EOF, <em>and</em> <code>skip()</code> guaranteed to skip at least one byte if at all possible (just like <code>read()</code> does).</p>\n\n<p>What's even worse, is that <code>skip()</code> is actually implemented using <code>read()</code>, which means it actually <em>does</em> the job. It just doesn't <em>promise</em> to do it, which means other implementations may fail to do so (and even Oracle one may potentially fail in the future if changed in future releases).</p>\n\n<p>To be completely safe: call <code>readBytes()</code>, and if it doesn't do the job, allocate a temp buffer and call <code>readFully()</code> (use a loop here if the ability to skip over arbitrarily large amounts of data is needed).</p>\n\n<p>On the other hand, calling <code>skipBytes()</code> in a loop is pointless. At least with Oracle implementation, because it's already a loop.</p>\n"}]
['https://Stackoverflow.com/questions/51380', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4728/']