|
2 | 2 |
|
3 | 3 | package com.koushikdutta.async.stetho; |
4 | 4 |
|
| 5 | +import android.util.Base64; |
| 6 | +import android.util.Base64OutputStream; |
| 7 | + |
5 | 8 | import com.facebook.stetho.inspector.network.NetworkEventReporter; |
6 | 9 | import com.facebook.stetho.inspector.network.NetworkPeerManager; |
7 | 10 | import com.facebook.stetho.inspector.network.ResponseHandler; |
|
10 | 13 | import com.koushikdutta.async.FilteredDataEmitter; |
11 | 14 | import com.koushikdutta.async.util.StreamUtility; |
12 | 15 |
|
| 16 | +import java.io.ByteArrayOutputStream; |
13 | 17 | import java.io.FileOutputStream; |
14 | 18 | import java.io.IOException; |
15 | 19 | import java.io.InputStream; |
16 | 20 | import java.nio.ByteBuffer; |
| 21 | +import java.nio.channels.Channels; |
17 | 22 | import java.nio.channels.FileChannel; |
| 23 | +import java.nio.channels.WritableByteChannel; |
18 | 24 |
|
19 | 25 | import javax.annotation.Nullable; |
20 | 26 |
|
@@ -74,17 +80,20 @@ private NetworkPeerManager getPeerManagerIfEnabled() { |
74 | 80 | return null; |
75 | 81 | } |
76 | 82 |
|
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) { |
81 | 84 | final NetworkPeerManager peerManager = getPeerManagerIfEnabled(); |
82 | 85 | if (peerManager == null) |
83 | 86 | return null; |
84 | 87 |
|
85 | | - final FileChannel channel; |
| 88 | + final WritableByteChannel channel; |
86 | 89 | 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 | + } |
88 | 97 | } |
89 | 98 | catch (IOException e) { |
90 | 99 | return null; |
@@ -112,7 +121,9 @@ public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) { |
112 | 121 | copy[i] = original[i].duplicate(); |
113 | 122 | } |
114 | 123 | try { |
115 | | - channel.write(copy); |
| 124 | + for (ByteBuffer c: copy) { |
| 125 | + channel.write(c); |
| 126 | + } |
116 | 127 | } |
117 | 128 | catch (IOException ignored) { |
118 | 129 | StreamUtility.closeQuietly(channel); |
|
0 commit comments