Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-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.internal;
 
 import java.io.File;
 
 
A mark-and-resettable input stream that can be used on files or file input streams. In particular, a ResettableInputStream allows the close operation to be disabled via ReleasableInputStream.disableClose() (to avoid accidentally being closed). This is necessary when such input stream needs to be marked-and-reset multiple times but only as long as the stream has not been closed.

The creator of this input stream should therefore always call ReleasableInputStream.release() in a finally block to truly release the underlying resources.

See also:
Releasable
 
 public class ResettableInputStream extends ReleasableInputStream {
     private static final Log log = LogFactory
             .getLog(ResettableInputStream.class);
     private final File file// null if the file is not known
     private FileInputStream fis// never null
     private FileChannel fileChannel// never null
     
Marked position of the file; default to zero.
 
     private long markPos;

    

Parameters:
file must not be null. Upon successful construction the the file will be opened with an input stream automatically marked at the starting position of the given file.

Note the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ReleasableInputStream.disableClose(), so that the release method becomes the only way to truly close the opened file.

 
     public ResettableInputStream(File filethrows IOException {
         this(new FileInputStream(file), file);
     }

    

Parameters:
fis file input stream; must not be null. Upon successful construction the input stream will be automatically marked at the current position of the given file input stream.

Note the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ReleasableInputStream.disableClose(), so that the release method becomes the only way to truly close the opened file.

 
    public ResettableInputStream(FileInputStream fisthrows IOException {
        this(fisnull);
    }

    

Parameters:
file can be null if not known
    private ResettableInputStream(FileInputStream fisFile filethrows IOException {
        super(fis);
        this. = file;
        this. = fis;
        this. = fis.getChannel();
        this. = .position();
    }
    @Override
    public final boolean markSupported() {
        return true;
    }

    
Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes. This method works as long as the underlying file has not been closed.

Note the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ReleasableInputStream.disableClose(), so that the release method becomes the only way to truly close the opened file.

Parameters:
_ ignored
    @Override
    public void mark(int _) {
        abortIfNeeded();
        try {
             = .position();
        } catch (IOException e) {
            throw new AmazonClientException("Failed to mark the file position"e);
        }
        if (.isTraceEnabled())
            .trace("File input stream marked at position " + );
    }

    
Repositions this stream to the position at the time the mark method was last called on this input stream. This method works as long as the underlying file has not been closed.

Note the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ReleasableInputStream.disableClose(), so that the release method becomes the only way to truly close the opened file.

    @Override
    public void reset() throws IOException {
        abortIfNeeded();
        .position();
        if (.isTraceEnabled())
            .trace("Reset to position " + );
    }
    @Override
    public int available() throws IOException {
        abortIfNeeded();
        return .available();
    }
    @Override
    public int read() throws IOException {
        abortIfNeeded();
        return .read();
    }
    @Override
    public long skip(long nthrows IOException {
        abortIfNeeded();
        return .skip(n);
    }
    @Override
    public int read(byte[] arg0int arg1int arg2throws IOException {
        abortIfNeeded();
        return .read(arg0arg1arg2);
    }

    
Returns the underlying file, if known; or null if not;
    public File getFile() {
        return ;
    }

    
Convenient factory method to construct a new resettable input stream for the given file, converting any IOException into AmazonClientException.

Note the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ReleasableInputStream.disableClose(), so that the release method becomes the only way to truly close the opened file.

    public static ResettableInputStream newResettableInputStream(File file) {
        return newResettableInputStream(filenull);
    }

    
Convenient factory method to construct a new resettable input stream for the given file, converting any IOException into AmazonClientException with the given error message.

Note the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ReleasableInputStream.disableClose(), so that the release method becomes the only way to truly close the opened file.

            String errmsg) {
        try {
            return new ResettableInputStream(file);
        } catch (IOException e) {
            throw errmsg == null
                ? new AmazonClientException(e)
                : new AmazonClientException(errmsge);
        }
    }

    
Convenient factory method to construct a new resettable input stream for the given file input stream, converting any IOException into AmazonClientException.

Note the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ReleasableInputStream.disableClose(), so that the release method becomes the only way to truly close the opened file.

            FileInputStream fis) {
        return newResettableInputStream(fisnull);
    }

    
Convenient factory method to construct a new resettable input stream for the given file input stream, converting any IOException into AmazonClientException with the given error message.

Note the creation of a ResettableInputStream would entail physically opening a file. If the opened file is meant to be closed only (in a finally block) by the very same code block that created it, then it is necessary that the release method must not be called while the execution is made in other stack frames. In such case, as other stack frames may inadvertently or indirectly call the close method of the stream, the creator of the stream would need to explicitly disable the accidental closing via ReleasableInputStream.disableClose(), so that the release method becomes the only way to truly close the opened file.

            FileInputStream fisString errmsg) {
        try {
            return new ResettableInputStream(fis);
        } catch (IOException e) {
            throw new AmazonClientException(errmsge);
        }
    }
New to GrepCode? Check out our FAQ X