Skip to content
Closed
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
60 changes: 57 additions & 3 deletions vtm/src/org/oscim/tiling/source/OkHttpEngine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright 2014 Charles Greb
* Copyright 2014 Hannes Janetzek
* Copyright 2016 Andrey Novikov
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
Expand Down Expand Up @@ -57,7 +58,7 @@ public HttpEngine create(UrlTileSource tileSource) {
}
}

private InputStream inputStream;
private InputOutputStream inputStream;

public OkHttpEngine(OkHttpClient client, UrlTileSource tileSource) {
mClient = client;
Expand All @@ -81,7 +82,7 @@ public void sendRequest(Tile tile) throws IOException {
conn.addRequestProperty(opt.getKey(), opt.getValue());

try {
inputStream = conn.getInputStream();
inputStream = new InputOutputStream(conn.getInputStream());
} catch (FileNotFoundException e) {
throw new IOException("ERROR " + conn.getResponseCode()
+ ": " + conn.getResponseMessage());
Expand All @@ -105,7 +106,7 @@ public void run() {

@Override
public void setCache(OutputStream os) {
// OkHttp cache implented through tileSource setResponseCache
inputStream.setOutputStream(os);
}

@Override
Expand All @@ -115,4 +116,57 @@ public boolean requestCompleted(boolean success) {

return success;
}

class InputOutputStream extends InputStream {
InputStream mInputStream;
Copy link
Collaborator

@devemux86 devemux86 Aug 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the internal InputStream here since we already extend it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's a kind of proxy. Do you wish to use super instead?

Copy link
Collaborator

@devemux86 devemux86 Aug 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't use internally a different InputStream type than the extended one, we could simplify the implementation?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we could make the inner class static?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll test it later.

OutputStream mOutputStream;

InputOutputStream(InputStream inputStream) {
mInputStream = inputStream;
}

public void setOutputStream(OutputStream outputStream) {
mOutputStream = outputStream;
}

@Override
public int read() throws IOException {
int data = mInputStream.read();
if (mOutputStream != null)
mOutputStream.write(data);
return data;
}

@Override
public int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
int num = mInputStream.read(b, off, len);
if (mOutputStream != null)
mOutputStream.write(b, off, num);
return num;
}

@Override
public long skip(long len) throws IOException {
return mInputStream.skip(len);
}

@Override
public void close() throws IOException {
mInputStream.close();
if (mOutputStream != null) {
mOutputStream.flush();
mOutputStream.close();
}
}

@Override
public synchronized void reset() throws IOException {
mInputStream.reset();
}
}
}