Skip to content

Commit 34a71cd

Browse files
committed
add checksumValidated flag to ChecksumValidatingInputStream to make sure we don't validate twice
1 parent 8f35f44 commit 34a71cd

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"type": "bugfix",
4+
"description": "Preserve computedChecksum in `ChecksumValidatingInputStream` so that it doesn't throw error if it validates more than once. See [#873](https://github.com/aws/aws-sdk-java-v2/issues/873)"
5+
}

services/s3/src/it/java/software/amazon/awssdk/services/s3/GetObjectIntegrationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import java.io.File;
2424
import java.io.IOException;
2525
import java.nio.file.Path;
26+
import java.util.Properties;
2627
import org.junit.AfterClass;
2728
import org.junit.BeforeClass;
2829
import org.junit.Test;
2930
import software.amazon.awssdk.core.ResponseInputStream;
3031
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
3132
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
33+
import software.amazon.awssdk.core.sync.RequestBody;
3234
import software.amazon.awssdk.core.sync.ResponseTransformer;
3335
import software.amazon.awssdk.services.s3.GetObjectAsyncIntegrationTest.AssertingExecutionInterceptor;
3436
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
@@ -41,6 +43,7 @@ public class GetObjectIntegrationTest extends S3IntegrationTestBase {
4143
private static final String BUCKET = temporaryBucketName(GetObjectIntegrationTest.class);
4244

4345
private static final String KEY = "some-key";
46+
private static final String PROPERTY_KEY = "properties";
4447

4548
private final GetObjectRequest getObjectRequest = GetObjectRequest.builder()
4649
.bucket(BUCKET)
@@ -71,6 +74,18 @@ public void toInputStream() throws Exception {
7174
}
7275
}
7376

77+
78+
@Test
79+
public void toInputStream_loadFromProperties() throws IOException {
80+
s3.putObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY), RequestBody.fromString("test: test"));
81+
try (ResponseInputStream<GetObjectResponse> object = s3.getObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY),
82+
ResponseTransformer.toInputStream())) {
83+
Properties properties = new Properties();
84+
properties.load(object);
85+
assertThat(properties.getProperty("test")).isEqualTo("test");
86+
}
87+
}
88+
7489
@Test
7590
public void toFile() throws Exception {
7691
Path path = RandomTempFile.randomUncreatedFile().toPath();

services/s3/src/main/java/software/amazon/awssdk/services/s3/checksums/ChecksumValidatingInputStream.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class ChecksumValidatingInputStream extends InputStream implements Aborta
3232
private long strippedLength;
3333
private byte[] streamChecksum = new byte[CHECKSUM_SIZE];
3434
private long lengthRead = 0;
35+
// Preserve the computed checksum because some InputStream readers (e.g., java.util.Properties) read more than once at the
36+
// end of the stream.
37+
private Integer computedChecksum;
3538

3639
/**
3740
* Creates an input stream using the specified Checksum, input stream, and length.
@@ -171,11 +174,14 @@ public int getStreamChecksum() {
171174

172175
private void validateAndThrow() {
173176
int streamChecksumInt = getStreamChecksum();
174-
int computedChecksumInt = ByteBuffer.wrap(checkSum.getChecksumBytes()).getInt();
175-
if (streamChecksumInt != computedChecksumInt) {
177+
if (computedChecksum == null) {
178+
computedChecksum = ByteBuffer.wrap(checkSum.getChecksumBytes()).getInt();
179+
}
180+
181+
if (streamChecksumInt != computedChecksum) {
176182
throw SdkClientException.builder().message(
177183
String.format("Data read has a different checksum than expected. Was %d, but expected %d",
178-
computedChecksumInt, streamChecksumInt)).build();
184+
computedChecksum, streamChecksumInt)).build();
179185
}
180186
}
181187

0 commit comments

Comments
 (0)