Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ dependencies {
testImplementation('com.github.tomakehurst:wiremock-jre8:2.26.3')

// mockito for dependency mocking
dependencies { testImplementation "org.mockito:mockito-core:3.+" }
dependencies {
testImplementation "org.mockito:mockito-core:3.+"
testImplementation 'org.mockito:mockito-inline:2.13.0'
}


///////////////////////////////////
// Examples dependencies
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/com/nylas/ByteChannelSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.nylas;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import okio.Buffer;
import okio.Source;
import okio.Timeout;

/**
* Creates a Source around a ReadableByteChannel and efficiently reads data using an UnsafeCursor.
*
* <p>This is a basic example showing another use for the UnsafeCursor. Using the
* {@link ByteBuffer#wrap(byte[], int, int) ByteBuffer.wrap()} along with access to Buffer segments,
* a ReadableByteChannel can be given direct access to Buffer data without having to copy the data.
*/
final class ByteChannelSource implements Source {
private final ReadableByteChannel channel;
private final Timeout timeout;

private final Buffer.UnsafeCursor cursor = new Buffer.UnsafeCursor();

ByteChannelSource(ReadableByteChannel channel, Timeout timeout) {
this.channel = channel;
this.timeout = timeout;
}

@Override public long read(Buffer sink, long byteCount) throws IOException {
if (!channel.isOpen()) throw new IllegalStateException("closed");

try (Buffer.UnsafeCursor ignored = sink.readAndWriteUnsafe(cursor)) {
timeout.throwIfReached();
long oldSize = sink.size();
int length = (int) Math.min(8192, byteCount);

cursor.expandBuffer(length);
int read = channel.read(ByteBuffer.wrap(cursor.data, cursor.start, length));
if (read == -1) {
cursor.resizeBuffer(oldSize);
return -1;
} else {
cursor.resizeBuffer(oldSize + read);
return read;
}
}
}

@Override public Timeout timeout() {
return timeout;
}

@Override public void close() throws IOException {
channel.close();
}
}
Loading