That fuck shit the fascists are using
at master 54 lines 1.1 kB view raw
1package org.tm.archive.net; 2 3import android.os.AsyncTask; 4 5import androidx.annotation.NonNull; 6import androidx.annotation.WorkerThread; 7 8import org.tm.archive.util.Util; 9 10import java.io.InputStream; 11import java.util.Optional; 12 13import okhttp3.Call; 14 15public class CallRequestController implements RequestController { 16 17 private final Call call; 18 19 private InputStream stream; 20 private boolean canceled; 21 22 public CallRequestController(@NonNull Call call) { 23 this.call = call; 24 } 25 26 @Override 27 public void cancel() { 28 AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { 29 synchronized (CallRequestController.this) { 30 if (canceled) return; 31 32 call.cancel(); 33 canceled = true; 34 } 35 }); 36 } 37 38 public synchronized void setStream(@NonNull InputStream stream) { 39 this.stream = stream; 40 notifyAll(); 41 } 42 43 /** 44 * Blocks until the stream is available or until the request is canceled. 45 */ 46 @WorkerThread 47 public synchronized Optional<InputStream> getStream() { 48 while(stream == null && !canceled) { 49 Util.wait(this, 0); 50 } 51 52 return Optional.ofNullable(this.stream); 53 } 54}