Example usage for com.amazonaws SignableRequest getReadLimitInfo

List of usage examples for com.amazonaws SignableRequest getReadLimitInfo

Introduction

In this page you can find the example usage for com.amazonaws SignableRequest getReadLimitInfo.

Prototype

ReadLimitInfo getReadLimitInfo();

Source Link

Document

Returns the read limit info about the original request.

Usage

From source file:com.ibm.og.s3.v4.AWSS3V4Signer.java

License:Open Source License

/**
 * Read the content of the request to get the length of the stream. This method will wrap the
 * stream by SdkBufferedInputStream if it is not mark-supported.
 *///from w ww. ja  v  a  2 s .c o  m
static long getContentLength(final SignableRequest<?> request) throws IOException {
    final InputStream content = request.getContent();
    if (!content.markSupported()) {
        throw new IllegalStateException(
                "Bug: request input stream must have been made mark-and-resettable at this point");
    }
    final ReadLimitInfo info = request.getReadLimitInfo();
    final int readLimit = info.getReadLimit();
    long contentLength = 0;
    final byte[] tmp = new byte[4096];
    int read;
    content.mark(readLimit);
    while ((read = content.read(tmp)) != -1) {
        contentLength += read;
    }
    try {
        content.reset();
    } catch (final IOException ex) {
        throw new ResetException("Failed to reset the input stream", ex);
    }
    return contentLength;
}