Skip to content

Commit 1a80a4f

Browse files
committed
Merge pull request square#63 from square/jw/read-to-byte-array
Read methods to copy data into external byte[] sinks.
2 parents bbe184e + 239c488 commit 1a80a4f

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

okio/src/main/java/okio/Buffer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,20 @@ String readUtf8Line(long newline) {
482482
return result;
483483
}
484484

485-
/** Like {@link InputStream#read}. */
486-
int read(byte[] sink, int offset, int byteCount) {
485+
@Override public int read(byte[] sink) {
486+
return read(sink, 0, sink.length);
487+
}
488+
489+
@Override public int read(byte[] sink, long byteCount) {
490+
return read(sink, 0, byteCount);
491+
}
492+
493+
@Override public int read(byte[] sink, int offset, long byteCount) {
494+
checkOffsetAndCount(sink.length, offset, byteCount);
495+
487496
Segment s = this.head;
488497
if (s == null) return -1;
489-
int toCopy = Math.min(byteCount, s.limit - s.pos);
498+
int toCopy = Math.min((int) byteCount, s.limit - s.pos);
490499
System.arraycopy(s.data, s.pos, sink, offset, toCopy);
491500

492501
s.pos += toCopy;

okio/src/main/java/okio/BufferedSource.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ public interface BufferedSource extends Source {
8181
/** Removes {@code byteCount} bytes from this and returns them as a byte array. */
8282
byte[] readByteArray(long byteCount) throws IOException;
8383

84+
/**
85+
* Removes up to {@code sink.length} bytes from this and copies them into {@code sink}.
86+
* Returns the number of bytes read, or -1 if this source is exhausted.
87+
*/
88+
int read(byte[] sink) throws IOException;
89+
90+
/**
91+
* Removes up to {@code byteCount} bytes from this and copies them into {@code sink}.
92+
* Returns the number of bytes read, or -1 if this source is exhausted.
93+
*/
94+
int read(byte[] sink, long byteCount) throws IOException;
95+
96+
/**
97+
* Removes up to {@code byteCount} bytes from this and copies them into {@code sink} at
98+
* {@code offset}. Returns the number of bytes read, or -1 if this source is exhausted.
99+
*/
100+
int read(byte[] sink, int offset, long byteCount) throws IOException;
101+
84102
/**
85103
* Removes exactly {@code byteCount} bytes from this and appends them to
86104
* {@code sink}. Throws an {@link java.io.EOFException} if the requested

okio/src/main/java/okio/RealBufferedSource.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ public RealBufferedSource(Source source) {
9393
return buffer.readByteArray(byteCount);
9494
}
9595

96+
@Override public int read(byte[] sink) throws IOException {
97+
return read(sink, 0, sink.length);
98+
}
99+
100+
@Override public int read(byte[] sink, long byteCount) throws IOException {
101+
return read(sink, 0, byteCount);
102+
}
103+
104+
@Override public int read(byte[] sink, int offset, long byteCount) throws IOException {
105+
checkOffsetAndCount(sink.length, offset, byteCount);
106+
107+
if (buffer.size == 0) {
108+
long read = source.read(buffer, Segment.SIZE);
109+
if (read == -1) return -1;
110+
}
111+
112+
long toRead = Math.min(byteCount, buffer.size);
113+
return buffer.read(sink, offset, toRead);
114+
}
115+
96116
@Override public void readFully(Buffer sink, long byteCount) throws IOException {
97117
require(byteCount);
98118
buffer.readFully(sink, byteCount);

okio/src/test/java/okio/RealBufferedSourceTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,48 @@ public final class RealBufferedSourceTest {
276276
assertEquals("abc", source.readByteString(3).utf8());
277277
assertEquals("d", source.readUtf8(1));
278278
}
279+
280+
@Test public void readIntoByteArray() throws IOException {
281+
Buffer buffer = new Buffer().writeUtf8("abcd");
282+
BufferedSource source = Okio.buffer((Source) buffer);
283+
284+
byte[] sink = new byte[3];
285+
int read = source.read(sink);
286+
assertEquals(3, read);
287+
byte[] expected = { 'a', 'b', 'c' };
288+
assertByteArraysEquals(expected, sink);
289+
}
290+
291+
@Test public void readIntoByteArrayNotEnough() throws IOException {
292+
Buffer buffer = new Buffer().writeUtf8("abcd");
293+
BufferedSource source = Okio.buffer((Source) buffer);
294+
295+
byte[] sink = new byte[5];
296+
int read = source.read(sink);
297+
assertEquals(4, read);
298+
byte[] expected = { 'a', 'b', 'c', 'd', 0 };
299+
assertByteArraysEquals(expected, sink);
300+
}
301+
302+
@Test public void readIntoByteArrayCount() throws IOException {
303+
Buffer buffer = new Buffer().writeUtf8("abcd");
304+
BufferedSource source = Okio.buffer((Source) buffer);
305+
306+
byte[] sink = new byte[5];
307+
int read = source.read(sink, 3);
308+
assertEquals(3, read);
309+
byte[] expected = { 'a', 'b', 'c', 0, 0 };
310+
assertByteArraysEquals(expected, sink);
311+
}
312+
313+
@Test public void readIntoByteArrayOffsetAndCount() throws IOException {
314+
Buffer buffer = new Buffer().writeUtf8("abcd");
315+
BufferedSource source = Okio.buffer((Source) buffer);
316+
317+
byte[] sink = new byte[7];
318+
int read = source.read(sink, 2, 3);
319+
assertEquals(3, read);
320+
byte[] expected = { 0, 0, 'a', 'b', 'c', 0, 0 };
321+
assertByteArraysEquals(expected, sink);
322+
}
279323
}

0 commit comments

Comments
 (0)