Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.util;
 
 
 
Used to perform length check to ensure the number of bytes read from the underlying input stream is the same as the expected total.
 
 public class LengthCheckInputStream extends SdkFilterInputStream {
     public static final boolean INCLUDE_SKIPPED_BYTES = true;
     public static final boolean EXCLUDE_SKIPPED_BYTES = false;
    
Total number of bytes expected to be read from the underlying input stream.
 
     private final long expectedLength;
    
True if skipped bytes are to be included as part of the data length; false otherwise.
 
     private final boolean includeSkipped;
    
The length of the data read from the underlying input stream so far.
 
     private long dataLength;
     private long marked// used for mark-and-reset purposes
     
    
Used for diagnostic purposes. True if reset has been called since last marked; False otherwise.
 
     private boolean resetSinceLastMarked;
     private int markCountresetCount;

    
Constructs an input stream that performs length check to ensure the number of bytes read from the underlying input stream is the same as the expected total.

Parameters:
in the underlying input stream
expectedLength the total length of the data in bytes expected to be read from the underlying input stream; must be non-negative.
includeSkipped true if bytes skipped are to be considered as part of the data length; false otherwise. Typically, this parameter should be set to false for uploading data to AWS, but set to true for receiving data from AWS.
 
     public LengthCheckInputStream(InputStream inlong expectedLength,
             boolean includeSkipped) {
         super(in);
         if (expectedLength < 0)
             throw new IllegalArgumentException();
         this. = expectedLength;
         this. = includeSkipped;
     }

    

Throws:
com.amazonaws.AmazonClientException 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)
             ++;
         checkLength(c == -1);
         return c;
     }

    

Throws:
com.amazonaws.AmazonClientException 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[] bint offint lenthrows IOException {
        int readLen = super.read(bofflen);
         += readLen >= 0 ? readLen : 0;
        checkLength(readLen == -1);
        return readLen;
    }
    @Override
    public void mark(int readlimit) {
        if (markSupported()) {
            super.mark(readlimit);
             = ;
            ++;
             = false;
        }
    }
    @Override
    public void reset() throws IOException {
        if (markSupported()) {
            super.reset();
             = ;
            ++;
             = true;
        } else {
            throw new IOException("mark/reset not supported");
        }
    }

    
Checks the data length read so far against the expected total.

Parameters:
eof true if end of stream has been encountered; false otherwise
Throws:
com.amazonaws.AmazonClientException 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 ( != ) {
                throw new AmazonClientException(
                        "Data read has a different length than the expected: "
                                + diagnosticInfo());
            }
        } else if ( > ) {
            throw new AmazonClientException("More data read than expected: "
                    + diagnosticInfo());
        }
    }
    private String diagnosticInfo() {
        return new StringBuilder()
            .append("dataLength=").append()
            .append("; expectedLength=").append()
            .append("; includeSkipped=").append()
            .append("; in.getClass()=").append(.getClass())
            .append("; markedSupported=").append(markSupported())
            .append("; marked=").append()
            .append("; resetSinceLastMarked=").append()
            .append("; markCount=").append()
            .append("; resetCount=").append()
            .toString();
    }

    

Throws:
com.amazonaws.AmazonClientException if includeSkipped is true and the data length skipped has exceeded the expected total.
    @Override
    public long skip(long nthrows IOException {
        final long skipped = super.skip(n);
        if ( && skipped > 0) {
             += skipped;
            checkLength(false);
        }
        return skipped;
    }
New to GrepCode? Check out our FAQ X