|
19 | 19 | import java.util.Map; |
20 | 20 | import java.util.Timer; |
21 | 21 | import java.util.TimerTask; |
| 22 | +import java.util.concurrent.ExecutorService; |
| 23 | +import java.util.concurrent.Executors; |
| 24 | +import java.util.concurrent.Future; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; |
22 | 28 |
|
23 | 29 | import com.sun.net.httpserver.HttpExchange; |
24 | 30 | import com.sun.net.httpserver.HttpHandler; |
@@ -77,49 +83,51 @@ public void handle(HttpExchange t) throws IOException { |
77 | 83 | URI replayUrl = URI.create(query.get("replay_url")); |
78 | 84 | // Get the replay as a byte[] |
79 | 85 | long tStart = System.currentTimeMillis(); |
80 | | - HttpClient client = HttpClient.newHttpClient(); |
81 | | - HttpRequest request = HttpRequest.newBuilder() |
82 | | - .timeout(Duration.ofSeconds(145)) |
83 | | - .uri(replayUrl) |
84 | | - .build(); |
85 | | - HttpResponse<byte[]> response = client.send(request, |
86 | | - HttpResponse.BodyHandlers.ofByteArray()); |
| 86 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 87 | + Future<byte[]> future = executor.submit(() -> { |
| 88 | + HttpClient client = HttpClient.newHttpClient(); |
| 89 | + HttpRequest request = HttpRequest.newBuilder() |
| 90 | + .uri(replayUrl) |
| 91 | + .build(); |
| 92 | + HttpResponse<byte[]> response = client.send(request, |
| 93 | + HttpResponse.BodyHandlers.ofByteArray()); |
| 94 | + return response.body(); |
| 95 | + }); |
| 96 | + byte[] bzIn = future.get(800, TimeUnit.SECONDS); |
87 | 97 | long tEnd = System.currentTimeMillis(); |
88 | 98 | System.err.format("download: %dms\n", tEnd - tStart); |
89 | 99 |
|
90 | | - byte[] bzIn = response.body(); |
91 | 100 | byte[] bzOut = bzIn; |
92 | | - |
93 | 101 | if (replayUrl.toString().endsWith(".bz2")) { |
94 | 102 | tStart = System.currentTimeMillis(); |
95 | 103 | // Write byte[] to bunzip, get back decompressed byte[] |
96 | 104 | // The C decompressor is a bit faster than Java, 4.3 vs 4.8s |
97 | | - // BZip2CompressorInputStream bz = new BZip2CompressorInputStream(new ByteArrayInputStream(bzIn)); |
98 | | - // bzOut = bz.readAllBytes(); |
99 | | - // bz.close(); |
100 | | - |
101 | | - Process bz = new ProcessBuilder(new String[] { "bunzip2" }).start(); |
102 | | - // Start separate thread so we can consume output while sending input |
103 | | - new Thread(() -> { |
104 | | - try { |
105 | | - bz.getOutputStream().write(bzIn); |
106 | | - bz.getOutputStream().close(); |
107 | | - } catch (IOException ex) { |
108 | | - ex.printStackTrace(); |
109 | | - } |
110 | | - }).start(); |
111 | | - |
112 | | - bzOut = bz.getInputStream().readAllBytes(); |
113 | | - bz.getInputStream().close(); |
114 | | - String bzError = new String(bz.getErrorStream().readAllBytes()); |
115 | | - bz.getErrorStream().close(); |
116 | | - System.err.println(bzError); |
117 | | - if (bzError.toString().contains("bunzip2: Data integrity error when decompressing") || bzError.contains("bunzip2: Compressed file ends unexpectedly")) { |
118 | | - // Corrupted replay, don't retry |
119 | | - t.sendResponseHeaders(204, 0); |
120 | | - t.getResponseBody().close(); |
121 | | - return; |
122 | | - } |
| 105 | + BZip2CompressorInputStream bz = new BZip2CompressorInputStream(new ByteArrayInputStream(bzIn)); |
| 106 | + bzOut = bz.readAllBytes(); |
| 107 | + bz.close(); |
| 108 | + |
| 109 | + // Process bz = new ProcessBuilder(new String[] { "bunzip2" }).start(); |
| 110 | + // // Start separate thread so we can consume output while sending input |
| 111 | + // new Thread(() -> { |
| 112 | + // try { |
| 113 | + // bz.getOutputStream().write(bzIn); |
| 114 | + // bz.getOutputStream().close(); |
| 115 | + // } catch (IOException ex) { |
| 116 | + // ex.printStackTrace(); |
| 117 | + // } |
| 118 | + // }).start(); |
| 119 | + |
| 120 | + // bzOut = bz.getInputStream().readAllBytes(); |
| 121 | + // bz.getInputStream().close(); |
| 122 | + // String bzError = new String(bz.getErrorStream().readAllBytes()); |
| 123 | + // bz.getErrorStream().close(); |
| 124 | + // System.err.println(bzError); |
| 125 | + // if (bzError.toString().contains("bunzip2: Data integrity error when decompressing") || bzError.contains("bunzip2: Compressed file ends unexpectedly")) { |
| 126 | + // // Corrupted replay, don't retry |
| 127 | + // t.sendResponseHeaders(204, 0); |
| 128 | + // t.getResponseBody().close(); |
| 129 | + // return; |
| 130 | + // } |
123 | 131 | tEnd = System.currentTimeMillis(); |
124 | 132 | System.err.format("bunzip2: %dms\n", tEnd - tStart); |
125 | 133 | } |
|
0 commit comments