001: /**
002: *
003: * Copyright (C) 2000-2007 Enterprise Distributed Technologies Ltd
004: *
005: * www.enterprisedt.com
006: *
007: * Change Log:
008: *
009: * $Log: FileTransferClientAdapter.java,v $
010: * Revision 1.2 2007-12-20 00:40:27 bruceb
011: * downloadByteArray
012: *
013: * Revision 1.1 2007-12-18 07:55:19 bruceb
014: * prepare for FileTransferClient
015: *
016: *
017: */package com.enterprisedt.net.ftp.test;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024: import java.text.ParseException;
025: import java.util.Date;
026:
027: import com.enterprisedt.net.ftp.EventListener;
028: import com.enterprisedt.net.ftp.FTPClientInterface;
029: import com.enterprisedt.net.ftp.FTPException;
030: import com.enterprisedt.net.ftp.FTPFile;
031: import com.enterprisedt.net.ftp.FTPProgressMonitor;
032: import com.enterprisedt.net.ftp.FTPTransferType;
033: import com.enterprisedt.net.ftp.FileTransferClient;
034: import com.enterprisedt.net.ftp.FileTransferOutputStream;
035: import com.enterprisedt.net.ftp.WriteMode;
036: import com.enterprisedt.util.debug.Logger;
037:
038: /**
039: * Adapts the FileTransferClient for use in the standard unit
040: * tests
041: *
042: * @author Bruce Blackshaw
043: * @version $Revision: 1.2 $
044: */
045: public class FileTransferClientAdapter implements FTPClientInterface,
046: EventListener {
047:
048: private Logger log = Logger.getLogger("FileTransferClientAdapter");
049:
050: private FileTransferClient client;
051:
052: private boolean resume = false;
053:
054: private FTPProgressMonitor monitor;
055:
056: /**
057: * Constructor
058: *
059: * @param client
060: */
061: public FileTransferClientAdapter(FileTransferClient client) {
062: this .client = client;
063: }
064:
065: public void cancelResume() throws IOException, FTPException {
066: resume = false;
067: }
068:
069: public void cancelTransfer() {
070: log.debug("Cancelling all transfers");
071: client.cancelAllTransfers();
072: }
073:
074: public void cdup() throws IOException, FTPException {
075: client.changeToParentDirectory();
076: }
077:
078: public void chdir(String dir) throws IOException, FTPException {
079: client.changeDirectory(dir);
080: }
081:
082: public void connect() throws IOException, FTPException {
083: client.connect();
084:
085: }
086:
087: public boolean connected() {
088: return client.isConnected();
089: }
090:
091: public void delete(String remoteFile) throws IOException,
092: FTPException {
093: client.deleteFile(remoteFile);
094: }
095:
096: public String[] dir() throws IOException, FTPException {
097: return client.directoryNameList("", false);
098: }
099:
100: public String[] dir(String dirname) throws IOException,
101: FTPException {
102: return client.directoryNameList(dirname, false);
103: }
104:
105: public String[] dir(String dirname, boolean full)
106: throws IOException, FTPException {
107: return client.directoryNameList(dirname, full);
108: }
109:
110: public FTPFile[] dirDetails(String dirname) throws IOException,
111: FTPException, ParseException {
112: return client.directoryList(dirname);
113: }
114:
115: public boolean exists(String remoteFile) throws IOException,
116: FTPException {
117: return client.exists(remoteFile);
118: }
119:
120: public void get(String localPath, String remoteFile)
121: throws IOException, FTPException {
122: try {
123: client.downloadFile(localPath, remoteFile,
124: resume ? WriteMode.RESUME : WriteMode.OVERWRITE);
125: } finally {
126: resume = false;
127: }
128: }
129:
130: public void get(OutputStream destStream, String remoteFile)
131: throws IOException, FTPException {
132: InputStream str = null;
133: try {
134: str = client.downloadStream(remoteFile);
135:
136: byte[] buf = new byte[1024];
137: int len = 0;
138: while ((len = str.read(buf)) >= 0) {
139: destStream.write(buf, 0, len);
140: }
141: } finally {
142: str.close();
143: }
144: }
145:
146: public byte[] get(String remoteFile) throws IOException,
147: FTPException {
148: return client.downloadByteArray(remoteFile);
149: }
150:
151: public int getDeleteCount() {
152: return client.getStatistics().getDeleteCount();
153: }
154:
155: public boolean getDetectTransferMode() {
156: return client.getDetectContentType();
157: }
158:
159: public int getDownloadCount() {
160: return client.getStatistics().getDownloadCount();
161: }
162:
163: public String getId() {
164: return null;
165: }
166:
167: public long getMonitorInterval() {
168: return client.getAdvancedFTPSettings()
169: .getTransferNotifyInterval();
170: }
171:
172: public String getRemoteHost() {
173: return client.getRemoteHost();
174: }
175:
176: public int getRemotePort() {
177: return client.getRemotePort();
178: }
179:
180: public int getTimeout() {
181: return client.getTimeout();
182: }
183:
184: public FTPTransferType getType() {
185: return client.getContentType();
186: }
187:
188: public int getUploadCount() {
189: return client.getStatistics().getUploadCount();
190: }
191:
192: public void keepAlive() throws IOException, FTPException {
193:
194: }
195:
196: public void mkdir(String dir) throws IOException, FTPException {
197: client.createDirectory(dir);
198: }
199:
200: public Date modtime(String remoteFile) throws IOException,
201: FTPException {
202: return client.getModifiedTime(remoteFile);
203: }
204:
205: public String put(String localPath, String remoteFile)
206: throws IOException, FTPException {
207: try {
208: return client.uploadFile(localPath, remoteFile,
209: (resume ? WriteMode.RESUME : WriteMode.OVERWRITE));
210: } finally {
211: resume = false;
212: }
213: }
214:
215: public String put(InputStream srcStream, String remoteFile)
216: throws IOException, FTPException {
217: return put(srcStream, remoteFile, false);
218: }
219:
220: public String put(InputStream srcStream, String remoteFile,
221: boolean append) throws IOException, FTPException {
222: FileTransferOutputStream str = null;
223: try {
224: str = client.uploadStream(remoteFile,
225: (append ? WriteMode.APPEND : WriteMode.OVERWRITE));
226: byte[] buf = new byte[2048];
227: int len = 0;
228: while ((len = srcStream.read(buf)) >= 0) {
229: str.write(buf, 0, len);
230: }
231: return str.getRemoteFile();
232: } finally {
233: if (str != null)
234: str.close();
235: }
236: }
237:
238: public String put(byte[] bytes, String remoteFile)
239: throws IOException, FTPException {
240: return put(bytes, remoteFile, false);
241: }
242:
243: public String put(byte[] bytes, String remoteFile, boolean append)
244: throws IOException, FTPException {
245: ByteArrayInputStream str = new ByteArrayInputStream(bytes);
246: return put(str, remoteFile, append);
247: }
248:
249: public String put(String localPath, String remoteFile,
250: boolean append) throws IOException, FTPException {
251: return client.uploadFile(localPath, remoteFile,
252: WriteMode.APPEND);
253: }
254:
255: public String pwd() throws IOException, FTPException {
256: return client.getRemoteDirectory();
257: }
258:
259: public void quit() throws IOException, FTPException {
260: client.disconnect();
261: }
262:
263: public void quitImmediately() throws IOException, FTPException {
264: client.disconnect(true);
265: }
266:
267: public void rename(String from, String to) throws IOException,
268: FTPException {
269: client.rename(from, to);
270: }
271:
272: public void resetDeleteCount() {
273: client.getStatistics().clear();
274: }
275:
276: public void resetDownloadCount() {
277: client.getStatistics().clear();
278: }
279:
280: public void resetUploadCount() {
281: client.getStatistics().clear();
282: }
283:
284: public void resume() throws FTPException {
285: resume = true;
286: }
287:
288: public void rmdir(String dir) throws IOException, FTPException {
289: client.deleteDirectory(dir);
290: }
291:
292: public void setDetectTransferMode(boolean detectTransferMode) {
293: client.setDetectContentType(detectTransferMode);
294: }
295:
296: public void setId(String id) {
297: // TODO Auto-generated method stub
298:
299: }
300:
301: public void setProgressMonitor(FTPProgressMonitor monitor,
302: long interval) {
303: this .monitor = monitor;
304: client.setEventListener(this );
305: client.getAdvancedFTPSettings().setTransferNotifyInterval(
306: (int) interval);
307: }
308:
309: public void setProgressMonitor(FTPProgressMonitor monitor) {
310: this .monitor = monitor;
311: client.setEventListener(this );
312: }
313:
314: public void setRemoteHost(String remoteHost) throws IOException,
315: FTPException {
316: client.setRemoteHost(remoteHost);
317: }
318:
319: public void setRemotePort(int remotePort) throws FTPException {
320: client.setRemotePort(remotePort);
321: }
322:
323: public void setTimeout(int timeout) throws IOException,
324: FTPException {
325: client.setTimeout(timeout);
326: }
327:
328: public void setType(FTPTransferType type) throws IOException,
329: FTPException {
330: client.setContentType(type);
331: }
332:
333: public long size(String remoteFile) throws IOException,
334: FTPException {
335: return client.getSize(remoteFile);
336: }
337:
338: public void bytesTransferred(long count) {
339: if (monitor != null)
340: monitor.bytesTransferred(count);
341: }
342:
343: public void commandSent(String cmd) {
344: // TODO Auto-generated method stub
345:
346: }
347:
348: public void downloadCompleted(String remoteFilename) {
349: // TODO Auto-generated method stub
350:
351: }
352:
353: public void downloadStarted(String remoteFilename) {
354: // TODO Auto-generated method stub
355: }
356:
357: public void replyReceived(String reply) {
358: // TODO Auto-generated method stub
359:
360: }
361:
362: public void uploadCompleted(String remoteFilename) {
363: // TODO Auto-generated method stub
364:
365: }
366:
367: public void uploadStarted(String remoteFilename) {
368: // TODO Auto-generated method stub
369:
370: }
371: }
|