result.setBody(new LengthCheckInputStream(result.getBody(), Long.parseLong(response.getHttpResponse().getHeaders().get("Content-Length")), com.amazonaws.util.LengthCheckInputStream.INCLUDE_SKIPPED_BYTES));
/** * {@inheritDoc} * * @throws SdkClientException * if the data length read has exceeded the expected total, or * if the total data length is not the same as the expected * total. */ @Override public int read() throws IOException { final int c = super.read(); if (c >= 0) dataLength++; checkLength(c == -1); return c; }
/** * Checks the data length read so far against the expected total. * * @param eof * true if end of stream has been encountered; false otherwise * * @throws SdkClientException * if the data length read has exceeded the expected total, or * if the total data length is not the same as the expected * total. */ private void checkLength(boolean eof) { if (eof) { if (dataLength != expectedLength) { throw new SdkClientException( "Data read has a different length than the expected: " + diagnosticInfo()); } } else if (dataLength > expectedLength) { throw new SdkClientException("More data read than expected: " + diagnosticInfo()); } }
/** * 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); }
/** * 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(); } }
/** * 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(); }
private String diagnosticInfo() { return new StringBuilder() .append("dataLength=").append(dataLength) .append("; expectedLength=").append(expectedLength) .append("; includeSkipped=").append(includeSkipped) .append("; in.getClass()=").append(in.getClass()) .append("; markedSupported=").append(markSupported()) .append("; marked=").append(marked) .append("; resetSinceLastMarked=").append(resetSinceLastMarked) .append("; markCount=").append(markCount) .append("; resetCount=").append(resetCount) .toString(); }
/** * 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(); }
/** * 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(); }
@Override public void mark(int readlimit) { if (markSupported()) { super.mark(readlimit); marked = dataLength; markCount++; resetSinceLastMarked = false; } }
isCurr = new LengthCheckInputStream(isCurr, plaintextLength, EXCLUDE_SKIPPED_BYTES);
/** * {@inheritDoc} * * @throws SdkClientException * if the data length read has exceeded the expected total, or * if the total data length is not the same as the expected * total. */ @Override public int read(byte[] b, int off, int len) throws IOException { int readLen = super.read(b, off, len); dataLength += readLen >= 0 ? readLen : 0; checkLength(readLen == -1); return readLen; }
/** * 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(); }
@Override public void reset() throws IOException { if (markSupported()) { super.reset(); dataLength = marked; resetCount++; resetSinceLastMarked = true; } else { throw new IOException("mark/reset not supported"); } }
/** * Checks the data length read so far against the expected total. * * @param eof * true if end of stream has been encountered; false otherwise * * @throws SdkClientException * if the data length read has exceeded the expected total, or * if the total data length is not the same as the expected * total. */ private void checkLength(boolean eof) { if (eof) { if (dataLength != expectedLength) { throw new SdkClientException( "Data read has a different length than the expected: " + diagnosticInfo()); } } else if (dataLength > expectedLength) { throw new SdkClientException("More data read than expected: " + diagnosticInfo()); } }
is = new LengthCheckInputStream(is,
/** * {@inheritDoc} * * @throws SdkClientException * if {@link #includeSkipped} is true and the data length * skipped has exceeded the expected total. */ @Override public long skip(long n) throws IOException { final long skipped = super.skip(n); if (includeSkipped && skipped > 0) { dataLength += skipped; checkLength(false); } return skipped; } }
private String diagnosticInfo() { return new StringBuilder() .append("dataLength=").append(dataLength) .append("; expectedLength=").append(expectedLength) .append("; includeSkipped=").append(includeSkipped) .append("; in.getClass()=").append(in.getClass()) .append("; markedSupported=").append(markSupported()) .append("; marked=").append(marked) .append("; resetSinceLastMarked=").append(resetSinceLastMarked) .append("; markCount=").append(markCount) .append("; resetCount=").append(resetCount) .toString(); }
/** * Checks the data length read so far against the expected total. * * @param eof * true if end of stream has been encountered; false otherwise * * @throws SdkClientException * if the data length read has exceeded the expected total, or * if the total data length is not the same as the expected * total. */ private void checkLength(boolean eof) { if (eof) { if (dataLength != expectedLength) { throw new SdkClientException( "Data read has a different length than the expected: " + diagnosticInfo()); } } else if (dataLength > expectedLength) { throw new SdkClientException("More data read than expected: " + diagnosticInfo()); } }
LengthCheckInputStream lcis = new LengthCheckInputStream( input,