| 1 |
/* |
| 2 |
* Copyright (C) 2012 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 |
|
| 17 |
package com.android.io; |
| 18 |
|
| 19 |
import com.android.annotations.NonNull; |
| 20 |
|
| 21 |
import java.io.FilterInputStream; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.InputStream; |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* <p> |
| 28 |
* Wraps an {@link InputStream} to change its closing behavior: |
| 29 |
* this makes it possible to ignore close operations or have them perform a |
| 30 |
* {@link InputStream#reset()} instead (if supported by the underlying stream) |
| 31 |
* or plain ignored. |
| 32 |
* </p> |
| 33 |
* <p> |
| 34 |
* This is useful to pass a stream to an XML parser or validator that automatically |
| 35 |
* closes its input stream -- which makes it impossible to validate a stream and then |
| 36 |
* parse it without reacquiring the stream. |
| 37 |
* </p> |
| 38 |
* <p> |
| 39 |
* Example of usage: |
| 40 |
* <pre> |
| 41 |
* // Get a stream that supports mark/reset in case the original does not |
| 42 |
* InputStream buffered = new BufferedInputStream(source); |
| 43 |
* // Mark the beginning of the stream and indicate how much buffering is acceptable |
| 44 |
* buffered.mark(500000); |
| 45 |
* // Wrap the stream so that closing it actually just resets it to the mark |
| 46 |
* NonClosingInputStream stream = new NonClosingInputStream(buffered); |
| 47 |
* stream.setCloseBehavior(CloseBehavior.RESET) |
| 48 |
* // pass it to a schema validator (pseudo code, real code is way more verbose.) |
| 49 |
* SAXParserFactory...newSAXParser().parse(stream) ... |
| 50 |
* // the validator parser closes the stream, which resets it to the beginning |
| 51 |
* // so passing it another parser will actually parse the whole stream |
| 52 |
* SAXParser...parse(stream) ... |
| 53 |
* </pre> |
| 54 |
*/ |
| 55 |
public class NonClosingInputStream extends FilterInputStream { |
| 56 |
|
| 57 |
private final InputStream mInputStream; |
| 58 |
private CloseBehavior mCloseBehavior = CloseBehavior.CLOSE; |
| 59 |
|
| 60 |
public enum CloseBehavior { |
| 61 |
/** |
| 62 |
* The behavior of {@link NonClosingInputStream#close()} is to close the |
| 63 |
* underlying input stream. This is the default. |
| 64 |
*/ |
| 65 |
CLOSE, |
| 66 |
/** |
| 67 |
* The behavior of {@link NonClosingInputStream#close()} is to ignore the |
| 68 |
* close request and do nothing. |
| 69 |
*/ |
| 70 |
IGNORE, |
| 71 |
/** |
| 72 |
* The behavior of {@link NonClosingInputStream#close()} is to call |
| 73 |
* {@link InputStream#reset()} on the underlying stream. This will |
| 74 |
* only succeed if the underlying stream supports it, e.g. it must |
| 75 |
* have {@link InputStream#markSupported()} return true <em>and</em> |
| 76 |
* the caller should have called {@link InputStream#mark(int)} at some |
| 77 |
* point before. |
| 78 |
*/ |
| 79 |
RESET |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Wraps an existing stream into this filtering stream. |
| 84 |
* @param in A non-null input stream. |
| 85 |
*/ |
| 86 |
public NonClosingInputStream(@NonNull InputStream in) { |
| 87 |
super(in); |
| 88 |
mInputStream = in; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns the current {@link CloseBehavior}. |
| 93 |
* @return the current {@link CloseBehavior}. Never null. |
| 94 |
*/ |
| 95 |
@NonNull |
| 96 |
public CloseBehavior getCloseBehavior() { |
| 97 |
return mCloseBehavior; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Changes the current {@link CloseBehavior}. |
| 102 |
* |
| 103 |
* @param closeBehavior A new non-null {@link CloseBehavior}. |
| 104 |
* @return Self for chaining. |
| 105 |
*/ |
| 106 |
public NonClosingInputStream setCloseBehavior(@NonNull CloseBehavior closeBehavior) { |
| 107 |
mCloseBehavior = closeBehavior; |
| 108 |
return this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Performs the requested {@code close()} operation, depending on the current |
| 113 |
* {@link CloseBehavior}. |
| 114 |
*/ |
| 115 |
@Override |
| 116 |
public void close() throws IOException { |
| 117 |
switch (mCloseBehavior) { |
| 118 |
case IGNORE: |
| 119 |
break; |
| 120 |
case RESET: |
| 121 |
mInputStream.reset(); |
| 122 |
break; |
| 123 |
case CLOSE: |
| 124 |
mInputStream.close(); |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
} |