1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/EofSensorInputStream.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 672367 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-06-27 12:49:20 -0700 (Fri, 27 Jun 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  this work for additional information regarding copyright ownership.
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  (the "License"); you may not use this file except in compliance with
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  See the License for the specific language governing permissions and
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  limitations under the License.
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.conn;
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.InputStream;
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.IOException;
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * A stream wrapper that triggers actions on {@link #close close()} and EOF.
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Primarily used to auto-release an underlying
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * {@link ManagedClientConnection connection}
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * when the response body is consumed or no longer needed.
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This class is based on <code>AutoCloseInputStream</code> in HttpClient 3.1,
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * but has notable differences. It does not allow mark/reset, distinguishes
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * different kinds of event, and does not always close the underlying stream
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * on EOF. That decision is left to the {@link EofSensorWatcher watcher}.
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * </p>
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see EofSensorWatcher EofSensorWatcher
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Ortwin Glueck
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Eric Johnson
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <!-- empty lines to avoid svn diff problems -->
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Revision: 672367 $
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0
62d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *
63d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * @deprecated Please use {@link java.net.URL#openConnection} instead.
64d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
65d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     for further details.
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
67d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath@Deprecated
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project// don't use FilterInputStream as the base class, we'd have to
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project// override markSupported(), mark(), and reset() to disable them
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class EofSensorInputStream extends InputStream
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    implements ConnectionReleaseTrigger {
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The wrapped input stream, while accessible.
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The value changes to <code>null</code> when the wrapped stream
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * becomes inaccessible.
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected InputStream wrappedStream;
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Indicates whether this stream itself is closed.
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * If it isn't, but {@link #wrappedStream wrappedStream}
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * is <code>null</code>, we're running in EOF mode.
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * All read operations will indicate EOF without accessing
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * the underlying stream. After closing this stream, read
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * operations will trigger an {@link IOException IOException}.
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #isReadAllowed isReadAllowed
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private boolean selfClosed;
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The watcher to be notified, if any. */
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private EofSensorWatcher eofWatcher;
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a new EOF sensor.
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * If no watcher is passed, the underlying stream will simply be
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * closed when EOF is detected or {@link #close close} is called.
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Otherwise, the watcher decides whether the underlying stream
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * should be closed before detaching from it.
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param in        the wrapped stream
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param watcher   the watcher for events, or <code>null</code> for
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  auto-close behavior without notification
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public EofSensorInputStream(final InputStream in,
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                final EofSensorWatcher watcher) {
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (in == null) {
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Wrapped stream may not be null.");
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        wrappedStream = in;
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        selfClosed = false;
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        eofWatcher = watcher;
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Checks whether the underlying stream can be read from.
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  <code>true</code> if the underlying stream is accessible,
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          <code>false</code> if this stream is in EOF mode and
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          detached from the underlying stream
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException      if this stream is already closed
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected boolean isReadAllowed() throws IOException {
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (selfClosed) {
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IOException("Attempted read on closed stream.");
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return (wrappedStream != null);
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int read() throws IOException {
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int l = -1;
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isReadAllowed()) {
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                l = wrappedStream.read();
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkEOF(l);
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkAbort();
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw ex;
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l;
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int read(byte[] b, int off, int len) throws IOException {
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int l = -1;
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isReadAllowed()) {
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                l = wrappedStream.read(b,  off,  len);
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkEOF(l);
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkAbort();
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw ex;
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l;
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int read(byte[] b) throws IOException {
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int l = -1;
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isReadAllowed()) {
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                l = wrappedStream.read(b);
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkEOF(l);
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkAbort();
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw ex;
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l;
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int available() throws IOException {
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int a = 0; // not -1
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isReadAllowed()) {
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                a = wrappedStream.available();
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                // no checkEOF() here, available() can't trigger EOF
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkAbort();
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw ex;
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return a;
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void close() throws IOException {
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // tolerate multiple calls to close()
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        selfClosed = true;
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        checkClose();
219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Detects EOF and notifies the watcher.
224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This method should only be called while the underlying stream is
225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * still accessible. Use {@link #isReadAllowed isReadAllowed} to
226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * check that condition.
227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <br/>
228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * If EOF is detected, the watcher will be notified and this stream
229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * is detached from the underlying stream. This prevents multiple
230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * notifications from this stream.
231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param eof       the result of the calling read operation.
233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  A negative value indicates that EOF is reached.
234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException
236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          in case of an IO problem on closing the underlying stream
237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected void checkEOF(int eof) throws IOException {
239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((wrappedStream != null) && (eof < 0)) {
241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                boolean scws = true; // should close wrapped stream?
243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (eofWatcher != null)
244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    scws = eofWatcher.eofDetected(wrappedStream);
245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (scws)
246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    wrappedStream.close();
247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } finally {
248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                wrappedStream = null;
249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Detects stream close and notifies the watcher.
256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * There's not much to detect since this is called by {@link #close close}.
257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The watcher will only be notified if this stream is closed
258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * for the first time and before EOF has been detected.
259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This stream will be detached from the underlying stream to prevent
260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * multiple notifications to the watcher.
261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException
263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          in case of an IO problem on closing the underlying stream
264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected void checkClose() throws IOException {
266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (wrappedStream != null) {
268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                boolean scws = true; // should close wrapped stream?
270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (eofWatcher != null)
271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    scws = eofWatcher.streamClosed(wrappedStream);
272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (scws)
273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    wrappedStream.close();
274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } finally {
275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                wrappedStream = null;
276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Detects stream abort and notifies the watcher.
283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * There's not much to detect since this is called by
284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * {@link #abortConnection abortConnection}.
285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The watcher will only be notified if this stream is aborted
286069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * for the first time and before EOF has been detected or the
287069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * stream has been {@link #close closed} gracefully.
288069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This stream will be detached from the underlying stream to prevent
289069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * multiple notifications to the watcher.
290069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
291069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException
292069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          in case of an IO problem on closing the underlying stream
293069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
294069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected void checkAbort() throws IOException {
295069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
296069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (wrappedStream != null) {
297069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
298069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                boolean scws = true; // should close wrapped stream?
299069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (eofWatcher != null)
300069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    scws = eofWatcher.streamAbort(wrappedStream);
301069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (scws)
302069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    wrappedStream.close();
303069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } finally {
304069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                wrappedStream = null;
305069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
306069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
307069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
308069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
309069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
310069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
311069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Same as {@link #close close()}.
312069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
313069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void releaseConnection() throws IOException {
314069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.close();
315069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
316069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
317069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
318069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Aborts this stream.
319069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This is a special version of {@link #close close()} which prevents
320069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * re-use of the underlying connection, if any. Calling this method
321069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * indicates that there should be no attempt to read until the end of
322069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * the stream.
323069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
324069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void abortConnection() throws IOException {
325069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // tolerate multiple calls
326069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        selfClosed = true;
327069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        checkAbort();
328069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
329069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
330069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} // class EOFSensorInputStream
331069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
332