1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.internal.net.www.protocol.ftp;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.net.Socket;
23
24/**
25 * This class associates a given inputStream with a control socket. This ensures
26 * the control socket Object stays live while the stream is in use
27 */
28class FtpURLInputStream extends InputStream {
29
30    private InputStream is; // Actual input stream
31
32    private Socket controlSocket;
33
34    public FtpURLInputStream(InputStream is, Socket controlSocket) {
35        this.is = is;
36        this.controlSocket = controlSocket;
37    }
38
39    @Override
40    public int read() throws IOException {
41        return is.read();
42    }
43
44    @Override
45    public int read(byte[] buf, int off, int nbytes) throws IOException {
46        return is.read(buf, off, nbytes);
47    }
48
49    @Override
50    public synchronized void reset() throws IOException {
51        is.reset();
52    }
53
54    @Override
55    public synchronized void mark(int limit) {
56        is.mark(limit);
57    }
58
59    @Override
60    public boolean markSupported() {
61        return is.markSupported();
62    }
63
64    @Override
65    public void close() {
66        try {
67            is.close();
68        } catch (Exception e) {
69            // ignored
70        }
71        try {
72            controlSocket.close();
73        } catch (Exception e) {
74            // ignored
75        }
76    }
77
78    @Override
79    public int available() throws IOException {
80        return is.available();
81    }
82
83    @Override
84    public long skip(long sbytes) throws IOException {
85        return is.skip(sbytes);
86    }
87
88}
89