Skip to content

Commit

Permalink
feat: implement request using InpuStream on Android and go
Browse files Browse the repository at this point in the history
  • Loading branch information
jefft0 committed Oct 25, 2022
1 parent f4f7a4d commit 2af8078
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected String doInBackground(Void... v) {
try {
fetchedData = ipfs.newRequest("cat")
.withArgument(cid)
.send();
.sendToBytes();

// Log.d(TAG, "fetched file data=" + MainActivity.bytesToHex(fetchedData));
return activity.getString(R.string.titleFetchedImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected String doInBackground(Void... v) {
String address = String.format("%s/latest/info.json", XKCDIPNS);
byte[] latestRaw = ipfs.newRequest("cat")
.withArgument(address)
.send();
.sendToBytes();

XKCDLatest = new JSONObject(new String(latestRaw)).getInt("num");
}
Expand All @@ -62,7 +62,7 @@ protected String doInBackground(Void... v) {

byte[] infoRaw = ipfs.newRequest("cat")
.withArgument(String.format("%s/%s/info.json", XKCDIPNS, formattedIndex))
.send();
.sendToBytes();
JSONObject infoJSON = new JSONObject(new String(infoRaw));

String title = infoJSON.getString("title");
Expand All @@ -73,7 +73,7 @@ protected String doInBackground(Void... v) {

fetchedData = ipfs.newRequest("cat")
.withArgument(String.format("%s/%s/image.%s", XKCDIPNS, formattedIndex, imgExt))
.send();
.sendToBytes();

return String.format(Locale.US, "%d. %s", randomIndex, title);
} catch (Exception err) {
Expand Down
3 changes: 2 additions & 1 deletion android/app/src/main/res/layout/activity_display_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@android:color/background_dark" />
tools:srcCompat="@android:color/background_dark"
android:contentDescription="XKCD Image" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.Arrays;

import static org.junit.Assert.*;

Expand All @@ -23,6 +25,15 @@
@RunWith(AndroidJUnit4.class)
public class requestIPFSTests {
private IPFS ipfs;
// This CID is the IPFS logo in the Wikipedia mirror. It should exist for a long time.
private String fileUri = "/ipfs/bafkreifxaqwd63x4bhjj33sfm3pmny2codycx27jo77it33hkexzrawyma";
private int expectedFileLength = 2940;
private byte[] expectedFileSha256 = new byte[] {
(byte)0xb7, (byte)0x04, (byte)0x2c, (byte)0x3f, (byte)0x6e, (byte)0xfc, (byte)0x09, (byte)0xd2,
(byte)0x9d, (byte)0xee, (byte)0x45, (byte)0x66, (byte)0xde, (byte)0xc6, (byte)0xe3, (byte)0x42,
(byte)0x70, (byte)0xf0, (byte)0x2b, (byte)0xeb, (byte)0xe9, (byte)0x77, (byte)0xfe, (byte)0x89,
(byte)0xef, (byte)0x67, (byte)0x51, (byte)0x2f, (byte)0x98, (byte)0x82, (byte)0xd8, (byte)0x60
};

@Rule
public Timeout globalTimeout = Timeout.seconds(600);
Expand Down Expand Up @@ -65,14 +76,46 @@ public void testDNSRequest() throws Exception {

@Test
public void testCatFile() throws Exception {
byte[] latestRaw = ipfs.newRequest("cat")
.withArgument("/ipfs/Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu")
.send();
byte[] response = ipfs.newRequest("cat")
.withArgument(fileUri)
.sendToBytes();

assertEquals(
"response should have the correct length",
12435,
latestRaw.length
expectedFileLength,
response.length
);
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
sha256.update(response);
assertTrue(
"response should have the correct SHA256",
Arrays.equals(sha256.digest(), expectedFileSha256)
);
}

@Test
public void testCatFileStream() throws Exception {
try (InputStream stream = ipfs.newRequest("cat")
.withArgument(fileUri)
.send()) {
byte[] buffer = new byte[1000];
int count = 0;
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
int n;
while ((n = stream.read(buffer)) != -1) {
count += n;
sha256.update(buffer, 0, n);
}

assertEquals(
"streamed response should have the correct length",
expectedFileLength,
count
);
assertTrue(
"response should have the correct SHA256",
Arrays.equals(sha256.digest(), expectedFileSha256)
);
}
}
}
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());
}
}
}
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;
}
}

0 comments on commit 2af8078

Please sign in to comment.