|
34 | 34 | import java.io.Closeable; |
35 | 35 | import java.io.DataInput; |
36 | 36 | import java.io.DataOutput; |
| 37 | +import java.io.EOFException; |
37 | 38 | import java.io.IOException; |
38 | 39 | import java.io.InputStreamReader; |
39 | 40 | import java.nio.ByteBuffer; |
@@ -74,6 +75,32 @@ public interface DataHandle<L extends Location> extends WrapperPlugin<L>, |
74 | 75 | */ |
75 | 76 | void setLength(long length) throws IOException; |
76 | 77 |
|
| 78 | + /** |
| 79 | + * Verifies that the handle has sufficient bytes available to read, returning |
| 80 | + * the actual number of bytes which will be possible to read, which might |
| 81 | + * be less than the requested value. |
| 82 | + * |
| 83 | + * @param count Number of bytes to read. |
| 84 | + * @return The actual number of bytes available to be read. |
| 85 | + * @throws IOException If something goes wrong with the check. |
| 86 | + */ |
| 87 | + default long available(final long count) throws IOException { |
| 88 | + final long remain = length() - offset(); |
| 89 | + return remain < count ? remain : count; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Ensures that the handle has sufficient bytes available to read. |
| 94 | + * |
| 95 | + * @param count Number of bytes to read. |
| 96 | + * @see #available(long) |
| 97 | + * @throws EOFException If there are insufficient bytes available. |
| 98 | + * @throws IOException If something goes wrong with the check. |
| 99 | + */ |
| 100 | + default void ensureReadable(final long count) throws IOException { |
| 101 | + if (available(count) < count) throw new EOFException(); |
| 102 | + } |
| 103 | + |
77 | 104 | /** |
78 | 105 | * Ensures that the handle has the correct length to be written to and extends |
79 | 106 | * it as required. |
|
0 commit comments