FtpURLInputStream.java revision 5aafac4db69e6d087c512cdfa5c7c0e2f1611681
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 libcore.net.url;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.net.Socket;
23import libcore.io.IoUtils;
24
25/**
26 * This class associates a given inputStream with a control socket. This ensures
27 * the control socket Object stays live while the stream is in use
28 */
29class FtpURLInputStream extends InputStream {
30
31    private InputStream is; // Actual input stream
32
33    private Socket controlSocket;
34
35    public FtpURLInputStream(InputStream is, Socket controlSocket) {
36        this.is = is;
37        this.controlSocket = controlSocket;
38    }
39
40    @Override
41    public int read() throws IOException {
42        return is.read();
43    }
44
45    @Override
46    public int read(byte[] buf, int off, int nbytes) throws IOException {
47        return is.read(buf, off, nbytes);
48    }
49
50    @Override
51    public synchronized void reset() throws IOException {
52        is.reset();
53    }
54
55    @Override
56    public synchronized void mark(int limit) {
57        is.mark(limit);
58    }
59
60    @Override
61    public boolean markSupported() {
62        return is.markSupported();
63    }
64
65    @Override
66    public void close() {
67        IoUtils.closeQuietly(is);
68        IoUtils.closeQuietly(controlSocket);
69    }
70
71    @Override
72    public int available() throws IOException {
73        return is.available();
74    }
75
76    @Override
77    public long skip(long byteCount) throws IOException {
78        return is.skip(byteCount);
79    }
80
81}
82