提交 5c0650a7 authored 作者: Thomas Mueller's avatar Thomas Mueller

Move FTP server and client to dev package

上级 bf724510
......@@ -6,9 +6,10 @@
*/
package org.h2.test.unit;
import org.h2.server.ftp.FtpEvent;
import org.h2.server.ftp.FtpEventListener;
import org.h2.server.ftp.FtpServer;
import org.h2.dev.ftp.FtpClient;
import org.h2.dev.ftp.server.FtpEvent;
import org.h2.dev.ftp.server.FtpEventListener;
import org.h2.dev.ftp.server.FtpServer;
import org.h2.test.TestBase;
import org.h2.tools.Server;
......
......@@ -4,10 +4,12 @@
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.unit;
package org.h2.dev.ftp;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
......@@ -45,7 +47,7 @@ public class FtpClient {
* @param url the FTP URL
* @return the ftp client object
*/
static FtpClient open(String url) throws IOException {
public static FtpClient open(String url) throws IOException {
FtpClient client = new FtpClient();
client.connect(url);
return client;
......@@ -61,9 +63,12 @@ public class FtpClient {
}
private void readLine() throws IOException {
while (true) {
message = reader.readLine();
if (message != null) {
int idx = message.indexOf(' ');
int idxSpace = message.indexOf(' ');
int idxMinus = message.indexOf('-');
int idx = idxSpace < 0 ? idxMinus : idxMinus < 0 ? idxSpace : Math.min(idxSpace, idxMinus);
if (idx < 0) {
code = 0;
} else {
......@@ -71,6 +76,11 @@ public class FtpClient {
message = message.substring(idx + 1);
}
}
if (message.equals("Quotas off")) {
continue;
}
break;
}
}
private void readCode(int expected) throws IOException {
......@@ -91,7 +101,7 @@ public class FtpClient {
* @param userName the user name
* @param password the password
*/
void login(String userName, String password) throws IOException {
public void login(String userName, String password) throws IOException {
send("USER " + userName);
readCode(331);
send("PASS " + password);
......@@ -109,7 +119,7 @@ public class FtpClient {
/**
* Close the connection (QUIT).
*/
void close() throws IOException {
public void close() throws IOException {
if (socket != null) {
send("QUIT");
readCode(221);
......@@ -122,7 +132,7 @@ public class FtpClient {
*
* @param dir the new directory
*/
void changeWorkingDirectory(String dir) throws IOException {
public void changeWorkingDirectory(String dir) throws IOException {
send("CWD " + dir);
readCode(250);
}
......@@ -130,7 +140,7 @@ public class FtpClient {
/**
* Change to the parent directory (CDUP).
*/
void changeDirectoryUp() throws IOException {
public void changeDirectoryUp() throws IOException {
send("CDUP");
readCode(250);
}
......@@ -150,7 +160,7 @@ public class FtpClient {
*
* @param dir the directory to create
*/
void makeDirectory(String dir) throws IOException {
public void makeDirectory(String dir) throws IOException {
send("MKD " + dir);
readCode(257);
}
......@@ -265,11 +275,30 @@ public class FtpClient {
*
* @param dir the directory to remove
*/
void removeDirectory(String dir) throws IOException {
public void removeDirectory(String dir) throws IOException {
send("RMD " + dir);
readCode(250);
}
/**
* Remove all files and directory in a directory, and then delete the
* directory itself.
*
* @param dir the directory to remove
*/
public void removeDirectoryRecursive(String dir) throws IOException {
File[] list = listFiles(dir);
for (int i = 0; i < list.length; i++) {
File f = list[i];
if (f.isDirectory()) {
removeDirectoryRecursive(dir + "/" + f.getName());
} else {
delete(dir + "/" + f.getName());
}
}
removeDirectory(dir);
}
/**
* Get the size of a file (SIZE).
*
......@@ -289,7 +318,7 @@ public class FtpClient {
* @param fileName the file name
* @param in the input stream
*/
void store(String fileName, InputStream in) throws IOException {
public void store(String fileName, InputStream in) throws IOException {
passive();
send("STOR " + fileName);
readCode(150);
......@@ -297,13 +326,33 @@ public class FtpClient {
readCode(226);
}
/**
* Copy a local file or directory to the FTP server, recursively.
*
* @param file the file to copy
*/
public void storeRecursive(File file) throws IOException {
if (file.isDirectory()) {
makeDirectory(file.getName());
changeWorkingDirectory(file.getName());
File[] list = file.listFiles();
for (int i = 0; i < list.length; i++) {
storeRecursive(list[i]);
}
changeWorkingDirectory("..");
} else {
InputStream in = new FileInputStream(file);
store(file.getName(), in);
}
}
/**
* Get the directory listing (NLST).
*
* @param dir the directory
* @return the listing
*/
String nameList(String dir) throws IOException {
public String nameList(String dir) throws IOException {
passive();
send("NLST " + dir);
readCode(150);
......@@ -320,7 +369,7 @@ public class FtpClient {
* @param dir the directory
* @return the listing
*/
String list(String dir) throws IOException {
public String list(String dir) throws IOException {
passive();
send("LIST " + dir);
readCode(150);
......@@ -331,4 +380,75 @@ public class FtpClient {
return new String(data);
}
/**
* A file on an FTP server.
*/
static class FtpFile extends File {
private final boolean dir;
private final long length;
FtpFile(String name, boolean dir, long length) {
super(name);
this.dir = dir;
this.length = length;
}
public long length() {
return length;
}
public boolean isFile() {
return !dir;
}
public boolean isDirectory() {
return dir;
}
public boolean exists() {
return true;
}
}
/**
* Check if a file exists on the FTP server.
*
* @param dir the directory
* @param name the directory or file name
* @return true if it exists
*/
public boolean exists(String dir, String name) throws IOException {
File[] list = listFiles(dir);
for (int i = 0; i < list.length; i++) {
if (list[i].getName().equals(name)) {
return true;
}
}
return false;
}
/**
* List the files on the FTP server.
*
* @param dir the directory
* @return the list of files
*/
public File[] listFiles(String dir) throws IOException {
String content = list(dir);
String[] list = StringUtils.arraySplit(content.trim(), '\n', true);
File[] files = new File[list.length];
for (int i = 0; i < files.length; i++) {
String s = list[i];
while (true) {
String s2 = StringUtils.replaceAll(s, " ", " ");
if (s2.equals(s)) {
break;
}
s = s2;
}
String[] tokens = StringUtils.arraySplit(s, ' ', true);
boolean directory = tokens[0].charAt(0) == 'd';
long length = Long.parseLong(tokens[4]);
String name = tokens[8];
File f = new FtpFile(name, directory, length);
files[i] = f;
}
return files;
}
}
......@@ -10,6 +10,6 @@ Initial Developer: H2 Group
Javadoc package documentation
</title></head><body style="font: 9pt/130% Tahoma, Arial, Helvetica, sans-serif; font-weight: normal;">
Implementation of the TCP server.
A simple FTP client.
</body></html>
\ No newline at end of file
......@@ -4,7 +4,7 @@
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.server.ftp;
package org.h2.dev.ftp.server;
import java.io.BufferedReader;
import java.io.IOException;
......
......@@ -4,7 +4,7 @@
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.server.ftp;
package org.h2.dev.ftp.server;
import java.io.IOException;
import java.io.InputStream;
......
......@@ -4,7 +4,7 @@
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.server.ftp;
package org.h2.dev.ftp.server;
/**
* Describes an FTP event. This class is used by the FtpEventListener.
......
......@@ -4,7 +4,7 @@
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.server.ftp;
package org.h2.dev.ftp.server;
/**
* Event listener for the FTP Server.
......
......@@ -4,7 +4,7 @@
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.server.ftp;
package org.h2.dev.ftp.server;
import java.io.File;
import java.io.IOException;
......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License, Version 1.0,
and under the Eclipse Public License, Version 1.0
(http://h2database.com/html/license.html).
Initial Developer: H2 Group
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>
Javadoc package documentation
</title></head><body style="font: 9pt/130% Tahoma, Arial, Helvetica, sans-serif; font-weight: normal;">
A simple FTP server.
</body></html>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论