use of okio.Source in project PocketHub by pockethub.
the class ImageBinPoster method post.
/**
* Post the image to ImageBin
*
* @param context A context
* @param uri The content URI
* @param callback Request callback
* @return If the file was successfully retrieved
*/
public static boolean post(Context context, Uri uri, Callback callback) {
byte[] bytes = null;
try {
InputStream stream = context.getContentResolver().openInputStream(uri);
if (stream != null) {
Source source = Okio.source(stream);
bytes = Okio.buffer(source).readByteArray();
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
post(bytes, callback);
return true;
}
use of okio.Source in project okhttp by square.
the class InMemoryFileSystem method source.
@Override
public Source source(File file) throws FileNotFoundException {
Buffer result = files.get(file);
if (result == null)
throw new FileNotFoundException();
final Source source = result.clone();
openSources.put(source, file);
return new ForwardingSource(source) {
@Override
public void close() throws IOException {
openSources.remove(source);
super.close();
}
};
}
use of okio.Source in project okhttp by square.
the class RequestBody method create.
/** Returns a new request body that transmits the content of {@code file}. */
public static RequestBody create(final MediaType contentType, final File file) {
if (file == null)
throw new NullPointerException("content == null");
return new RequestBody() {
@Override
public MediaType contentType() {
return contentType;
}
@Override
public long contentLength() {
return file.length();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
Source source = null;
try {
source = Okio.source(file);
sink.writeAll(source);
} finally {
Util.closeQuietly(source);
}
}
};
}
use of okio.Source in project okhttp by square.
the class CacheInterceptor method cacheWritingResponse.
/**
* Returns a new source that writes bytes to {@code cacheRequest} as they are read by the source
* consumer. This is careful to discard bytes left over when the stream is closed; otherwise we
* may never exhaust the source stream and therefore not complete the cached response.
*/
private Response cacheWritingResponse(final CacheRequest cacheRequest, Response response) throws IOException {
// Some apps return a null body; for compatibility we treat that like a null cache request.
if (cacheRequest == null)
return response;
Sink cacheBodyUnbuffered = cacheRequest.body();
if (cacheBodyUnbuffered == null)
return response;
final BufferedSource source = response.body().source();
final BufferedSink cacheBody = Okio.buffer(cacheBodyUnbuffered);
Source cacheWritingSource = new Source() {
boolean cacheRequestClosed;
@Override
public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead;
try {
bytesRead = source.read(sink, byteCount);
} catch (IOException e) {
if (!cacheRequestClosed) {
cacheRequestClosed = true;
// Failed to write a complete cache response.
cacheRequest.abort();
}
throw e;
}
if (bytesRead == -1) {
if (!cacheRequestClosed) {
cacheRequestClosed = true;
// The cache response is complete!
cacheBody.close();
}
return -1;
}
sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead);
cacheBody.emitCompleteSegments();
return bytesRead;
}
@Override
public Timeout timeout() {
return source.timeout();
}
@Override
public void close() throws IOException {
if (!cacheRequestClosed && !discard(this, HttpCodec.DISCARD_STREAM_TIMEOUT_MILLIS, MILLISECONDS)) {
cacheRequestClosed = true;
cacheRequest.abort();
}
source.close();
}
};
return response.newBuilder().body(new RealResponseBody(response.headers(), Okio.buffer(cacheWritingSource))).build();
}
use of okio.Source in project okhttp by square.
the class Http2ConnectionTest method readSendsWindowUpdate.
@Test
public void readSendsWindowUpdate() throws Exception {
int windowSize = 100;
int windowUpdateThreshold = 50;
// Write the mocking script.
peer.sendFrame().settings(new Settings());
// ACK
peer.acceptFrame();
// SYN_STREAM
peer.acceptFrame();
peer.sendFrame().synReply(false, 3, headerEntries("a", "android"));
for (int i = 0; i < 3; i++) {
// Send frames of summing to size 50, which is windowUpdateThreshold.
peer.sendFrame().data(false, 3, data(24), 24);
peer.sendFrame().data(false, 3, data(25), 25);
peer.sendFrame().data(false, 3, data(1), 1);
// connection WINDOW UPDATE
peer.acceptFrame();
// stream WINDOW UPDATE
peer.acceptFrame();
}
peer.sendFrame().data(true, 3, data(0), 0);
peer.play();
// Play it back.
Http2Connection connection = connect(peer);
connection.okHttpSettings.set(INITIAL_WINDOW_SIZE, windowSize);
Http2Stream stream = connection.newStream(headerEntries("b", "banana"), false);
assertEquals(0, stream.unacknowledgedBytesRead);
assertEquals(headerEntries("a", "android"), stream.takeResponseHeaders());
Source in = stream.getSource();
Buffer buffer = new Buffer();
buffer.writeAll(in);
assertEquals(-1, in.read(buffer, 1));
assertEquals(150, buffer.size());
InFrame synStream = peer.takeFrame();
assertEquals(Http2.TYPE_HEADERS, synStream.type);
for (int i = 0; i < 3; i++) {
List<Integer> windowUpdateStreamIds = new ArrayList<>(2);
for (int j = 0; j < 2; j++) {
InFrame windowUpdate = peer.takeFrame();
assertEquals(Http2.TYPE_WINDOW_UPDATE, windowUpdate.type);
windowUpdateStreamIds.add(windowUpdate.streamId);
assertEquals(windowUpdateThreshold, windowUpdate.windowSizeIncrement);
}
// connection
assertTrue(windowUpdateStreamIds.contains(0));
// stream
assertTrue(windowUpdateStreamIds.contains(3));
}
}
Aggregations