Skip to content

Commit 98d872e

Browse files
committed
Fix casting in GridFSDownloadImpl
JAVA-2548
1 parent c55a3a4 commit 98d872e

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

driver/src/main/com/mongodb/client/gridfs/GridFSDownloadStreamImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public long skip(final long bytesToSkip) {
114114
}
115115

116116
long skippedPosition = currentPosition + bytesToSkip;
117-
bufferOffset = (int) skippedPosition % chunkSizeInBytes;
117+
bufferOffset = (int) (skippedPosition % chunkSizeInBytes);
118118
if (skippedPosition >= length) {
119119
long skipped = length - currentPosition;
120120
chunkIndex = numberOfChunks - 1;
@@ -123,7 +123,7 @@ public long skip(final long bytesToSkip) {
123123
discardCursor();
124124
return skipped;
125125
} else {
126-
int newChunkIndex = (int) Math.floor((float) skippedPosition / chunkSizeInBytes);
126+
int newChunkIndex = (int) Math.floor(skippedPosition / (double) chunkSizeInBytes);
127127
if (chunkIndex != newChunkIndex) {
128128
chunkIndex = newChunkIndex;
129129
buffer = null;
@@ -161,9 +161,9 @@ public synchronized void reset() {
161161
return;
162162
}
163163

164-
bufferOffset = (int) markPosition % chunkSizeInBytes;
164+
bufferOffset = (int) (markPosition % chunkSizeInBytes);
165165
currentPosition = markPosition;
166-
int markChunkIndex = (int) Math.floor((float) markPosition / chunkSizeInBytes);
166+
int markChunkIndex = (int) Math.floor(markPosition / (double) chunkSizeInBytes);
167167
if (markChunkIndex != chunkIndex) {
168168
chunkIndex = markChunkIndex;
169169
buffer = null;

driver/src/test/unit/com/mongodb/client/gridfs/GridFSDownloadStreamSpecification.groovy

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,19 @@ class GridFSDownloadStreamSpecification extends Specification {
169169

170170
def 'should skip to the correct point'() {
171171
given:
172-
def fileInfo = new GridFSFile(new BsonObjectId(new ObjectId()), 'filename', 60L, 25, new Date(), 'abc', new Document())
172+
def fileInfo = new GridFSFile(new BsonObjectId(new ObjectId()), 'filename', 4194297L, 32,
173+
new Date(), 'abc', new Document())
173174

174-
def firstChunkBytes = 1..25 as byte[]
175-
def thirdChunkBytes = 51 .. 60 as byte[]
175+
def firstChunkBytes = 1..32 as byte[]
176+
def lastChunkBytes = 33 .. 57 as byte[]
176177

177178
def sort = new Document('n', 1)
178179

179180
def findQueries = [new Document('files_id', fileInfo.getId()).append('n', new Document('$gte', 0)),
180-
new Document('files_id', fileInfo.getId()).append('n', new Document('$gte', 2))]
181+
new Document('files_id', fileInfo.getId()).append('n', new Document('$gte', 131071))]
181182
def chunkDocuments =
182183
[new Document('files_id', fileInfo.getId()).append('n', 0).append('data', new Binary(firstChunkBytes)),
183-
new Document('files_id', fileInfo.getId()).append('n', 2).append('data', new Binary(thirdChunkBytes))]
184+
new Document('files_id', fileInfo.getId()).append('n', 131071).append('data', new Binary(lastChunkBytes))]
184185

185186
def mongoCursor = Mock(MongoCursor)
186187
def findIterable = Mock(FindIterable)
@@ -210,10 +211,10 @@ class GridFSDownloadStreamSpecification extends Specification {
210211
readByte == [16, 17, 18, 19, 20] as byte[]
211212

212213
when:
213-
skipResult = downloadStream.skip(35)
214+
skipResult = downloadStream.skip(4194272)
214215

215216
then:
216-
skipResult == 35L
217+
skipResult == 4194272L
217218
0 * chunksCollection.find(_)
218219

219220
when:
@@ -228,7 +229,7 @@ class GridFSDownloadStreamSpecification extends Specification {
228229
1 * mongoCursor.next() >> chunkDocuments[1]
229230

230231
then:
231-
readByte == [56, 57, 58, 59, 60] as byte[]
232+
readByte == [53, 54, 55, 56, 57] as byte[]
232233

233234
when:
234235
skipResult = downloadStream.skip(1)

0 commit comments

Comments
 (0)