Skip to content

Commit 59795a8

Browse files
ChunkedUploadResponseHandler: Use InputStream to parse ChunkedUploadResult
1 parent 15eaaf6 commit 59795a8

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

‎src/main/java/com/microsoft/graph/concurrency/ChunkedUploadResponseHandler.java‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package com.microsoft.graph.concurrency;
2424

25+
import com.google.common.io.ByteStreams;
26+
2527
import com.microsoft.graph.core.ClientException;
2628
import com.microsoft.graph.core.Constants;
2729
import com.microsoft.graph.http.CoreHttpProvider;
@@ -33,7 +35,11 @@
3335
import com.microsoft.graph.logger.ILogger;
3436
import com.microsoft.graph.serializer.ISerializer;
3537

38+
import org.jetbrains.annotations.NotNull;
39+
3640
import java.io.BufferedInputStream;
41+
import java.io.ByteArrayInputStream;
42+
import java.io.IOException;
3743
import java.io.InputStream;
3844
import java.util.Map;
3945

@@ -111,19 +117,7 @@ public ChunkedUploadResult<UploadType> generateResult(
111117
final String location = headers.get("Location");
112118
if (contentType != null
113119
&& contentType.contains(Constants.JSON_CONTENT_TYPE)) {
114-
try (final InputStream in = new BufferedInputStream(response.body().byteStream())) {
115-
final String rawJson = CoreHttpProvider.streamToString(in);
116-
final IUploadSession session = serializer.deserializeObject(rawJson, uploadSessionClass);
117-
118-
if (session == null || session.getNextExpectedRanges() == null) {
119-
logger.logDebug("Upload session is completed (ODSP), uploaded item returned.");
120-
final UploadType uploadedItem = serializer.deserializeObject(in, this.deserializeTypeClass);
121-
return new ChunkedUploadResult<>(uploadedItem);
122-
} else {
123-
logger.logDebug("Chunk bytes has been accepted by the server.");
124-
return new ChunkedUploadResult<>(session);
125-
}
126-
}
120+
return parseJsonUploadResult(response, serializer, logger);
127121
} else if (location != null) {
128122
logger.logDebug("Upload session is completed (Outlook), uploaded item returned.");
129123
return new ChunkedUploadResult<>(this.deserializeTypeClass.getDeclaredConstructor().newInstance());
@@ -133,4 +127,21 @@ public ChunkedUploadResult<UploadType> generateResult(
133127
}
134128
return new ChunkedUploadResult<>(new ClientException("Received an unexpected response from the service, response code: " + response.code(), null));
135129
}
130+
131+
@NotNull
132+
private ChunkedUploadResult<UploadType> parseJsonUploadResult(@NotNull Response response, @NotNull ISerializer serializer, @NotNull ILogger logger) throws IOException {
133+
try (final InputStream in = response.body().byteStream()) {
134+
final byte[] responseBytes = ByteStreams.toByteArray(in);
135+
final IUploadSession session = serializer.deserializeObject(new ByteArrayInputStream(responseBytes), uploadSessionClass);
136+
137+
if (session == null || session.getNextExpectedRanges() == null) {
138+
logger.logDebug("Upload session is completed (ODSP), uploaded item returned.");
139+
final UploadType uploadedItem = serializer.deserializeObject(new ByteArrayInputStream(responseBytes), this.deserializeTypeClass);
140+
return new ChunkedUploadResult<>(uploadedItem);
141+
} else {
142+
logger.logDebug("Chunk bytes has been accepted by the server.");
143+
return new ChunkedUploadResult<>(session);
144+
}
145+
}
146+
}
136147
}

0 commit comments

Comments
 (0)