Skip to content

Commit 3f57667

Browse files
committed
Change multipart upload
1 parent cd9e502 commit 3f57667

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@
4848
import org.junit.BeforeClass;
4949

5050
import java.io.BufferedInputStream;
51-
import java.io.BufferedReader;
5251
import java.io.ByteArrayOutputStream;
5352
import java.io.IOException;
54-
import java.io.InputStream;
55-
import java.io.InputStreamReader;
5653
import java.net.InetAddress;
5754
import java.net.InetSocketAddress;
5855
import java.net.URLDecoder;
@@ -74,7 +71,6 @@
7471
import java.util.stream.Collectors;
7572
import java.util.zip.GZIPInputStream;
7673

77-
import static java.nio.charset.StandardCharsets.ISO_8859_1;
7874
import static java.nio.charset.StandardCharsets.UTF_8;
7975
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.CREDENTIALS_FILE_SETTING;
8076
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.ENDPOINT_SETTING;
@@ -302,41 +298,50 @@ public void handle(final HttpExchange exchange) throws IOException {
302298

303299
} else if (Regex.simpleMatch("POST /upload/storage/v1/b/bucket/*uploadType=multipart*", request)) {
304300
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) {
311305
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;
321331
}
322-
}
323-
if (markAndContinue) {
324-
reader.mark(1);
325-
continue;
326332
}
327333
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);
333338
}
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)));
337343
} finally {
338-
//blob = null;
339-
reader.mark(0);
344+
blob = null;
340345
}
341346
}
342347
}

0 commit comments

Comments
 (0)