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
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project// don't use FilterInputStream as the base class, we'd have to
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project// override markSupported(), mark(), and reset() to disable them
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class EofSensorInputStream extends InputStream
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    implements ConnectionReleaseTrigger {
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The wrapped input stream, while accessible.
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The value changes to <code>null</code> when the wrapped stream
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * becomes inaccessible.
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected InputStream wrappedStream;
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Indicates whether this stream itself is closed.
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * If it isn't, but {@link #wrappedStream wrappedStream}
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * is <code>null</code>, we're running in EOF mode.
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * All read operations will indicate EOF without accessing
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * the underlying stream. After closing this stream, read
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * operations will trigger an {@link IOException IOException}.
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #isReadAllowed isReadAllowed
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private boolean selfClosed;
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** The watcher to be notified, if any. */
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private EofSensorWatcher eofWatcher;
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Creates a new EOF sensor.
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * If no watcher is passed, the underlying stream will simply be
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * closed when EOF is detected or {@link #close close} is called.
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Otherwise, the watcher decides whether the underlying stream
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * should be closed before detaching from it.
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param in        the wrapped stream
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param watcher   the watcher for events, or <code>null</code> for
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  auto-close behavior without notification
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public EofSensorInputStream(final InputStream in,
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                                final EofSensorWatcher watcher) {
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (in == null) {
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                ("Wrapped stream may not be null.");
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        wrappedStream = in;
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        selfClosed = false;
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        eofWatcher = watcher;
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Checks whether the underlying stream can be read from.
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return  <code>true</code> if the underlying stream is accessible,
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          <code>false</code> if this stream is in EOF mode and
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          detached from the underlying stream
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException      if this stream is already closed
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected boolean isReadAllowed() throws IOException {
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (selfClosed) {
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IOException("Attempted read on closed stream.");
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return (wrappedStream != null);
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int read() throws IOException {
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int l = -1;
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isReadAllowed()) {
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                l = wrappedStream.read();
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkEOF(l);
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkAbort();
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw ex;
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l;
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int read(byte[] b, int off, int len) throws IOException {
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int l = -1;
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isReadAllowed()) {
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                l = wrappedStream.read(b,  off,  len);
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkEOF(l);
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkAbort();
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw ex;
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l;
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int read(byte[] b) throws IOException {
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int l = -1;
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isReadAllowed()) {
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                l = wrappedStream.read(b);
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkEOF(l);
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkAbort();
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw ex;
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l;
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public int available() throws IOException {
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        int a = 0; // not -1
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isReadAllowed()) {
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                a = wrappedStream.available();
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                // no checkEOF() here, available() can't trigger EOF
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } catch (IOException ex) {
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                checkAbort();
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw ex;
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return a;
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // non-javadoc, see base class InputStream
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    @Override
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void close() throws IOException {
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // tolerate multiple calls to close()
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        selfClosed = true;
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        checkClose();
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Detects EOF and notifies the watcher.
219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This method should only be called while the underlying stream is
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * still accessible. Use {@link #isReadAllowed isReadAllowed} to
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * check that condition.
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <br/>
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * If EOF is detected, the watcher will be notified and this stream
224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * is detached from the underlying stream. This prevents multiple
225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * notifications from this stream.
226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param eof       the result of the calling read operation.
228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  A negative value indicates that EOF is reached.
229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException
231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          in case of an IO problem on closing the underlying stream
232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected void checkEOF(int eof) throws IOException {
234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if ((wrappedStream != null) && (eof < 0)) {
236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                boolean scws = true; // should close wrapped stream?
238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (eofWatcher != null)
239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    scws = eofWatcher.eofDetected(wrappedStream);
240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (scws)
241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    wrappedStream.close();
242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } finally {
243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                wrappedStream = null;
244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Detects stream close and notifies the watcher.
251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * There's not much to detect since this is called by {@link #close close}.
252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The watcher will only be notified if this stream is closed
253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * for the first time and before EOF has been detected.
254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This stream will be detached from the underlying stream to prevent
255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * multiple notifications to the watcher.
256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException
258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          in case of an IO problem on closing the underlying stream
259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected void checkClose() throws IOException {
261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (wrappedStream != null) {
263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                boolean scws = true; // should close wrapped stream?
265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (eofWatcher != null)
266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    scws = eofWatcher.streamClosed(wrappedStream);
267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (scws)
268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    wrappedStream.close();
269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } finally {
270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                wrappedStream = null;
271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Detects stream abort and notifies the watcher.
278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * There's not much to detect since this is called by
279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * {@link #abortConnection abortConnection}.
280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The watcher will only be notified if this stream is aborted
281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * for the first time and before EOF has been detected or the
282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * stream has been {@link #close closed} gracefully.
283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This stream will be detached from the underlying stream to prevent
284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * multiple notifications to the watcher.
285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
286069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException
287069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          in case of an IO problem on closing the underlying stream
288069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
289069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected void checkAbort() throws IOException {
290069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
291069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (wrappedStream != null) {
292069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            try {
293069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                boolean scws = true; // should close wrapped stream?
294069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (eofWatcher != null)
295069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    scws = eofWatcher.streamAbort(wrappedStream);
296069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (scws)
297069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    wrappedStream.close();
298069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } finally {
299069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                wrappedStream = null;
300069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
301069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
302069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
303069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
304069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
305069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
306069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Same as {@link #close close()}.
307069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
308069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void releaseConnection() throws IOException {
309069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.close();
310069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
311069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
312069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
313069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Aborts this stream.
314069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This is a special version of {@link #close close()} which prevents
315069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * re-use of the underlying connection, if any. Calling this method
316069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * indicates that there should be no attempt to read until the end of
317069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * the stream.
318069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
319069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void abortConnection() throws IOException {
320069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // tolerate multiple calls
321069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        selfClosed = true;
322069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        checkAbort();
323069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
324069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
325069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} // class EOFSensorInputStream
326069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
327