75
* Skips the given number of bytes by repeatedly invoking skip on
76
* the given input stream if necessary.
78
* <p>This method will only skip less than the requested number of
79
* bytes if the end of the input stream has been reached.</p>
81
* @param input stream to skip bytes in
82
* @param numToSkip the number of bytes to skip
83
* @return the number of bytes actually skipped
86
public static long skip(InputStream input, long numToSkip) throws IOException {
87
long available = numToSkip;
88
while (numToSkip > 0) {
89
long skipped = input.skip(numToSkip);
95
return (available - numToSkip);
99
* Reads as much from input as possible to fill the given array.
101
* <p>This method may invoke read repeatedly to fill the array and
102
* only read less bytes than the length of the array if the end of
103
* the stream has been reached.</p>
105
* @param input stream to read from
106
* @param b buffer to fill
107
* @return the number of bytes actually read
108
* @throws IOException
110
public static int readFully(InputStream input, byte[] b) throws IOException {
111
return readFully(input, b, 0, b.length);
115
* Reads as much from input as possible to fill the given array
116
* with the given amount of bytes.
118
* <p>This method may invoke read repeatedly to read the bytes and
119
* only read less bytes than the requested length if the end of
120
* the stream has been reached.</p>
122
* @param input stream to read from
123
* @param b buffer to fill
124
* @param offset offset into the buffer to start filling at
125
* @param len of bytes to read
126
* @return the number of bytes actually read
127
* @throws IOException
128
* if an I/O error has occurred
130
public static int readFully(InputStream input, byte[] b, int offset, int len)
132
if (len < 0 || offset < 0 || len + offset > b.length) {
133
throw new IndexOutOfBoundsException();
135
int count = 0, x = 0;
136
while (count != len) {
137
x = input.read(b, offset + count, len - count);
75
146
// toByteArray(InputStream) copied from:
76
147
// commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?revision=1428941