-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathData.java
More file actions
224 lines (200 loc) · 5.15 KB
/
Data.java
File metadata and controls
224 lines (200 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* This file is part of the QuickServer library
* Copyright (C) 2003-2005 QuickServer.org
*
* Use, modification, copying and distribution of this software is subject to
* the terms and conditions of the GNU Lesser General Public License.
* You should have received a copy of the GNU LGP License along with this
* library; if not, you can download a copy from <http://www.quickserver.org/>.
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* visit http://www.quickserver.org
*
*/
package ftpserver;
import org.quickserver.net.*;
import org.quickserver.net.server.*;
import org.quickserver.util.pool.PoolableObject;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.PoolableObjectFactory;
import java.io.*;
import java.net.*;
import java.util.logging.Logger;
public class Data implements ClientData, PoolableObject {
private static Logger logger =
Logger.getLogger(Data.class.getName());
boolean isTransferring = false;
boolean isRenameFrom = false;
boolean isStop = false;
boolean isPassive = false;
Socket socket = null;
ServerSocket server = null; //if passive
int socketPort = -1;
int serverPort = -1;
boolean closeDataServer = true;
String ip = null;
//pipelining
boolean wasQuit = false;
boolean isProcessing = false;
int noOfBytesTransferred = -1;
int startPosition = 0; //for REST
String root = null; //no ending slash \
String wDir = "/"; //WORKING DIRECTORY
String account = null; //ACCOUNT
String username = ""; //USERNAME
boolean binary = false;
char type = 'A'; //ASCII,IMAGE
char typeSub = 'N'; //Non-print,
String mode = "Stream"; //Stream
String structure = "File"; //File, Record
public void sendFile(String file) throws Exception {
isStop = false;
if(socket==null)
throw new IOException("No client connected");
String input = "";
int i=0;
FileInputStream fin = null;
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(socket.getOutputStream());
fin = new FileInputStream(file);
/*
if(data.type == 'A') {
PrintWriter rout=new PrintWriter(out);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
while(true) {
input = br.readLine();
if(input==null)
break;
rout.
}
rout.flush();
//rout.close();
} else {
}*/
while(true) {
i = fin.read();
if(i==-1 || isStop==true) //if aborted
break;
out.write(i);
}
out.flush();
}catch (Exception e){
throw e;
} finally {
if(fin!=null)
fin.close();
}
}
public void sendData(String sdata) throws Exception {
//System.out.print("Sending data on datacon ");
if(socket==null) {
Thread.currentThread().sleep(500);
if(socket==null) {
throw new IOException("No client connected");
}
}
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(socket.getOutputStream());
out.write( sdata.getBytes(),0,sdata.length() );
out.flush();
}catch (Exception e){
throw e;
}
}
public void startDataServer(ServerSocket acceptSocket, ftpserver.Data data) {
new DataServer(acceptSocket, data);
}
//-- PoolableObject
public void clean() {
isTransferring = false;
isRenameFrom = false;
isStop = false;
isPassive = false;
socket = null;
server = null;
socketPort = -1;
serverPort = -1;
closeDataServer = true;
ip = null;
wasQuit = false;
isProcessing = false;
noOfBytesTransferred = -1;
startPosition = 0;
root = null;
wDir = "/";
account = null;
username = "";
binary = false;
type = 'A';
typeSub = 'N';
mode = "Stream";
structure = "File";
}
public boolean isPoolable() {
return true;
}
public PoolableObjectFactory getPoolableObjectFactory() {
return new BasePoolableObjectFactory() {
public Object makeObject() {
return new Data();
}
public void passivateObject(Object obj) {
Data ed = (Data)obj;
ed.clean();
}
public void destroyObject(Object obj) {
if(obj==null) return;
passivateObject(obj);
obj = null;
}
public boolean validateObject(Object obj) {
if(obj==null)
return false;
else
return true;
}
};
}
}
class DataServer extends Thread {
private static Logger logger =
Logger.getLogger(DataServer.class.getName());
ServerSocket acceptSocket=null;
ftpserver.Data data = null;
public DataServer(ServerSocket acceptSocket, ftpserver.Data data) {
super("DataServer");
this.acceptSocket = acceptSocket;
this.data = data;
setDaemon(true);
this.data.isStop = false;
start();
}
public void run() {
try {
data.closeDataServer = false;
data.socket = acceptSocket.accept();
logger.fine("Data con opened.");
while(data!=null && data.closeDataServer == false
&& data.isStop==false ) {
Thread.sleep(300);
}
if(data.socket!=null) {
data.socket.close();
}
} catch(IOException e) {
System.err.println("Error at dataServer :"+e);
} catch(InterruptedException e) {
System.err.println("Thread Error at dataServer :"+e);
} finally {
try {
acceptSocket.close();
}catch(Exception re){
System.err.println("BAD Error at dataServer :"+re);
}
}
data.socket = null;
data.server = null;
}
}