Skip to content

Commit fefce9f

Browse files
committed
b64 images for stetho preview
1 parent 453a7f9 commit fefce9f

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

AndroidAsyncStetho/src/com/koushikdutta/async/stetho/NetworkEventReporterWrapper.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
package com.koushikdutta.async.stetho;
44

5+
import android.util.Base64;
6+
import android.util.Base64OutputStream;
7+
58
import com.facebook.stetho.inspector.network.NetworkEventReporter;
69
import com.facebook.stetho.inspector.network.NetworkPeerManager;
710
import com.facebook.stetho.inspector.network.ResponseHandler;
@@ -10,11 +13,14 @@
1013
import com.koushikdutta.async.FilteredDataEmitter;
1114
import com.koushikdutta.async.util.StreamUtility;
1215

16+
import java.io.ByteArrayOutputStream;
1317
import java.io.FileOutputStream;
1418
import java.io.IOException;
1519
import java.io.InputStream;
1620
import java.nio.ByteBuffer;
21+
import java.nio.channels.Channels;
1722
import java.nio.channels.FileChannel;
23+
import java.nio.channels.WritableByteChannel;
1824

1925
import javax.annotation.Nullable;
2026

@@ -74,17 +80,20 @@ private NetworkPeerManager getPeerManagerIfEnabled() {
7480
return null;
7581
}
7682

77-
public DataEmitter interpretResponseEmitter(
78-
final String requestId,
79-
@Nullable DataEmitter body
80-
) {
83+
public DataEmitter interpretResponseEmitter(final String requestId, @Nullable DataEmitter body, final boolean b64Encode) {
8184
final NetworkPeerManager peerManager = getPeerManagerIfEnabled();
8285
if (peerManager == null)
8386
return null;
8487

85-
final FileChannel channel;
88+
final WritableByteChannel channel;
8689
try {
87-
channel = ((FileOutputStream)peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false)).getChannel();
90+
if (b64Encode) {
91+
final Base64OutputStream b64out = new Base64OutputStream(peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false), Base64.DEFAULT);
92+
channel = Channels.newChannel(b64out);
93+
}
94+
else {
95+
channel = ((FileOutputStream)peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false)).getChannel();
96+
}
8897
}
8998
catch (IOException e) {
9099
return null;
@@ -112,7 +121,9 @@ public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
112121
copy[i] = original[i].duplicate();
113122
}
114123
try {
115-
channel.write(copy);
124+
for (ByteBuffer c: copy) {
125+
channel.write(c);
126+
}
116127
}
117128
catch (IOException ignored) {
118129
StreamUtility.closeQuietly(channel);

AndroidAsyncStetho/src/com/koushikdutta/async/stetho/StethoMiddleware.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ public void onBodyDecoder(OnBodyDataOnRequestSentData data) {
189189
if (inspect == null)
190190
return;
191191

192-
DataEmitter emitter = eventReporter.interpretResponseEmitter(inspect.id(), data.bodyEmitter);
192+
String ct = data.response.headers().get("Content-Type");
193+
boolean isImage = ct != null && ct.startsWith("image/");
194+
DataEmitter emitter = eventReporter.interpretResponseEmitter(inspect.id(), data.bodyEmitter, isImage);
193195
if (emitter != null)
194196
data.bodyEmitter = emitter;
195197
}

0 commit comments

Comments
 (0)