|
48 | 48 | import org.junit.BeforeClass;
|
49 | 49 |
|
50 | 50 | import java.io.BufferedInputStream;
|
51 |
| -import java.io.BufferedReader; |
52 | 51 | import java.io.ByteArrayOutputStream;
|
53 | 52 | import java.io.IOException;
|
54 |
| -import java.io.InputStream; |
55 |
| -import java.io.InputStreamReader; |
56 | 53 | import java.net.InetAddress;
|
57 | 54 | import java.net.InetSocketAddress;
|
58 | 55 | import java.net.URLDecoder;
|
|
74 | 71 | import java.util.stream.Collectors;
|
75 | 72 | import java.util.zip.GZIPInputStream;
|
76 | 73 |
|
77 |
| -import static java.nio.charset.StandardCharsets.ISO_8859_1; |
78 | 74 | import static java.nio.charset.StandardCharsets.UTF_8;
|
79 | 75 | import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.CREDENTIALS_FILE_SETTING;
|
80 | 76 | import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.ENDPOINT_SETTING;
|
@@ -302,41 +298,50 @@ public void handle(final HttpExchange exchange) throws IOException {
|
302 | 298 |
|
303 | 299 | } else if (Regex.simpleMatch("POST /upload/storage/v1/b/bucket/*uploadType=multipart*", request)) {
|
304 | 300 | byte[] response = new byte[0];
|
305 |
| - try ( |
306 |
| - InputStream input = new GZIPInputStream(exchange.getRequestBody()); |
307 |
| - BufferedReader reader = new BufferedReader(new InputStreamReader(input, ISO_8859_1)) |
308 |
| - ) { |
309 |
| - String blob = null, line; |
310 |
| - while ((line = reader.readLine()) != null) { |
| 301 | + try (BufferedInputStream in = new BufferedInputStream(new GZIPInputStream(exchange.getRequestBody()))) { |
| 302 | + String blob = null; |
| 303 | + int read; |
| 304 | + while ((read = in.read()) != -1) { |
311 | 305 | boolean markAndContinue = false;
|
312 |
| - if (line.length() == 0 || line.equals("\r\n") || line.startsWith("--") |
313 |
| - || line.toLowerCase(Locale.ROOT).startsWith("content")) { |
314 |
| - markAndContinue = true; |
315 |
| - } else if (line.startsWith("{\"bucket\":\"bucket\"")) { |
316 |
| - markAndContinue = true; |
317 |
| - Matcher matcher = Pattern.compile("\"name\":\"([^\"]*)\"").matcher(line); |
318 |
| - if (matcher.find()) { |
319 |
| - blob = matcher.group(1); |
320 |
| - response = line.getBytes(UTF_8); |
| 306 | + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { |
| 307 | + do { // search next new line char and stops |
| 308 | + char current = (char) read; |
| 309 | + if (current == '\n') { |
| 310 | + break; |
| 311 | + } else if (current != '\r') { |
| 312 | + out.write(read); |
| 313 | + } |
| 314 | + } while ((read = in.read()) != -1); |
| 315 | + |
| 316 | + final String line = new String(out.toByteArray(), UTF_8); |
| 317 | + if (line.length() == 0 || line.equals("\r\n") || line.startsWith("--") |
| 318 | + || line.toLowerCase(Locale.ROOT).startsWith("content")) { |
| 319 | + markAndContinue = true; |
| 320 | + } else if (line.startsWith("{\"bucket\":\"bucket\"")) { |
| 321 | + markAndContinue = true; |
| 322 | + Matcher matcher = Pattern.compile("\"name\":\"([^\"]*)\"").matcher(line); |
| 323 | + if (matcher.find()) { |
| 324 | + blob = matcher.group(1); |
| 325 | + response = line.getBytes(UTF_8); |
| 326 | + } |
| 327 | + } |
| 328 | + if (markAndContinue) { |
| 329 | + in.mark(Integer.MAX_VALUE); |
| 330 | + continue; |
321 | 331 | }
|
322 |
| - } |
323 |
| - if (markAndContinue) { |
324 |
| - reader.mark(1); |
325 |
| - continue; |
326 | 332 | }
|
327 | 333 | if (blob != null) {
|
328 |
| - reader.reset(); |
329 |
| - try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { |
330 |
| - int c; |
331 |
| - while ((c = reader.read()) != -1) { |
332 |
| - out.write(c); // one char to one byte, because of the ISO_8859_1 encoding |
| 334 | + in.reset(); |
| 335 | + try (ByteArrayOutputStream binary = new ByteArrayOutputStream()) { |
| 336 | + while ((read = in.read()) != -1) { |
| 337 | + binary.write(read); |
333 | 338 | }
|
334 |
| - byte[] trailing = ("\r\n--__END_OF_PART__--\r\n").getBytes(ISO_8859_1); |
335 |
| - byte[] content = Arrays.copyOf(out.toByteArray(), out.toByteArray().length - trailing.length); |
336 |
| - blobs.put(blob, new BytesArray(content)); |
| 339 | + binary.flush(); |
| 340 | + byte[] tmp = binary.toByteArray(); |
| 341 | + // removes the trailing end "\r\n--__END_OF_PART__--\r\n" which is 23 bytes long |
| 342 | + blobs.put(blob, new BytesArray(Arrays.copyOf(tmp, tmp.length - 23))); |
337 | 343 | } finally {
|
338 |
| - //blob = null; |
339 |
| - reader.mark(0); |
| 344 | + blob = null; |
340 | 345 | }
|
341 | 346 | }
|
342 | 347 | }
|
|
0 commit comments