Skip to content

Commit 2ad9271

Browse files
ffcdfNdiritu
authored andcommitted
Update LargeFileUploadTask.java
Suggested modification to the chunkInputStream method: From the second pass through this method, the code in the line "int lengthAssert = stream.read(buffer, begin, length);" throws the exception java.lang.IndexOutOfBoundsException when trying to assign the read bytes to a non-existent position in the buffer. The suggested change implies changing InputStream to FileInputStream throughout the LargeFileUploadTask class.
1 parent 5699824 commit 2ad9271

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

‎src/main/java/com/microsoft/graph/core/tasks/LargeFileUploadTask.java‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import jakarta.annotation.Nullable;
2525
import java.io.ByteArrayInputStream;
2626
import java.io.IOException;
27-
import java.io.InputStream;
27+
import java.io.FileInputStream;
2828
import java.lang.reflect.InvocationTargetException;
2929
import java.time.OffsetDateTime;
3030
import java.util.*;
3131
import java.util.concurrent.CancellationException;
3232
import java.util.concurrent.TimeUnit;
3333
import java.util.function.Consumer;
34+
import java.nio.ByteBuffer;
35+
import java.nio.channels.FileChannel;
3436

3537
/**
3638
* Task for uploading large files including pausing and resuming.
@@ -41,7 +43,7 @@ public class LargeFileUploadTask<T extends Parsable > {
4143
private static final long DEFAULT_MAX_SLICE_SIZE = (long) 5*1024*1024;
4244
private IUploadSession uploadSession;
4345
private final RequestAdapter requestAdapter;
44-
private final InputStream uploadStream;
46+
private final FileInputStream uploadStream;
4547
private final long maxSliceSize;
4648
private ArrayList<AbstractMap.SimpleEntry<Long, Long>> rangesRemaining;
4749
private final long totalUploadLength;
@@ -61,7 +63,7 @@ public class LargeFileUploadTask<T extends Parsable > {
6163
*/
6264
public LargeFileUploadTask(@Nullable final RequestAdapter requestAdapter,
6365
@Nonnull Parsable uploadSession,
64-
@Nonnull InputStream uploadStream,
66+
@Nonnull FileInputStream uploadStream,
6567
long streamSize,
6668
@Nonnull ParsableFactory<T> factory) throws IllegalAccessException, IOException, InvocationTargetException, NoSuchMethodException {
6769
this(requestAdapter, uploadSession,uploadStream, streamSize,DEFAULT_MAX_SLICE_SIZE, factory);
@@ -81,7 +83,7 @@ public LargeFileUploadTask(@Nullable final RequestAdapter requestAdapter,
8183
*/
8284
public LargeFileUploadTask(@Nullable final RequestAdapter requestAdapter,
8385
@Nonnull Parsable uploadSession,
84-
@Nonnull InputStream uploadStream,
86+
@Nonnull FileInputStream uploadStream,
8587
long streamSize,
8688
long maxSliceSize,
8789
@Nonnull ParsableFactory<T> factory) throws IllegalAccessException, IOException, InvocationTargetException, NoSuchMethodException {
@@ -275,9 +277,12 @@ private long nextSliceSize(long rangeBegin, long rangeEnd) {
275277
long size = rangeEnd - rangeBegin + 1;
276278
return Math.min(size, this.maxSliceSize);
277279
}
278-
private byte[] chunkInputStream(InputStream stream, int begin, int length) throws IOException {
279-
byte[] buffer = new byte[length];
280-
int lengthAssert = stream.read(buffer, begin, length);
280+
private byte[] chunkInputStream(FileInputStream stream, int begin, int length) throws IOException {
281+
ByteBuffer byteBuffer = ByteBuffer.allocate(length);
282+
int lengthAssert = stream.getChannel().read(byteBuffer);
283+
byteBuffer.flip();
284+
byte[] buffer = byteBuffer.array();
285+
byteBuffer.clear();
281286
assert lengthAssert == length;
282287
return buffer;
283288
}

0 commit comments

Comments
 (0)