| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.infra; |
| 18 |
|
| 19 |
import static java.util.concurrent.TimeUnit.SECONDS; |
| 20 |
|
| 21 |
import android.os.AsyncTask; |
| 22 |
import android.os.ParcelFileDescriptor; |
| 23 |
|
| 24 |
import com.android.internal.util.FunctionalUtils.ThrowingConsumer; |
| 25 |
import com.android.internal.util.FunctionalUtils.ThrowingFunction; |
| 26 |
|
| 27 |
import libcore.io.IoUtils; |
| 28 |
|
| 29 |
import java.io.ByteArrayOutputStream; |
| 30 |
import java.io.Closeable; |
| 31 |
import java.io.IOException; |
| 32 |
import java.io.InputStream; |
| 33 |
import java.io.OutputStream; |
| 34 |
import java.util.concurrent.Executor; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public abstract class RemoteStream<RES, IOSTREAM extends Closeable> |
| 80 |
extends AndroidFuture<RES> |
| 81 |
implements Runnable { |
| 82 |
|
| 83 |
private final ThrowingFunction<IOSTREAM, RES> mHandleStream; |
| 84 |
private volatile ParcelFileDescriptor mLocalPipe; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static <R> AndroidFuture<R> receiveBytes( |
| 97 |
ThrowingConsumer<ParcelFileDescriptor> ipc, ThrowingFunction<InputStream, R> read) { |
| 98 |
return new RemoteStream<R, InputStream>( |
| 99 |
ipc, read, AsyncTask.THREAD_POOL_EXECUTOR, true ) { |
| 100 |
@Override |
| 101 |
protected InputStream createStream(ParcelFileDescriptor fd) { |
| 102 |
return new ParcelFileDescriptor.AutoCloseInputStream(fd); |
| 103 |
} |
| 104 |
}; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static AndroidFuture<byte[]> receiveBytes(ThrowingConsumer<ParcelFileDescriptor> ipc) { |
| 115 |
return receiveBytes(ipc, RemoteStream::readAll); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static byte[] readAll(InputStream inputStream) throws IOException { |
| 125 |
ByteArrayOutputStream combinedBuffer = new ByteArrayOutputStream(); |
| 126 |
byte[] buffer = new byte[16 * 1024]; |
| 127 |
while (true) { |
| 128 |
int numRead = inputStream.read(buffer); |
| 129 |
if (numRead == -1) { |
| 130 |
break; |
| 131 |
} |
| 132 |
combinedBuffer.write(buffer, 0, numRead); |
| 133 |
} |
| 134 |
return combinedBuffer.toByteArray(); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public static <R> AndroidFuture<R> sendBytes( |
| 148 |
ThrowingConsumer<ParcelFileDescriptor> ipc, ThrowingFunction<OutputStream, R> write) { |
| 149 |
return new RemoteStream<R, OutputStream>( |
| 150 |
ipc, write, AsyncTask.THREAD_POOL_EXECUTOR, false ) { |
| 151 |
@Override |
| 152 |
protected OutputStream createStream(ParcelFileDescriptor fd) { |
| 153 |
return new ParcelFileDescriptor.AutoCloseOutputStream(fd); |
| 154 |
} |
| 155 |
}; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public static AndroidFuture<Void> sendBytes( |
| 163 |
ThrowingConsumer<ParcelFileDescriptor> ipc, ThrowingConsumer<OutputStream> write) { |
| 164 |
return sendBytes(ipc, os -> { |
| 165 |
write.acceptOrThrow(os); |
| 166 |
return null; |
| 167 |
}); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static AndroidFuture<Void> sendBytes( |
| 175 |
ThrowingConsumer<ParcelFileDescriptor> ipc, byte[] data) { |
| 176 |
return sendBytes(ipc, os -> { |
| 177 |
os.write(data); |
| 178 |
return null; |
| 179 |
}); |
| 180 |
} |
| 181 |
|
| 182 |
private RemoteStream( |
| 183 |
ThrowingConsumer<ParcelFileDescriptor> ipc, |
| 184 |
ThrowingFunction<IOSTREAM, RES> handleStream, |
| 185 |
Executor backgroundExecutor, |
| 186 |
boolean read) { |
| 187 |
mHandleStream = handleStream; |
| 188 |
|
| 189 |
ParcelFileDescriptor[] pipe; |
| 190 |
try { |
| 191 |
|
| 192 |
pipe = ParcelFileDescriptor.createPipe(); |
| 193 |
try (ParcelFileDescriptor remotePipe = pipe[read ? 1 : 0]) { |
| 194 |
ipc.acceptOrThrow(remotePipe); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
mLocalPipe = pipe[read ? 0 : 1]; |
| 199 |
backgroundExecutor.execute(this); |
| 200 |
|
| 201 |
|
| 202 |
orTimeout(30, SECONDS); |
| 203 |
} catch (Throwable e) { |
| 204 |
completeExceptionally(e); |
| 205 |
|
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
protected abstract IOSTREAM createStream(ParcelFileDescriptor fd); |
| 210 |
|
| 211 |
@Override |
| 212 |
public void run() { |
| 213 |
try (IOSTREAM stream = createStream(mLocalPipe)) { |
| 214 |
complete(mHandleStream.applyOrThrow(stream)); |
| 215 |
} catch (Throwable t) { |
| 216 |
completeExceptionally(t); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
protected void onCompleted(RES res, Throwable err) { |
| 222 |
super.onCompleted(res, err); |
| 223 |
IoUtils.closeQuietly(mLocalPipe); |
| 224 |
} |
| 225 |
} |