-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement request using InpuStream on Android and go
- Loading branch information
Showing
9 changed files
with
344 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
android/bridge/src/main/java/ipfs/gomobile/android/InputStreamFromGo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ipfs.gomobile.android; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
final class InputStreamFromGo extends InputStream { | ||
private final core.ReadCloser readCloser; | ||
private boolean closed; | ||
|
||
InputStreamFromGo(@NonNull core.ReadCloser readCloser) { | ||
this.readCloser = readCloser; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (closed) { | ||
throw new IOException("InputStream already closed"); | ||
} | ||
|
||
closed = true; | ||
|
||
try { | ||
readCloser.close(); | ||
} catch (Exception e) { | ||
throw new IOException(e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
byte[] b = new byte[1]; | ||
|
||
try { | ||
readCloser.read(b); | ||
} catch (Exception e) { | ||
if (e.getMessage() != null && e.getMessage().equals("EOF")) { | ||
return -1; | ||
} | ||
throw new IOException(e.getMessage()); | ||
} | ||
|
||
return b[0]; | ||
} | ||
|
||
@Override | ||
public int read(@NonNull byte[] b, int off, int len) | ||
throws IOException, IndexOutOfBoundsException { | ||
if (off < 0 || len < 0 || len > b.length - off) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
try { | ||
if (b.length == len) { | ||
return (int)readCloser.read(b); | ||
} | ||
|
||
byte[] tmp = new byte[len]; | ||
int read; | ||
|
||
read = (int)readCloser.read(tmp); | ||
System.arraycopy(tmp, 0, b, off, read); | ||
|
||
return read; | ||
} catch (Exception e) { | ||
if (e.getMessage() != null && e.getMessage().equals("EOF")) { | ||
return -1; | ||
} | ||
throw new IOException(e.getMessage()); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
android/bridge/src/main/java/ipfs/gomobile/android/InputStreamToGo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ipfs.gomobile.android; | ||
|
||
import java.io.InputStream; | ||
|
||
final class InputStreamToGo implements core.Reader { | ||
private final InputStream inputStream; | ||
|
||
InputStreamToGo(InputStream inputStream) { | ||
this.inputStream = inputStream; | ||
} | ||
|
||
public long read(byte[] p) throws Exception { | ||
long r = inputStream.read(p); | ||
|
||
if (r == -1) { | ||
inputStream.close(); // Auto-close inputStream when EOF is reached | ||
throw new Exception("EOF"); | ||
} | ||
|
||
return r; | ||
} | ||
} |
Oops, something went wrong.