~ubuntu-branches/ubuntu/karmic/libgnuinet-java/karmic

« back to all changes in this revision

Viewing changes to source/gnu/inet/ftp/PassiveModeDTP.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-04-14 12:42:10 UTC
  • Revision ID: james.westby@ubuntu.com-20040414124210-osc3q0wzthgme27p
Tags: upstream-0.0.cvs20031116
ImportĀ upstreamĀ versionĀ 0.0.cvs20031116

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: PassiveModeDTP.java,v 1.2 2003/10/19 16:16:49 dog Exp $
 
3
 * Copyright (C) 2003 The Free Software Foundation
 
4
 * 
 
5
 * This file is part of GNU inetlib, a library.
 
6
 * 
 
7
 * GNU inetlib is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 * 
 
12
 * GNU inetlib is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 * 
 
21
 * As a special exception, if you link this library with other files to
 
22
 * produce an executable, this library does not by itself cause the
 
23
 * resulting executable to be covered by the GNU General Public License.
 
24
 * This exception does not however invalidate any other reasons why the
 
25
 * executable file might be covered by the GNU General Public License.
 
26
 */
 
27
 
 
28
package gnu.inet.ftp;
 
29
 
 
30
import java.io.InputStream;
 
31
import java.io.IOException;
 
32
import java.io.OutputStream;
 
33
import java.net.InetAddress;
 
34
import java.net.Socket;
 
35
 
 
36
/**
 
37
 * A passive mode FTP data transfer process.
 
38
 * This connects to the host specified and proxies the resulting socket's
 
39
 * input and output streams.
 
40
 *
 
41
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
 
42
 * @version $Revision: 1.2 $ $Date: 2003/10/19 16:16:49 $
 
43
 */
 
44
final class PassiveModeDTP implements DTP
 
45
{
 
46
 
 
47
  final String address;
 
48
  final int port;
 
49
  Socket socket;
 
50
  DTPInputStream in;
 
51
  DTPOutputStream out;
 
52
  boolean completed;
 
53
  boolean inProgress;
 
54
  int transferMode;
 
55
 
 
56
    PassiveModeDTP(String address, int port, InetAddress localhost)
 
57
    throws IOException
 
58
  {
 
59
    this.address = address;
 
60
    this.port = port;
 
61
    completed = false;
 
62
    inProgress = false;
 
63
    socket = new Socket(address, port, localhost, port + 1);
 
64
  }
 
65
 
 
66
        /**
 
67
         * Returns an input stream from which a remote file can be read.
 
68
         */
 
69
  public InputStream getInputStream() throws IOException
 
70
  {
 
71
    if (inProgress)
 
72
      throw new IOException("Transfer in progress");
 
73
    switch (transferMode)
 
74
    {
 
75
    case FTPConnection.MODE_STREAM:
 
76
      in = new StreamInputStream(this, socket.getInputStream());
 
77
      break;
 
78
      case FTPConnection.MODE_BLOCK:in =
 
79
        new BlockInputStream(this, socket.getInputStream());
 
80
      break;
 
81
      case FTPConnection.MODE_COMPRESSED:in =
 
82
        new CompressedInputStream(this, socket.getInputStream());
 
83
      break;
 
84
      default:throw new IllegalStateException("Invalid transfer mode");
 
85
    }
 
86
    in.setTransferComplete(false);
 
87
      return in;
 
88
  }
 
89
 
 
90
        /**
 
91
         * Returns an output stream to which a local file can be written for
 
92
         * upload.
 
93
         */
 
94
  public OutputStream getOutputStream() throws IOException
 
95
  {
 
96
    if (inProgress)
 
97
      throw new IOException("Transfer in progress");
 
98
    switch (transferMode)
 
99
    {
 
100
    case FTPConnection.MODE_STREAM:
 
101
      out = new StreamOutputStream(this, socket.getOutputStream());
 
102
      break;
 
103
      case FTPConnection.MODE_BLOCK:out =
 
104
        new BlockOutputStream(this, socket.getOutputStream());
 
105
      break;
 
106
      case FTPConnection.MODE_COMPRESSED:out =
 
107
        new CompressedOutputStream(this, socket.getOutputStream());
 
108
      break;
 
109
      default:throw new IllegalStateException("Invalid transfer mode");
 
110
    }
 
111
    out.setTransferComplete(false);
 
112
    return out;
 
113
  }
 
114
 
 
115
  public void setTransferMode(int mode)
 
116
  {
 
117
    transferMode = mode;
 
118
  }
 
119
 
 
120
  public void complete()
 
121
  {
 
122
    completed = true;
 
123
    if (!inProgress)
 
124
      transferComplete();
 
125
  }
 
126
 
 
127
  public boolean abort()
 
128
  {
 
129
    completed = true;
 
130
    transferComplete();
 
131
    return inProgress;
 
132
  }
 
133
 
 
134
  /*
 
135
   * Called by DTPInputStream or DTPOutputStream when end of
 
136
   * stream is reached.
 
137
   */
 
138
  public void transferComplete()
 
139
  {
 
140
    in.setTransferComplete(true);
 
141
    out.setTransferComplete(true);
 
142
    inProgress = false;
 
143
    completed = completed || (transferMode == FTPConnection.MODE_STREAM);
 
144
    if (completed)
 
145
    {
 
146
      try
 
147
      {
 
148
        socket.close();
 
149
      }
 
150
      catch(IOException e)
 
151
      {
 
152
      }
 
153
    }
 
154
  }
 
155
 
 
156
}