Skip to content

Commit

Permalink
Fix SlicedByteSource.sizeIfKnown() to work in the case where offset +…
Browse files Browse the repository at this point in the history
… length > Long.MAX_VALUE.

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=100584971
  • Loading branch information
cgdecker authored and cpovirk committed Aug 13, 2015
1 parent 047addc commit b1c37ad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
18 changes: 9 additions & 9 deletions guava-tests/test/com/google/common/io/ByteSourceTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ private static TestSuite suiteForBytes(ByteSourceFactory factory, byte[] bytes,
// if expected.length == 0, off has to be 0 but length doesn't matter--result will be empty
int off = expected.length == 0 ? 0 : random.nextInt(expected.length);
int len = expected.length == 0 ? 4 : random.nextInt(expected.length - off);

ByteSourceFactory sliced = SourceSinkFactories.asSlicedByteSourceFactory(factory, off, len);
suite.addTest(suiteForBytes(sliced, bytes, name + ".slice[int, int]",
suite.addTest(suiteForBytes(sliced, bytes, name + ".slice[long, long]",
desc, false));

// test a slice() of the ByteSource starting at a random offset with a length of
// Long.MAX_VALUE
ByteSourceFactory slicedLongMaxValue = SourceSinkFactories.asSlicedByteSourceFactory(
factory, off, Long.MAX_VALUE);
suite.addTest(suiteForBytes(slicedLongMaxValue, bytes, name + ".slice[long, Long.MAX_VALUE]",
desc, false));
}

Expand Down Expand Up @@ -224,14 +232,6 @@ public void testSlice_constrainedRange() throws IOException {
}
}

public void testSlice_longMaxValue() throws IOException {
long size = source.read().length;
if (size >= 1) {
ByteSource sliced = source.slice(1, Long.MAX_VALUE);
assertEquals(size - 1, sliced.read().length);
}
}

private void assertExpectedBytes(byte[] readBytes) {
assertArrayEquals(expected, readBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void tearDown() throws IOException {
}

public static ByteSourceFactory asSlicedByteSourceFactory(final ByteSourceFactory factory,
final int off, final int len) {
final long off, final long len) {
checkNotNull(factory);
return new ByteSourceFactory() {
@Override
Expand All @@ -163,7 +163,9 @@ public ByteSource createSource(byte[] bytes) throws IOException {
@Override
public byte[] getExpected(byte[] bytes) {
byte[] baseExpected = factory.getExpected(bytes);
return Arrays.copyOfRange(baseExpected, off, Math.min(baseExpected.length, off + len));
int startOffset = (int) Math.min(off, baseExpected.length);
int actualLen = (int) Math.min(len, baseExpected.length - startOffset);
return Arrays.copyOfRange(baseExpected, startOffset, startOffset + actualLen);
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions guava/src/com/google/common/io/ByteSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,11 @@ public boolean isEmpty() throws IOException {

@Override
public Optional<Long> sizeIfKnown() {
Optional<Long> unslicedSize = ByteSource.this.sizeIfKnown();
if (unslicedSize.isPresent()) {
return Optional.of(Math.min(offset + length, unslicedSize.get()) - offset);
Optional<Long> optionalUnslicedSize = ByteSource.this.sizeIfKnown();
if (optionalUnslicedSize.isPresent()) {
long unslicedSize = optionalUnslicedSize.get();
long off = Math.min(offset, unslicedSize);
return Optional.of(Math.min(length, unslicedSize - off));
}
return Optional.absent();
}
Expand Down

0 comments on commit b1c37ad

Please sign in to comment.