1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.result;
17 
18 import java.io.Closeable;
19 import java.io.InputStream;
20 
21 /**
22  * This interface basically wraps an {@link InputStream} to make it clonable.
23  *
24  * <p>It should be expected that a resource will be leaked unless {@link #cancel()} is called, and
25  * that once {@link #cancel()} is called on an instance, that that instance and any {@link
26  * InputStream}s it has created will be invalid.
27  */
28 public interface InputStreamSource extends Closeable {
29 
30     /**
31      * Return a new clone of the {@link InputStream}, so that the caller can read the stream from
32      * the beginning.  Each invocation of this method (until {@link #cancel()} is called) will
33      * return an identically-behaving {@link InputStream} -- the same contents will be returned.
34      *
35      * @return An {@link InputStream} that the caller can use to read the data source from the
36      *         beginning.  May return {@code null} if this {@code InputStreamSource} has been
37      *         invalidated by a prior call to {@link #cancel()}, or if a new InputStream cannot be
38      *         created for some other reason.
39      */
createInputStream()40     public InputStream createInputStream();
41 
42     /**
43      * Do any required cleanup on the source of the InputStream. Calling this method essentially
44      * invalidates this {@code InputStreamSource}.
45      *
46      * @deprecated use {@link #close()} instead.
47      */
48     @Deprecated
cancel()49     public default void cancel() {
50         close();
51     }
52 
53     /**
54      * Do any required cleanup on the source of the InputStream. Calling this method essentially
55      * invalidates this {@code InputStreamSource}.
56      */
57     @Override
close()58     public void close();
59 
60     /**
61      * Return the size in bytes of the source data.
62      */
size()63     public long size();
64 }
65 
66