result.setBody(new LengthCheckInputStream(result.getBody(), Long.parseLong(response.getHttpResponse().getHeaders().get("Content-Length")), com.amazonaws.util.LengthCheckInputStream.INCLUDE_SKIPPED_BYTES));
isCurr = new LengthCheckInputStream(isCurr, plaintextLength, EXCLUDE_SKIPPED_BYTES);
is = new LengthCheckInputStream(is,
LengthCheckInputStream lcis = new LengthCheckInputStream( input,
/** * Actual number of bytes consumed is exactly what's expected, when skipped * bytes are excluded. */ @Test public void testSkipExcluded() throws IOException { byte[] bytes = new byte[100]; InputStream is = new LengthCheckInputStream( new ByteArrayInputStream(bytes), 90, EXCLUDE_SKIPPED_BYTES); assertTrue(10 == is.skip(10)); StreamUtils.consumeInputStream(is); is.close(); }
/** * Actual number of bytes consumed is exactly what's expected, when skipped * bytes are included. */ @Test public void testSkipIncluded() throws IOException { byte[] bytes = new byte[100]; InputStream is = new LengthCheckInputStream( new ByteArrayInputStream(bytes), 100, INCLUDE_SKIPPED_BYTES); assertTrue(10 == is.skip(10)); StreamUtils.consumeInputStream(is); is.close(); }
/** * Actual number of bytes consumed is more than what's expected, when * skipped bytes are included. */ @Test public void testSkipIncludedFailure() throws IOException { byte[] bytes = new byte[100]; InputStream is = new LengthCheckInputStream( new ByteArrayInputStream(bytes), 90, INCLUDE_SKIPPED_BYTES); assertTrue(10 == is.skip(10)); try { StreamUtils.consumeInputStream(is); fail(); } catch (AmazonClientException ex) { // expected } is.close(); }
/** * @param plaintextLength the expected total number of bytes of the * plaintext; or -1 if not available. */ private static InputStream getEncryptedInputStream( PutObjectRequest request, CipherFactory cipherFactory, long plaintextLength) { try { InputStream is = request.getInputStream(); if (request.getFile() != null) { // Historically file takes precedence over the original input // stream is = new RepeatableFileInputStream(request.getFile()); } if (plaintextLength > -1) { // This ensures the plain-text read from the underlying data // stream has the same length as the expected total is = new LengthCheckInputStream(is, plaintextLength, EXCLUDE_SKIPPED_BYTES); } return new RepeatableCipherInputStream(is, cipherFactory); } catch (Exception e) { throw new AmazonClientException("Unable to create cipher input stream: " + e.getMessage(), e); } }
/** * Tests if the content length set in the stream equals the bytes read from * the stream. If any exception is thrown, then the test fails. */ @Test public void testContentLength() throws Exception { InputStream in = new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)); LengthCheckInputStream rvis = new LengthCheckInputStream(in, sampleData.getBytes(StringUtils.UTF8).length, INCLUDE_SKIPPED_BYTES); try { StreamUtils.consumeInputStream(rvis); } catch (Exception e) { fail(); } rvis.close(); }
/** * Skipped more than what's expected. */ @Test public void testOverSkipped() throws IOException { byte[] bytes = new byte[100]; LengthCheckInputStream is = new LengthCheckInputStream( new ByteArrayInputStream(bytes), 99, INCLUDE_SKIPPED_BYTES); try { is.skip(100); fail(); } catch (AmazonClientException ex) { // expected } is.close(); } }
/** * Actual number of bytes consumed is less than what's expected, when * skipped bytes are excluded. */ @Test public void testSkipExcludedFailure() throws IOException { byte[] bytes = new byte[100]; LengthCheckInputStream is = new LengthCheckInputStream( new ByteArrayInputStream(bytes), 100, EXCLUDE_SKIPPED_BYTES); assertTrue(10 == is.skip(10)); try { StreamUtils.consumeInputStream(is); fail(); } catch (AmazonClientException ex) { // expected } is.close(); }
/** * This test case initiates a mark after reading 100 bytes from the stream. * Reads the next 100 bytes from the stream. Now the stream is reset and it * is drained. At the last read, the length validation must fail because the * stream was initialized with wrong expected length. */ @Test public void testMarkAndResetWithWrongExpectedLength() throws Exception { InputStream in = new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)); LengthCheckInputStream rvis = new LengthCheckInputStream(in, sampleData.getBytes(StringUtils.UTF8).length + 1, INCLUDE_SKIPPED_BYTES); rvis.read(new byte[100]); rvis.mark(100); rvis.read(new byte[100]); rvis.reset(); rvis.read(new byte[sampleData.getBytes(StringUtils.UTF8).length - 100]); try { rvis.read(); fail(); } catch (Exception e) { System.out .println("Exception occurred. Message: " + e.getMessage()); } rvis.close(); }
/** * This test case initiates a marks the stream initially, drains the whole * stream. Resets the stream and drains again. */ @Test public void testMarkAndResetAtEnd() throws Exception { InputStream in = new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)); LengthCheckInputStream rvis = new LengthCheckInputStream(in, sampleData.getBytes(StringUtils.UTF8).length, INCLUDE_SKIPPED_BYTES); rvis.mark(100); StreamUtils.consumeInputStream(rvis); rvis.reset(); StreamUtils.consumeInputStream(rvis); try { rvis.read(); } catch (Exception e) { fail(); } rvis.close(); }
/** * This test case initiates a mark at the start, reads the first 100 bytes * from the stream. Now the stream is reset and it is drained. At the last * read, the length validation must be success. */ @Test public void testMarkInitiallyAndReset() throws Exception { InputStream in = new ByteArrayInputStream(sampleData.getBytes(StringUtils.UTF8)); LengthCheckInputStream rvis = new LengthCheckInputStream(in, sampleData.getBytes(StringUtils.UTF8).length, INCLUDE_SKIPPED_BYTES); rvis.mark(100); rvis.read(new byte[100]); rvis.reset(); rvis.read(new byte[sampleData.getBytes(StringUtils.UTF8).length]); try { rvis.read(); } catch (Exception e) { fail(); } rvis.close(); StreamUtils.consumeInputStream(rvis); }
isCurr = new LengthCheckInputStream(isCurr, plaintextLength, EXCLUDE_SKIPPED_BYTES);
LengthCheckInputStream lcis = new LengthCheckInputStream( input,
input = new LengthCheckInputStream(input,
result.setBody(new LengthCheckInputStream(result.getBody(), Long.parseLong(response.getHttpResponse().getHeaders().get("Content-Length")), com.amazonaws.util.LengthCheckInputStream.INCLUDE_SKIPPED_BYTES));
/** * @param plaintextLength the expected total number of bytes of the * plaintext; or -1 if not available. */ private static InputStream getEncryptedInputStream( PutObjectRequest request, CipherFactory cipherFactory, long plaintextLength) { try { InputStream is = request.getInputStream(); if (request.getFile() != null) { // Historically file takes precedence over the original input // stream is = new RepeatableFileInputStream(request.getFile()); } if (plaintextLength > -1) { // This ensures the plain-text read from the underlying data // stream has the same length as the expected total is = new LengthCheckInputStream(is, plaintextLength, EXCLUDE_SKIPPED_BYTES); } return new RepeatableCipherInputStream(is, cipherFactory); } catch (Exception e) { throw new AmazonClientException("Unable to create cipher input stream: " + e.getMessage(), e); } }
isCurr = new LengthCheckInputStream(isCurr, plaintextLength, EXCLUDE_SKIPPED_BYTES);