Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
   * for applicable license terms and NOTICE.txt for applicable notices.
   *
   * 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;
 
 
 

Deprecated:
by com.amazonaws.internal.SdkBufferedInputStream. A repeatable input stream wrapper for any input stream. This input stream relies on buffered data to repeat, and can therefore only be repeated when less data has been read than this buffer can hold.

Note: Always use a com.amazonaws.internal.ResettableInputStream instead of this class if you are sourcing data from a file, as the file-based repeatable input stream can be repeated without any limitations.

 
 public class RepeatableInputStream extends SdkInputStream {
     private static final Log log = LogFactory.getLog(RepeatableInputStream.class);
 
     private InputStream is;
     private int bufferSize;
     private int bufferOffset;
     private long bytesReadPastMark;
     private byte[] buffer;
     private boolean hasWarnedBufferOverflow;

    
Creates a repeatable input stream based on another input stream.

Parameters:
inputStream The input stream to wrap. The data read from the wrapped input stream is buffered as it is read, up to the buffer limit specified.
bufferSize The number of bytes buffered by this class.
 
     public RepeatableInputStream(InputStream inputStreamint bufferSize) {
         if (inputStream == null) {
             throw new IllegalArgumentException("InputStream cannot be null");
         }
 
         this. = inputStream;
         this. = bufferSize;
         this. = new byte[this.];
 
         if (.isDebugEnabled()) {
             .debug("Underlying input stream will be repeatable up to "
                     + this..length + " bytes");
         }
     }

    
Resets the input stream to the beginning by pointing the buffer offset to the beginning of the available data buffer.

Throws:
java.io.IOException When the available buffer size has been exceeded, in which case the input stream data cannot be repeated.
 
     public void reset() throws IOException {
         abortIfNeeded();
         if ( <= ) {
             if (.isDebugEnabled()) {
                 .debug("Reset after reading " +  + " bytes.");
             }
              = 0;
         } else {
             throw new IOException(
                 "Input stream cannot be reset as " + this.
                 + " bytes have been written, exceeding the available buffer size of " + this.);
         }
     }

    
    public boolean markSupported() {
        return true;
    }

    
This method can only be used while less data has been read from the input stream than fits into the buffer. The readLimit parameter is ignored entirely.
    public void mark(int readlimit) {
        abortIfNeeded();
        if (.isDebugEnabled()) {
            .debug("Input stream marked at " +  + " bytes");
        }
        if ( <=  &&  != null) {
            /*
             * Clear buffer of already-read data to make more space. It's safe
             * to cast bytesReadPastMark to an int because it is known to be
             * less than bufferSize, which is an int.
             */
            byte[] newBuffer = new byte[this.];
            System.arraycopy(newBuffer, 0, (int)( - ));
            this. = newBuffer;
            this. -= ;
            this. = 0;
        } else {
            // If mark is called after the buffer was already exceeded, create a new buffer.
            this. = 0;
            this. = 0;
            this. = new byte[this.];
        }
    }

    
    public int available() throws IOException {
        abortIfNeeded();
        return .available();
    }

    
    public void close() throws IOException {
        .close();
        abortIfNeeded();
    }

    
    public int read(byte[] outint outOffsetint outLengththrows IOException {
        abortIfNeeded();
        // Check whether we already have buffered data.
        if ( <  &&  != null) {
            // Data is being repeated, so read from buffer instead of wrapped input stream.
            int bytesFromBuffer = outLength;
            if ( + bytesFromBuffer > ) {
                bytesFromBuffer = (int - ;
            }
            // Write to output.
            System.arraycopy(outoutOffsetbytesFromBuffer);
             += bytesFromBuffer;
            return bytesFromBuffer;
        }
        // Read data from input stream.
        int count = .read(outoutOffsetoutLength);
        if (count <= 0) {
            return count;
        }
        // Fill the buffer with data, as long as we won't exceed its capacity.
        if ( + count <= ) {
            System.arraycopy(outoutOffset, (intcount);
             += count;
        } else {
            // We have exceeded the buffer capacity, after which point it is of no use. Free the memory.
            if (! ) {
                if (.isDebugEnabled()) {
                    .debug("Buffer size " +  + " has been exceeded and the input stream "
                            + "will not be repeatable until the next mark. Freeing buffer memory");
                }
                 = true;
            }
             = null;
        }
         += count;
        return count;
    }

    
    public int read() throws IOException {
        abortIfNeeded();
        byte[] tmp = new byte[1];
        int count = read(tmp);
        if (count != -1) {
            int unsignedByte = (inttmp[0] & 0xFF;
            return unsignedByte;
        } else {
            return count;
        }
    }
    @Override
        return ;
    }
New to GrepCode? Check out our FAQ X