Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013-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.services.s3.internal.crypto;
 
 
 

Author(s):
Hanson Char
See also:
CipherLite
GCMCipherLite
 
 public class CipherLiteInputStream extends SdkFilterInputStream {
     private static final int MAX_RETRY = 1000;
     private static final int DEFAULT_IN_BUFFER_SIZE = 512;
     private CipherLite cipherLite;
    
True if this input stream is currently involved in a multipart uploads; false otherwise. For multipart uploads, the doFinal method if the underlying cipher has to be triggered via the read methods rather than the close method, since we cann't tell if closing the input stream is due to a recoverable error (in which case the cipher's doFinal method should never be called) or normal completion (where the cipher's doFinal method would need to be called if it was not a multipart upload).
 
     private final boolean multipart;
    
True if this is the last part of a multipart upload; false otherwise.
 
     private final boolean lastMultiPart;
     private boolean eof;
     private byte[] bufin;
     private byte[] bufout;
     private int curr_pos;
     private int max_pos;
 
     public CipherLiteInputStream(InputStream isCipherLite cipherLite) {
         this(iscipherLitefalsefalse);
     }
 
     public CipherLiteInputStream(InputStream isCipherLite cint buffsize) {
         this(iscbuffsizefalsefalse);
     }
 
     public CipherLiteInputStream(InputStream isCipherLite cint buffsize,
             boolean multipartboolean lastMultiPart) {
         super(is);
         if (lastMultiPart && !multipart)
             throw new IllegalArgumentException("lastMultiPart can only be true if multipart is true");
         this. = multipart;
         this. = lastMultiPart;
         this. = c;
         if (buffsize <= 0 || (buffsize % ) != 0) {
             throw new IllegalArgumentException("buffsize (" + buffsize
                     + ") must be a positive multiple of "
                     + );
         }
         this. = new byte[buffsize];
     }
 
     protected CipherLiteInputStream(InputStream is) {
         this(is.falsefalse);
     }
 
     @Override public int read() throws IOException {
         if ( >= ) {
             if ()
                 return -1;
             int count = 0;
             int len;
             do { 
                 if (count > )
                     throw new IOException("exceeded maximum number of attempts to read next chunk of data");
                 len = nextChunk();
                 count++;
             } while (len == 0);
 
             if (len == -1)
                 return -1;
         }
         return ((int[++] & 0xFF);
    };
    @Override public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }
    @Override
    public int read(byte buf[], int offint target_lenthrows IOException {
        if ( >= ) {
            // all buffered data has been read, let's get some more
            if ()
                return -1;
            int count=0;
            int len;
            do {
                if (count > )
                    throw new IOException("exceeded maximum number of attempts to read next chunk of data");
                len = nextChunk();
                count++;
            } while (len == 0);
            if (len == -1)
                return -1;
        }
        if (target_len <= 0)
            return 0;
        int len =  - ;
        if (target_len < len)
            len = target_len;
        // if buf == null, will throw NPE as intended per javadoc
        System.arraycopy(bufofflen);
         += len;
        return len;
    }
    @Override public long skip(long nthrows IOException {
        abortIfNeeded();
        int available =  - ;
        if (n > available)
            n = available;
        if (n < 0)
            return 0;
         += n;
        return n;
    }
    @Override public int available() {
        abortIfNeeded();
        return  - 
    }
    @Override public void close() throws IOException {
        .close();
        // For multipart upload the doFinal has to be triggered via the read
        // methods, since we cann't tell if the close is due to error or normal
        // completion.
        if (!) {
            if (!S3CryptoScheme.isAesGcm(.getCipherAlgorithm())) {
                try {
                    // simulate the RI: throw away the unprocessed data
                    .doFinal();
                } catch (BadPaddingException ex) {
                } catch (IllegalBlockSizeException ex) {
                }
            }
        }
         =  = 0;
        abortIfNeeded();
    }
    @Override
    public boolean markSupported() {
        abortIfNeeded();
        return .markSupported() && .markSupported();
    }
    @Override
    public void mark(int readlimit) {
        abortIfNeeded();
        .mark(readlimit);
        .mark();
    }

    

Subclassing this method would need to take care in keeping all internal states consistent. REF: TT0036173414, ISSUE-JAVA-547.

    @Override
    public void reset() throws IOException {
        abortIfNeeded();
        .reset();
        .reset();
        resetInternal();
    }
    final void resetInternal() {
         =  = 0;
         = false;
    }

    
Reads and process the next chunk of data into memory.

Returns:
the length of the data chunk read and processed, or -1 if end of stream.
Throws:
java.io.IOException if there is an IO exception from the underlying input stream
java.lang.SecurityException if there is authentication failure
    private int nextChunk() throws IOException {
        abortIfNeeded();
        if ()
            return -1;
         = null;
        int len = .read();
        if (len == -1) {
             = true;
            // Skip doFinal if it's a multi-part upload but not the last part 
            if (! || ) {
                try {
                     = .doFinal();
                    if ( == null) {
                        // bufout can be null, for example, when it was the
                        // javax.crypto.NullCipher 
                        return -1;
                    }
                     = 0;
                    return  = .;
                } catch (IllegalBlockSizeException ignore) {
                    // like the RI
                } catch (BadPaddingException e) {
                    if (S3CryptoScheme.isAesGcm(.getCipherAlgorithm()))
                        throw new SecurityException(e);
                }
            }
            return -1;
        }
         = .update(, 0, len);
         = 0;
        return  = ( == null ? 0 : .);
    }
    void renewCipherLite() {
         = .recreate();
    }
New to GrepCode? Check out our FAQ X