代码整理

This commit is contained in:
wyl
2023-04-30 15:14:59 +08:00
parent a84f1bd1cd
commit feae52fe04
18 changed files with 1634 additions and 1650 deletions
@@ -18,16 +18,6 @@
<artifactId>aivfo-log-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-log-spring-boot-core</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-log-spring-boot-autoconfigure</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-mqtt-spring-boot-starter</artifactId>
@@ -35,27 +25,7 @@
</dependency>
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-mqtt-spring-boot-core</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-mqtt-spring-boot-autoconfigure</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-fastDFS-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-fastDFS-spring-boot-core</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-fastDFS-spring-boot-autoconfigure</artifactId>
<artifactId>aivfo-fast-dfs-client</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
@@ -13,8 +13,8 @@
<dependencies>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-fast-dfs-client</artifactId>
</dependency>
</dependencies>
</project>
@@ -10,6 +10,11 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>aivfo-fast-dfs-client</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
</project>
@@ -8,8 +8,8 @@
package csource.fastdfs;
import org.csource.common.IniFileReader;
import org.csource.common.MyException;
import csource.common.IniFileReader;
import csource.common.MyException;
import java.io.IOException;
import java.io.InputStream;
@@ -27,331 +27,331 @@ import java.util.Properties;
*/
public class ClientGlobal {
public static final String CONF_KEY_CONNECT_TIMEOUT = "connect_timeout";
public static final String CONF_KEY_NETWORK_TIMEOUT = "network_timeout";
public static final String CONF_KEY_CHARSET = "charset";
public static final String CONF_KEY_HTTP_ANTI_STEAL_TOKEN = "http.anti_steal_token";
public static final String CONF_KEY_HTTP_SECRET_KEY = "http.secret_key";
public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "http.tracker_http_port";
public static final String CONF_KEY_TRACKER_SERVER = "tracker_server";
public static final String PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS = "fastdfs.connect_timeout_in_seconds";
public static final String PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS = "fastdfs.network_timeout_in_seconds";
public static final String PROP_KEY_CHARSET = "fastdfs.charset";
public static final String PROP_KEY_HTTP_ANTI_STEAL_TOKEN = "fastdfs.http_anti_steal_token";
public static final String PROP_KEY_HTTP_SECRET_KEY = "fastdfs.http_secret_key";
public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port";
public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers";
public static final String CONF_KEY_CONNECT_TIMEOUT = "connect_timeout";
public static final String CONF_KEY_NETWORK_TIMEOUT = "network_timeout";
public static final String CONF_KEY_CHARSET = "charset";
public static final String CONF_KEY_HTTP_ANTI_STEAL_TOKEN = "http.anti_steal_token";
public static final String CONF_KEY_HTTP_SECRET_KEY = "http.secret_key";
public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "http.tracker_http_port";
public static final String CONF_KEY_TRACKER_SERVER = "tracker_server";
public static final String PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS = "fastdfs.connect_timeout_in_seconds";
public static final String PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS = "fastdfs.network_timeout_in_seconds";
public static final String PROP_KEY_CHARSET = "fastdfs.charset";
public static final String PROP_KEY_HTTP_ANTI_STEAL_TOKEN = "fastdfs.http_anti_steal_token";
public static final String PROP_KEY_HTTP_SECRET_KEY = "fastdfs.http_secret_key";
public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port";
public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers";
public static final String PROP_KEY_CONNECTION_POOL_ENABLED = "fastdfs.connection_pool.enabled";
public static final String PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = "fastdfs.connection_pool.max_count_per_entry";
public static final String PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME = "fastdfs.connection_pool.max_idle_time";
public static final String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = "fastdfs.connection_pool.max_wait_time_in_ms";
public static final String PROP_KEY_CONNECTION_POOL_ENABLED = "fastdfs.connection_pool.enabled";
public static final String PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = "fastdfs.connection_pool.max_count_per_entry";
public static final String PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME = "fastdfs.connection_pool.max_idle_time";
public static final String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = "fastdfs.connection_pool.max_wait_time_in_ms";
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
public static final String DEFAULT_CHARSET = "UTF-8";
public static final boolean DEFAULT_HTTP_ANTI_STEAL_TOKEN = false;
public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890";
public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80;
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
public static final String DEFAULT_CHARSET = "UTF-8";
public static final boolean DEFAULT_HTTP_ANTI_STEAL_TOKEN = false;
public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890";
public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80;
public static final boolean DEFAULT_CONNECTION_POOL_ENABLED = true;
public static final int DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = 100;
public static final int DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME = 3600 ;//second
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = 1000 ;//millisecond
public static final boolean DEFAULT_CONNECTION_POOL_ENABLED = true;
public static final int DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = 100;
public static final int DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME = 3600;//second
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = 1000;//millisecond
public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
public static String g_charset = DEFAULT_CHARSET;
public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN; //if anti-steal token
public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; //generage token secret key
public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT;
public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
public static String g_charset = DEFAULT_CHARSET;
public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN; //if anti-steal token
public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; //generage token secret key
public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT;
public static boolean g_connection_pool_enabled = DEFAULT_CONNECTION_POOL_ENABLED;
public static int g_connection_pool_max_count_per_entry = DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY;
public static int g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME * 1000; //millisecond
public static int g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS; //millisecond
public static boolean g_connection_pool_enabled = DEFAULT_CONNECTION_POOL_ENABLED;
public static int g_connection_pool_max_count_per_entry = DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY;
public static int g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME * 1000; //millisecond
public static int g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS; //millisecond
public static TrackerGroup g_tracker_group;
public static TrackerGroup g_tracker_group;
private ClientGlobal() {
}
/**
* load global variables
*
* @param conf_filename config filename
*/
public static void init(String conf_filename) throws IOException, MyException {
IniFileReader iniReader;
String[] szTrackerServers;
String[] parts;
iniReader = new IniFileReader(conf_filename);
g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
if (g_connect_timeout < 0) {
g_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
}
g_connect_timeout *= 1000; //millisecond
g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT);
if (g_network_timeout < 0) {
g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
}
g_network_timeout *= 1000; //millisecond
g_charset = iniReader.getStrValue("charset");
if (g_charset == null || g_charset.length() == 0) {
g_charset = "ISO8859-1";
private ClientGlobal() {
}
szTrackerServers = iniReader.getValues("tracker_server");
if (szTrackerServers == null) {
throw new MyException("item \"tracker_server\" in " + conf_filename + " not found");
/**
* load global variables
*
* @param conf_filename config filename
*/
public static void init(String conf_filename) throws IOException, MyException {
IniFileReader iniReader;
String[] szTrackerServers;
String[] parts;
iniReader = new IniFileReader(conf_filename);
g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
if (g_connect_timeout < 0) {
g_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
}
g_connect_timeout *= 1000; //millisecond
g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT);
if (g_network_timeout < 0) {
g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
}
g_network_timeout *= 1000; //millisecond
g_charset = iniReader.getStrValue("charset");
if (g_charset == null || g_charset.length() == 0) {
g_charset = "ISO8859-1";
}
szTrackerServers = iniReader.getValues("tracker_server");
if (szTrackerServers == null) {
throw new MyException("item \"tracker_server\" in " + conf_filename + " not found");
}
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
for (int i = 0; i < szTrackerServers.length; i++) {
parts = szTrackerServers[i].split("\\:", 2);
if (parts.length != 2) {
throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
}
tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
}
g_tracker_group = new TrackerGroup(tracker_servers);
g_tracker_http_port = iniReader.getIntValue("http.tracker_http_port", 80);
g_anti_steal_token = iniReader.getBoolValue("http.anti_steal_token", false);
if (g_anti_steal_token) {
g_secret_key = iniReader.getStrValue("http.secret_key");
}
g_connection_pool_enabled = iniReader.getBoolValue("connection_pool.enabled", DEFAULT_CONNECTION_POOL_ENABLED);
g_connection_pool_max_count_per_entry = iniReader.getIntValue("connection_pool.max_count_per_entry", DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
g_connection_pool_max_idle_time = iniReader.getIntValue("connection_pool.max_idle_time", DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME);
if (g_connection_pool_max_idle_time < 0) {
g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME;
}
g_connection_pool_max_idle_time *= 1000;
g_connection_pool_max_wait_time_in_ms = iniReader.getIntValue("connection_pool.max_wait_time_in_ms", DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
if (g_connection_pool_max_wait_time_in_ms < 0) {
g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
}
}
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
for (int i = 0; i < szTrackerServers.length; i++) {
parts = szTrackerServers[i].split("\\:", 2);
if (parts.length != 2) {
throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
}
tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
/**
* load from properties file
*
* @param propsFilePath properties file path, eg:
* "fastdfs-client.properties"
* "config/fastdfs-client.properties"
* "/opt/fastdfs-client.properties"
* "C:\\Users\\James\\config\\fastdfs-client.properties"
* properties文件至少包含一个配置项 fastdfs.tracker_servers 例如:
* fastdfs.tracker_servers = 10.0.11.245:22122,10.0.11.246:22122
* server的IP和端口用冒号':'分隔
* server之间用逗号','分隔
*/
public static void initByProperties(String propsFilePath) throws IOException, MyException {
Properties props = new Properties();
InputStream in = IniFileReader.loadFromOsFileSystemOrClasspathAsStream(propsFilePath);
if (in != null) {
props.load(in);
}
initByProperties(props);
}
g_tracker_group = new TrackerGroup(tracker_servers);
g_tracker_http_port = iniReader.getIntValue("http.tracker_http_port", 80);
g_anti_steal_token = iniReader.getBoolValue("http.anti_steal_token", false);
if (g_anti_steal_token) {
g_secret_key = iniReader.getStrValue("http.secret_key");
public static void initByProperties(Properties props) throws IOException, MyException {
String trackerServersConf = props.getProperty(PROP_KEY_TRACKER_SERVERS);
if (trackerServersConf == null || trackerServersConf.trim().length() == 0) {
throw new MyException(String.format("configure item %s is required", PROP_KEY_TRACKER_SERVERS));
}
initByTrackers(trackerServersConf.trim());
String connectTimeoutInSecondsConf = props.getProperty(PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS);
String networkTimeoutInSecondsConf = props.getProperty(PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS);
String charsetConf = props.getProperty(PROP_KEY_CHARSET);
String httpAntiStealTokenConf = props.getProperty(PROP_KEY_HTTP_ANTI_STEAL_TOKEN);
String httpSecretKeyConf = props.getProperty(PROP_KEY_HTTP_SECRET_KEY);
String httpTrackerHttpPortConf = props.getProperty(PROP_KEY_HTTP_TRACKER_HTTP_PORT);
String poolEnabled = props.getProperty(PROP_KEY_CONNECTION_POOL_ENABLED);
String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME);
String poolMaxWaitTimeInMS = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
}
if (networkTimeoutInSecondsConf != null && networkTimeoutInSecondsConf.trim().length() != 0) {
g_network_timeout = Integer.parseInt(networkTimeoutInSecondsConf.trim()) * 1000;
}
if (charsetConf != null && charsetConf.trim().length() != 0) {
g_charset = charsetConf.trim();
}
if (httpAntiStealTokenConf != null && httpAntiStealTokenConf.trim().length() != 0) {
g_anti_steal_token = Boolean.parseBoolean(httpAntiStealTokenConf);
}
if (httpSecretKeyConf != null && httpSecretKeyConf.trim().length() != 0) {
g_secret_key = httpSecretKeyConf.trim();
}
if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf.trim().length() != 0) {
g_tracker_http_port = Integer.parseInt(httpTrackerHttpPortConf);
}
if (poolEnabled != null && poolEnabled.trim().length() != 0) {
g_connection_pool_enabled = Boolean.parseBoolean(poolEnabled);
}
if (poolMaxCountPerEntry != null && poolMaxCountPerEntry.trim().length() != 0) {
g_connection_pool_max_count_per_entry = Integer.parseInt(poolMaxCountPerEntry);
}
if (poolMaxIdleTime != null && poolMaxIdleTime.trim().length() != 0) {
g_connection_pool_max_idle_time = Integer.parseInt(poolMaxIdleTime) * 1000;
}
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS);
}
}
g_connection_pool_enabled = iniReader.getBoolValue("connection_pool.enabled", DEFAULT_CONNECTION_POOL_ENABLED);
g_connection_pool_max_count_per_entry = iniReader.getIntValue("connection_pool.max_count_per_entry", DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
g_connection_pool_max_idle_time = iniReader.getIntValue("connection_pool.max_idle_time", DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME);
if (g_connection_pool_max_idle_time < 0) {
g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME;
/**
* load from properties file
*
* @param trackerServers 例如:"10.0.11.245:22122,10.0.11.246:22122"
* server的IP和端口用冒号':'分隔
* server之间用逗号','分隔
*/
public static void initByTrackers(String trackerServers) throws IOException, MyException {
List<InetSocketAddress> list = new ArrayList<InetSocketAddress>();
String spr1 = ",";
String spr2 = ":";
String[] arr1 = trackerServers.trim().split(spr1);
for (String addrStr : arr1) {
String[] arr2 = addrStr.trim().split(spr2);
String host = arr2[0].trim();
int port = Integer.parseInt(arr2[1].trim());
list.add(new InetSocketAddress(host, port));
}
InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]);
initByTrackers(trackerAddresses);
}
g_connection_pool_max_idle_time *= 1000;
g_connection_pool_max_wait_time_in_ms = iniReader.getIntValue("connection_pool.max_wait_time_in_ms", DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
if (g_connection_pool_max_wait_time_in_ms < 0) {
g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
public static void initByTrackers(InetSocketAddress[] trackerAddresses) throws IOException, MyException {
g_tracker_group = new TrackerGroup(trackerAddresses);
}
}
/**
* load from properties file
*
* @param propsFilePath properties file path, eg:
* "fastdfs-client.properties"
* "config/fastdfs-client.properties"
* "/opt/fastdfs-client.properties"
* "C:\\Users\\James\\config\\fastdfs-client.properties"
* properties文件至少包含一个配置项 fastdfs.tracker_servers 例如:
* fastdfs.tracker_servers = 10.0.11.245:22122,10.0.11.246:22122
* server的IP和端口用冒号':'分隔
* server之间用逗号','分隔
*/
public static void initByProperties(String propsFilePath) throws IOException, MyException {
Properties props = new Properties();
InputStream in = IniFileReader.loadFromOsFileSystemOrClasspathAsStream(propsFilePath);
if (in != null) {
props.load(in);
/**
* construct Socket object
*
* @param ip_addr ip address or hostname
* @param port port number
* @return connected Socket object
*/
public static Socket getSocket(String ip_addr, int port) throws IOException {
Socket sock = new Socket();
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
return sock;
}
initByProperties(props);
}
public static void initByProperties(Properties props) throws IOException, MyException {
String trackerServersConf = props.getProperty(PROP_KEY_TRACKER_SERVERS);
if (trackerServersConf == null || trackerServersConf.trim().length() == 0) {
throw new MyException(String.format("configure item %s is required", PROP_KEY_TRACKER_SERVERS));
/**
* construct Socket object
*
* @param addr InetSocketAddress object, including ip address and port
* @return connected Socket object
*/
public static Socket getSocket(InetSocketAddress addr) throws IOException {
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(addr, ClientGlobal.g_connect_timeout);
return sock;
}
initByTrackers(trackerServersConf.trim());
String connectTimeoutInSecondsConf = props.getProperty(PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS);
String networkTimeoutInSecondsConf = props.getProperty(PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS);
String charsetConf = props.getProperty(PROP_KEY_CHARSET);
String httpAntiStealTokenConf = props.getProperty(PROP_KEY_HTTP_ANTI_STEAL_TOKEN);
String httpSecretKeyConf = props.getProperty(PROP_KEY_HTTP_SECRET_KEY);
String httpTrackerHttpPortConf = props.getProperty(PROP_KEY_HTTP_TRACKER_HTTP_PORT);
String poolEnabled = props.getProperty(PROP_KEY_CONNECTION_POOL_ENABLED);
String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME);
String poolMaxWaitTimeInMS = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
public static int getG_connect_timeout() {
return g_connect_timeout;
}
if (networkTimeoutInSecondsConf != null && networkTimeoutInSecondsConf.trim().length() != 0) {
g_network_timeout = Integer.parseInt(networkTimeoutInSecondsConf.trim()) * 1000;
public static void setG_connect_timeout(int connect_timeout) {
ClientGlobal.g_connect_timeout = connect_timeout;
}
if (charsetConf != null && charsetConf.trim().length() != 0) {
g_charset = charsetConf.trim();
public static int getG_network_timeout() {
return g_network_timeout;
}
if (httpAntiStealTokenConf != null && httpAntiStealTokenConf.trim().length() != 0) {
g_anti_steal_token = Boolean.parseBoolean(httpAntiStealTokenConf);
public static void setG_network_timeout(int network_timeout) {
ClientGlobal.g_network_timeout = network_timeout;
}
if (httpSecretKeyConf != null && httpSecretKeyConf.trim().length() != 0) {
g_secret_key = httpSecretKeyConf.trim();
public static String getG_charset() {
return g_charset;
}
if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf.trim().length() != 0) {
g_tracker_http_port = Integer.parseInt(httpTrackerHttpPortConf);
public static void setG_charset(String charset) {
ClientGlobal.g_charset = charset;
}
if (poolEnabled != null && poolEnabled.trim().length() != 0) {
g_connection_pool_enabled = Boolean.parseBoolean(poolEnabled);
public static int getG_tracker_http_port() {
return g_tracker_http_port;
}
if (poolMaxCountPerEntry != null && poolMaxCountPerEntry.trim().length() != 0 ) {
g_connection_pool_max_count_per_entry = Integer.parseInt(poolMaxCountPerEntry);
public static void setG_tracker_http_port(int tracker_http_port) {
ClientGlobal.g_tracker_http_port = tracker_http_port;
}
if (poolMaxIdleTime != null && poolMaxIdleTime.trim().length() != 0) {
g_connection_pool_max_idle_time = Integer.parseInt(poolMaxIdleTime) * 1000;
public static boolean getG_anti_steal_token() {
return g_anti_steal_token;
}
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS);
public static boolean isG_anti_steal_token() {
return g_anti_steal_token;
}
}
/**
* load from properties file
*
* @param trackerServers 例如:"10.0.11.245:22122,10.0.11.246:22122"
* server的IP和端口用冒号':'分隔
* server之间用逗号','分隔
*/
public static void initByTrackers(String trackerServers) throws IOException, MyException {
List<InetSocketAddress> list = new ArrayList<InetSocketAddress>();
String spr1 = ",";
String spr2 = ":";
String[] arr1 = trackerServers.trim().split(spr1);
for (String addrStr : arr1) {
String[] arr2 = addrStr.trim().split(spr2);
String host = arr2[0].trim();
int port = Integer.parseInt(arr2[1].trim());
list.add(new InetSocketAddress(host, port));
public static void setG_anti_steal_token(boolean anti_steal_token) {
ClientGlobal.g_anti_steal_token = anti_steal_token;
}
InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]);
initByTrackers(trackerAddresses);
}
public static void initByTrackers(InetSocketAddress[] trackerAddresses) throws IOException, MyException {
g_tracker_group = new TrackerGroup(trackerAddresses);
}
/**
* construct Socket object
*
* @param ip_addr ip address or hostname
* @param port port number
* @return connected Socket object
*/
public static Socket getSocket(String ip_addr, int port) throws IOException {
Socket sock = new Socket();
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
return sock;
}
/**
* construct Socket object
*
* @param addr InetSocketAddress object, including ip address and port
* @return connected Socket object
*/
public static Socket getSocket(InetSocketAddress addr) throws IOException {
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(addr, ClientGlobal.g_connect_timeout);
return sock;
}
public static int getG_connect_timeout() {
return g_connect_timeout;
}
public static void setG_connect_timeout(int connect_timeout) {
ClientGlobal.g_connect_timeout = connect_timeout;
}
public static int getG_network_timeout() {
return g_network_timeout;
}
public static void setG_network_timeout(int network_timeout) {
ClientGlobal.g_network_timeout = network_timeout;
}
public static String getG_charset() {
return g_charset;
}
public static void setG_charset(String charset) {
ClientGlobal.g_charset = charset;
}
public static int getG_tracker_http_port() {
return g_tracker_http_port;
}
public static void setG_tracker_http_port(int tracker_http_port) {
ClientGlobal.g_tracker_http_port = tracker_http_port;
}
public static boolean getG_anti_steal_token() {
return g_anti_steal_token;
}
public static boolean isG_anti_steal_token() {
return g_anti_steal_token;
}
public static void setG_anti_steal_token(boolean anti_steal_token) {
ClientGlobal.g_anti_steal_token = anti_steal_token;
}
public static String getG_secret_key() {
return g_secret_key;
}
public static void setG_secret_key(String secret_key) {
ClientGlobal.g_secret_key = secret_key;
}
public static TrackerGroup getG_tracker_group() {
return g_tracker_group;
}
public static void setG_tracker_group(TrackerGroup tracker_group) {
ClientGlobal.g_tracker_group = tracker_group;
}
public static boolean isG_connection_pool_enabled() {
return g_connection_pool_enabled;
}
public static String configInfo() {
String trackerServers = "";
if (g_tracker_group != null) {
InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers;
for (InetSocketAddress inetSocketAddress : trackerAddresses) {
if(trackerServers.length() > 0) trackerServers += ",";
String address = inetSocketAddress.toString();
trackerServers += address.startsWith("/") ? address.substring(1) : address;
}
public static String getG_secret_key() {
return g_secret_key;
}
public static void setG_secret_key(String secret_key) {
ClientGlobal.g_secret_key = secret_key;
}
public static TrackerGroup getG_tracker_group() {
return g_tracker_group;
}
public static void setG_tracker_group(TrackerGroup tracker_group) {
ClientGlobal.g_tracker_group = tracker_group;
}
public static boolean isG_connection_pool_enabled() {
return g_connection_pool_enabled;
}
public static String configInfo() {
String trackerServers = "";
if (g_tracker_group != null) {
InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers;
for (InetSocketAddress inetSocketAddress : trackerAddresses) {
if (trackerServers.length() > 0) trackerServers += ",";
String address = inetSocketAddress.toString();
trackerServers += address.startsWith("/") ? address.substring(1) : address;
}
}
return "{"
+ "\n g_connect_timeout(ms) = " + g_connect_timeout
+ "\n g_network_timeout(ms) = " + g_network_timeout
+ "\n g_charset = " + g_charset
+ "\n g_anti_steal_token = " + g_anti_steal_token
+ "\n g_secret_key = " + g_secret_key
+ "\n g_tracker_http_port = " + g_tracker_http_port
+ "\n g_connection_pool_enabled = " + g_connection_pool_enabled
+ "\n g_connection_pool_max_count_per_entry = " + g_connection_pool_max_count_per_entry
+ "\n g_connection_pool_max_idle_time(ms) = " + g_connection_pool_max_idle_time
+ "\n g_connection_pool_max_wait_time_in_ms(ms) = " + g_connection_pool_max_wait_time_in_ms
+ "\n trackerServers = " + trackerServers
+ "\n}";
}
return "{"
+ "\n g_connect_timeout(ms) = " + g_connect_timeout
+ "\n g_network_timeout(ms) = " + g_network_timeout
+ "\n g_charset = " + g_charset
+ "\n g_anti_steal_token = " + g_anti_steal_token
+ "\n g_secret_key = " + g_secret_key
+ "\n g_tracker_http_port = " + g_tracker_http_port
+ "\n g_connection_pool_enabled = " + g_connection_pool_enabled
+ "\n g_connection_pool_max_count_per_entry = " + g_connection_pool_max_count_per_entry
+ "\n g_connection_pool_max_idle_time(ms) = " + g_connection_pool_max_idle_time
+ "\n g_connection_pool_max_wait_time_in_ms(ms) = " + g_connection_pool_max_wait_time_in_ms
+ "\n trackerServers = " + trackerServers
+ "\n}";
}
}
@@ -15,13 +15,13 @@ package csource.fastdfs;
* @version Version 1.4
*/
public interface DownloadCallback {
/**
* recv file content callback function, may be called more than once when the file downloaded
*
* @param file_size file size
* @param data data buff
* @param bytes data bytes
* @return 0 success, return none zero(errno) if fail
*/
public int recv(long file_size, byte[] data, int bytes);
/**
* recv file content callback function, may be called more than once when the file downloaded
*
* @param file_size file size
* @param data data buff
* @param bytes data bytes
* @return 0 success, return none zero(errno) if fail
*/
int recv(long file_size, byte[] data, int bytes);
}
@@ -26,6 +26,7 @@ public class DownloadStream implements DownloadCallback {
* @param bytes data bytes
* @return 0 success, return none zero(errno) if fail
*/
@Override
public int recv(long fileSize, byte[] data, int bytes) {
try {
out.write(data, 0, bytes);
@@ -18,158 +18,158 @@ import java.util.Date;
* @version Version 1.23
*/
public class FileInfo {
public static final short FILE_TYPE_NORMAL = 1;
public static final short FILE_TYPE_APPENDER = 2;
public static final short FILE_TYPE_SLAVE = 4;
public static final short FILE_TYPE_NORMAL = 1;
public static final short FILE_TYPE_APPENDER = 2;
public static final short FILE_TYPE_SLAVE = 4;
protected boolean fetch_from_server;
protected short file_type;
protected String source_ip_addr;
protected long file_size;
protected Date create_timestamp;
protected int crc32;
protected boolean fetch_from_server;
protected short file_type;
protected String source_ip_addr;
protected long file_size;
protected Date create_timestamp;
protected int crc32;
/**
* Constructor
*
* @param fetch_from_server if fetch from server flag
* @param file_type the file type
* @param file_size the file size
* @param create_timestamp create timestamp in seconds
* @param crc32 the crc32 signature
* @param source_ip_addr the source storage ip address
*/
public FileInfo(boolean fetch_from_server, short file_type, long file_size,
int create_timestamp, int crc32, String source_ip_addr)
{
this.fetch_from_server = fetch_from_server;
this.file_type = file_type;
this.file_size = file_size;
this.create_timestamp = new Date(create_timestamp * 1000L);
this.crc32 = crc32;
this.source_ip_addr = source_ip_addr;
}
/**
* Constructor
*
* @param fetch_from_server if fetch from server flag
* @param file_type the file type
* @param file_size the file size
* @param create_timestamp create timestamp in seconds
* @param crc32 the crc32 signature
* @param source_ip_addr the source storage ip address
*/
public FileInfo(boolean fetch_from_server, short file_type, long file_size,
int create_timestamp, int crc32, String source_ip_addr) {
this.fetch_from_server = fetch_from_server;
this.file_type = file_type;
this.file_size = file_size;
this.create_timestamp = new Date(create_timestamp * 1000L);
this.crc32 = crc32;
this.source_ip_addr = source_ip_addr;
}
/**
* get the fetch_from_server flag
*
* @return the fetch_from_server flag
*/
public boolean getFetchFromServer() {
return this.fetch_from_server;
}
/**
* get the fetch_from_server flag
*
* @return the fetch_from_server flag
*/
public boolean getFetchFromServer() {
return this.fetch_from_server;
}
/**
* set the fetch_from_server flag
*
* @param fetch_from_server the fetch from server flag
*/
public void setFetchFromServer(boolean fetch_from_server) {
this.fetch_from_server = fetch_from_server;
}
/**
* set the fetch_from_server flag
*
* @param fetch_from_server the fetch from server flag
*/
public void setFetchFromServer(boolean fetch_from_server) {
this.fetch_from_server = fetch_from_server;
}
/**
* get the file type
*
* @return the file type
*/
public short getFileType() {
return this.file_type;
}
/**
* get the file type
*
* @return the file type
*/
public short getFileType() {
return this.file_type;
}
/**
* set the file type
*
* @param file_type the file type
*/
public void setFileType(short file_type) {
this.file_type = file_type;
}
/**
* set the file type
*
* @param file_type the file type
*/
public void setFileType(short file_type) {
this.file_type = file_type;
}
/**
* get the source ip address of the file uploaded to
*
* @return the source ip address of the file uploaded to
*/
public String getSourceIpAddr() {
return this.source_ip_addr;
}
/**
* get the source ip address of the file uploaded to
*
* @return the source ip address of the file uploaded to
*/
public String getSourceIpAddr() {
return this.source_ip_addr;
}
/**
* set the source ip address of the file uploaded to
*
* @param source_ip_addr the source ip address
*/
public void setSourceIpAddr(String source_ip_addr) {
this.source_ip_addr = source_ip_addr;
}
/**
* set the source ip address of the file uploaded to
*
* @param source_ip_addr the source ip address
*/
public void setSourceIpAddr(String source_ip_addr) {
this.source_ip_addr = source_ip_addr;
}
/**
* get the file size
*
* @return the file size
*/
public long getFileSize() {
return this.file_size;
}
/**
* get the file size
*
* @return the file size
*/
public long getFileSize() {
return this.file_size;
}
/**
* set the file size
*
* @param file_size the file size
*/
public void setFileSize(long file_size) {
this.file_size = file_size;
}
/**
* set the file size
*
* @param file_size the file size
*/
public void setFileSize(long file_size) {
this.file_size = file_size;
}
/**
* get the create timestamp of the file
*
* @return the create timestamp of the file
*/
public Date getCreateTimestamp() {
return this.create_timestamp;
}
/**
* get the create timestamp of the file
*
* @return the create timestamp of the file
*/
public Date getCreateTimestamp() {
return this.create_timestamp;
}
/**
* set the create timestamp of the file
*
* @param create_timestamp create timestamp in seconds
*/
public void setCreateTimestamp(int create_timestamp) {
this.create_timestamp = new Date(create_timestamp * 1000L);
}
/**
* set the create timestamp of the file
*
* @param create_timestamp create timestamp in seconds
*/
public void setCreateTimestamp(int create_timestamp) {
this.create_timestamp = new Date(create_timestamp * 1000L);
}
/**
* get the file CRC32 signature
*
* @return the file CRC32 signature
*/
public long getCrc32() {
return this.crc32;
}
/**
* get the file CRC32 signature
*
* @return the file CRC32 signature
*/
public long getCrc32() {
return this.crc32;
}
/**
* set the create timestamp of the file
*
* @param crc32 the crc32 signature
*/
public void setCrc32(int crc32) {
this.crc32 = crc32;
}
/**
* set the create timestamp of the file
*
* @param crc32 the crc32 signature
*/
public void setCrc32(int crc32) {
this.crc32 = crc32;
}
/**
* to string
*
* @return string
*/
public String toString() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return "fetch_from_server = " + this.fetch_from_server + ", " +
"file_type = " + this.file_type + ", " +
"source_ip_addr = " + this.source_ip_addr + ", " +
"file_size = " + this.file_size + ", " +
"create_timestamp = " + df.format(this.create_timestamp) + ", " +
"crc32 = " + this.crc32;
}
/**
* to string
*
* @return string
*/
@Override
public String toString() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return "fetch_from_server = " + this.fetch_from_server + ", " +
"file_type = " + this.file_type + ", " +
"source_ip_addr = " + this.source_ip_addr + ", " +
"file_size = " + this.file_size + ", " +
"create_timestamp = " + df.format(this.create_timestamp) + ", " +
"crc32 = " + this.crc32;
}
}
@@ -8,8 +8,9 @@
package csource.fastdfs;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import csource.common.MyException;
import csource.common.NameValuePair;
import java.io.IOException;
import java.io.InputStream;
@@ -25,481 +26,481 @@ import java.util.Arrays;
* @version Version 1.18
*/
public class ProtoCommon {
public static final byte FDFS_PROTO_CMD_QUIT = 82;
public static final byte TRACKER_PROTO_CMD_SERVER_LIST_GROUP = 91;
public static final byte TRACKER_PROTO_CMD_SERVER_LIST_STORAGE = 92;
public static final byte TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE = 93;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE = 101;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE = 102;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE = 103;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE = 104;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ALL = 105;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL = 106;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL = 107;
public static final byte TRACKER_PROTO_CMD_RESP = 100;
public static final byte FDFS_PROTO_CMD_ACTIVE_TEST = 111;
public static final byte STORAGE_PROTO_CMD_UPLOAD_FILE = 11;
public static final byte STORAGE_PROTO_CMD_DELETE_FILE = 12;
public static final byte STORAGE_PROTO_CMD_SET_METADATA = 13;
public static final byte STORAGE_PROTO_CMD_DOWNLOAD_FILE = 14;
public static final byte STORAGE_PROTO_CMD_GET_METADATA = 15;
public static final byte STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE = 21;
public static final byte STORAGE_PROTO_CMD_QUERY_FILE_INFO = 22;
public static final byte STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE = 23; //create appender file
public static final byte STORAGE_PROTO_CMD_APPEND_FILE = 24; //append file
public static final byte STORAGE_PROTO_CMD_MODIFY_FILE = 34; //modify appender file
public static final byte STORAGE_PROTO_CMD_TRUNCATE_FILE = 36; //truncate appender file
public static final byte STORAGE_PROTO_CMD_REGENERATE_APPENDER_FILENAME = 38; //rename appender file to normal file
public static final byte FDFS_PROTO_CMD_QUIT = 82;
public static final byte TRACKER_PROTO_CMD_SERVER_LIST_GROUP = 91;
public static final byte TRACKER_PROTO_CMD_SERVER_LIST_STORAGE = 92;
public static final byte TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE = 93;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE = 101;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE = 102;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE = 103;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE = 104;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ALL = 105;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL = 106;
public static final byte TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL = 107;
public static final byte TRACKER_PROTO_CMD_RESP = 100;
public static final byte FDFS_PROTO_CMD_ACTIVE_TEST = 111;
public static final byte STORAGE_PROTO_CMD_UPLOAD_FILE = 11;
public static final byte STORAGE_PROTO_CMD_DELETE_FILE = 12;
public static final byte STORAGE_PROTO_CMD_SET_METADATA = 13;
public static final byte STORAGE_PROTO_CMD_DOWNLOAD_FILE = 14;
public static final byte STORAGE_PROTO_CMD_GET_METADATA = 15;
public static final byte STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE = 21;
public static final byte STORAGE_PROTO_CMD_QUERY_FILE_INFO = 22;
public static final byte STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE = 23; //create appender file
public static final byte STORAGE_PROTO_CMD_APPEND_FILE = 24; //append file
public static final byte STORAGE_PROTO_CMD_MODIFY_FILE = 34; //modify appender file
public static final byte STORAGE_PROTO_CMD_TRUNCATE_FILE = 36; //truncate appender file
public static final byte STORAGE_PROTO_CMD_REGENERATE_APPENDER_FILENAME = 38; //rename appender file to normal file
public static final byte STORAGE_PROTO_CMD_RESP = TRACKER_PROTO_CMD_RESP;
public static final byte FDFS_STORAGE_STATUS_INIT = 0;
public static final byte FDFS_STORAGE_STATUS_WAIT_SYNC = 1;
public static final byte FDFS_STORAGE_STATUS_SYNCING = 2;
public static final byte FDFS_STORAGE_STATUS_IP_CHANGED = 3;
public static final byte FDFS_STORAGE_STATUS_DELETED = 4;
public static final byte FDFS_STORAGE_STATUS_OFFLINE = 5;
public static final byte FDFS_STORAGE_STATUS_ONLINE = 6;
public static final byte FDFS_STORAGE_STATUS_ACTIVE = 7;
public static final byte FDFS_STORAGE_STATUS_NONE = 99;
/**
* for overwrite all old metadata
*/
public static final byte STORAGE_SET_METADATA_FLAG_OVERWRITE = 'O';
/**
* for replace, insert when the meta item not exist, otherwise update it
*/
public static final byte STORAGE_SET_METADATA_FLAG_MERGE = 'M';
public static final int FDFS_PROTO_PKG_LEN_SIZE = 8;
public static final int FDFS_PROTO_CMD_SIZE = 1;
public static final int FDFS_GROUP_NAME_MAX_LEN = 16;
public static final int FDFS_IPADDR_SIZE = 16;
public static final int FDFS_DOMAIN_NAME_MAX_SIZE = 128;
public static final int FDFS_VERSION_SIZE = 6;
public static final int FDFS_STORAGE_ID_MAX_SIZE = 16;
public static final String FDFS_RECORD_SEPERATOR = "\u0001";
public static final String FDFS_FIELD_SEPERATOR = "\u0002";
public static final int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
+ FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE;
public static final int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
+ FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE;
public static final byte FDFS_FILE_EXT_NAME_MAX_LEN = 6;
public static final byte FDFS_FILE_PREFIX_MAX_LEN = 16;
public static final byte FDFS_FILE_PATH_LEN = 10;
public static final byte FDFS_FILENAME_BASE64_LENGTH = 27;
public static final byte FDFS_TRUNK_FILE_INFO_LEN = 16;
public static final byte ERR_NO_ENOENT = 2;
public static final byte ERR_NO_EIO = 5;
public static final byte ERR_NO_EBUSY = 16;
public static final byte ERR_NO_EINVAL = 22;
public static final byte ERR_NO_ENOSPC = 28;
public static final byte ECONNREFUSED = 61;
public static final byte ERR_NO_EALREADY = 114;
public static final long INFINITE_FILE_SIZE = 256 * 1024L * 1024 * 1024 * 1024 * 1024L;
public static final long APPENDER_FILE_SIZE = INFINITE_FILE_SIZE;
public static final long TRUNK_FILE_MARK_SIZE = 512 * 1024L * 1024 * 1024 * 1024 * 1024L;
public static final long NORMAL_LOGIC_FILENAME_LENGTH = FDFS_FILE_PATH_LEN + FDFS_FILENAME_BASE64_LENGTH + FDFS_FILE_EXT_NAME_MAX_LEN + 1;
public static final long TRUNK_LOGIC_FILENAME_LENGTH = NORMAL_LOGIC_FILENAME_LENGTH + FDFS_TRUNK_FILE_INFO_LEN;
protected static final int PROTO_HEADER_CMD_INDEX = FDFS_PROTO_PKG_LEN_SIZE;
protected static final int PROTO_HEADER_STATUS_INDEX = FDFS_PROTO_PKG_LEN_SIZE + 1;
public static final byte STORAGE_PROTO_CMD_RESP = TRACKER_PROTO_CMD_RESP;
public static final byte FDFS_STORAGE_STATUS_INIT = 0;
public static final byte FDFS_STORAGE_STATUS_WAIT_SYNC = 1;
public static final byte FDFS_STORAGE_STATUS_SYNCING = 2;
public static final byte FDFS_STORAGE_STATUS_IP_CHANGED = 3;
public static final byte FDFS_STORAGE_STATUS_DELETED = 4;
public static final byte FDFS_STORAGE_STATUS_OFFLINE = 5;
public static final byte FDFS_STORAGE_STATUS_ONLINE = 6;
public static final byte FDFS_STORAGE_STATUS_ACTIVE = 7;
public static final byte FDFS_STORAGE_STATUS_NONE = 99;
/**
* for overwrite all old metadata
*/
public static final byte STORAGE_SET_METADATA_FLAG_OVERWRITE = 'O';
/**
* for replace, insert when the meta item not exist, otherwise update it
*/
public static final byte STORAGE_SET_METADATA_FLAG_MERGE = 'M';
public static final int FDFS_PROTO_PKG_LEN_SIZE = 8;
public static final int FDFS_PROTO_CMD_SIZE = 1;
public static final int FDFS_GROUP_NAME_MAX_LEN = 16;
public static final int FDFS_IPADDR_SIZE = 16;
public static final int FDFS_DOMAIN_NAME_MAX_SIZE = 128;
public static final int FDFS_VERSION_SIZE = 6;
public static final int FDFS_STORAGE_ID_MAX_SIZE = 16;
public static final String FDFS_RECORD_SEPERATOR = "\u0001";
public static final String FDFS_FIELD_SEPERATOR = "\u0002";
public static final int TRACKER_QUERY_STORAGE_FETCH_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
+ FDFS_IPADDR_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE;
public static final int TRACKER_QUERY_STORAGE_STORE_BODY_LEN = FDFS_GROUP_NAME_MAX_LEN
+ FDFS_IPADDR_SIZE + FDFS_PROTO_PKG_LEN_SIZE;
public static final byte FDFS_FILE_EXT_NAME_MAX_LEN = 6;
public static final byte FDFS_FILE_PREFIX_MAX_LEN = 16;
public static final byte FDFS_FILE_PATH_LEN = 10;
public static final byte FDFS_FILENAME_BASE64_LENGTH = 27;
public static final byte FDFS_TRUNK_FILE_INFO_LEN = 16;
public static final byte ERR_NO_ENOENT = 2;
public static final byte ERR_NO_EIO = 5;
public static final byte ERR_NO_EBUSY = 16;
public static final byte ERR_NO_EINVAL = 22;
public static final byte ERR_NO_ENOSPC = 28;
public static final byte ECONNREFUSED = 61;
public static final byte ERR_NO_EALREADY = 114;
public static final long INFINITE_FILE_SIZE = 256 * 1024L * 1024 * 1024 * 1024 * 1024L;
public static final long APPENDER_FILE_SIZE = INFINITE_FILE_SIZE;
public static final long TRUNK_FILE_MARK_SIZE = 512 * 1024L * 1024 * 1024 * 1024 * 1024L;
public static final long NORMAL_LOGIC_FILENAME_LENGTH = FDFS_FILE_PATH_LEN + FDFS_FILENAME_BASE64_LENGTH + FDFS_FILE_EXT_NAME_MAX_LEN + 1;
public static final long TRUNK_LOGIC_FILENAME_LENGTH = NORMAL_LOGIC_FILENAME_LENGTH + FDFS_TRUNK_FILE_INFO_LEN;
protected static final int PROTO_HEADER_CMD_INDEX = FDFS_PROTO_PKG_LEN_SIZE;
protected static final int PROTO_HEADER_STATUS_INDEX = FDFS_PROTO_PKG_LEN_SIZE + 1;
private ProtoCommon() {
}
public static String getStorageStatusCaption(byte status) {
switch (status) {
case FDFS_STORAGE_STATUS_INIT:
return "INIT";
case FDFS_STORAGE_STATUS_WAIT_SYNC:
return "WAIT_SYNC";
case FDFS_STORAGE_STATUS_SYNCING:
return "SYNCING";
case FDFS_STORAGE_STATUS_IP_CHANGED:
return "IP_CHANGED";
case FDFS_STORAGE_STATUS_DELETED:
return "DELETED";
case FDFS_STORAGE_STATUS_OFFLINE:
return "OFFLINE";
case FDFS_STORAGE_STATUS_ONLINE:
return "ONLINE";
case FDFS_STORAGE_STATUS_ACTIVE:
return "ACTIVE";
case FDFS_STORAGE_STATUS_NONE:
return "NONE";
default:
return "UNKOWN";
}
}
/**
* pack header by FastDFS transfer protocol
*
* @param cmd which command to send
* @param pkg_len package body length
* @param errno status code, should be (byte)0
* @return packed byte buffer
*/
public static byte[] packHeader(byte cmd, long pkg_len, byte errno) throws UnsupportedEncodingException {
byte[] header;
byte[] hex_len;
header = new byte[FDFS_PROTO_PKG_LEN_SIZE + 2];
Arrays.fill(header, (byte) 0);
hex_len = ProtoCommon.long2buff(pkg_len);
System.arraycopy(hex_len, 0, header, 0, hex_len.length);
header[PROTO_HEADER_CMD_INDEX] = cmd;
header[PROTO_HEADER_STATUS_INDEX] = errno;
return header;
}
/**
* receive pack header
*
* @param in input stream
* @param expect_cmd expect response command
* @param expect_body_len expect response package body length
* @return RecvHeaderInfo: errno and pkg body length
*/
public static RecvHeaderInfo recvHeader(InputStream in, byte expect_cmd, long expect_body_len) throws IOException {
byte[] header;
int bytes;
long pkg_len;
header = new byte[FDFS_PROTO_PKG_LEN_SIZE + 2];
if ((bytes = in.read(header)) != header.length) {
throw new IOException("recv package size " + bytes + " != " + header.length);
private ProtoCommon() {
}
if (header[PROTO_HEADER_CMD_INDEX] != expect_cmd) {
throw new IOException("recv cmd: " + header[PROTO_HEADER_CMD_INDEX] + " is not correct, expect cmd: " + expect_cmd);
public static String getStorageStatusCaption(byte status) {
switch (status) {
case FDFS_STORAGE_STATUS_INIT:
return "INIT";
case FDFS_STORAGE_STATUS_WAIT_SYNC:
return "WAIT_SYNC";
case FDFS_STORAGE_STATUS_SYNCING:
return "SYNCING";
case FDFS_STORAGE_STATUS_IP_CHANGED:
return "IP_CHANGED";
case FDFS_STORAGE_STATUS_DELETED:
return "DELETED";
case FDFS_STORAGE_STATUS_OFFLINE:
return "OFFLINE";
case FDFS_STORAGE_STATUS_ONLINE:
return "ONLINE";
case FDFS_STORAGE_STATUS_ACTIVE:
return "ACTIVE";
case FDFS_STORAGE_STATUS_NONE:
return "NONE";
default:
return "UNKOWN";
}
}
if (header[PROTO_HEADER_STATUS_INDEX] != 0) {
return new RecvHeaderInfo(header[PROTO_HEADER_STATUS_INDEX], 0);
/**
* pack header by FastDFS transfer protocol
*
* @param cmd which command to send
* @param pkg_len package body length
* @param errno status code, should be (byte)0
* @return packed byte buffer
*/
public static byte[] packHeader(byte cmd, long pkg_len, byte errno) throws UnsupportedEncodingException {
byte[] header;
byte[] hex_len;
header = new byte[FDFS_PROTO_PKG_LEN_SIZE + 2];
Arrays.fill(header, (byte) 0);
hex_len = ProtoCommon.long2buff(pkg_len);
System.arraycopy(hex_len, 0, header, 0, hex_len.length);
header[PROTO_HEADER_CMD_INDEX] = cmd;
header[PROTO_HEADER_STATUS_INDEX] = errno;
return header;
}
pkg_len = ProtoCommon.buff2long(header, 0);
if (pkg_len < 0) {
throw new IOException("recv body length: " + pkg_len + " < 0!");
/**
* receive pack header
*
* @param in input stream
* @param expect_cmd expect response command
* @param expect_body_len expect response package body length
* @return RecvHeaderInfo: errno and pkg body length
*/
public static RecvHeaderInfo recvHeader(InputStream in, byte expect_cmd, long expect_body_len) throws IOException {
byte[] header;
int bytes;
long pkg_len;
header = new byte[FDFS_PROTO_PKG_LEN_SIZE + 2];
if ((bytes = in.read(header)) != header.length) {
throw new IOException("recv package size " + bytes + " != " + header.length);
}
if (header[PROTO_HEADER_CMD_INDEX] != expect_cmd) {
throw new IOException("recv cmd: " + header[PROTO_HEADER_CMD_INDEX] + " is not correct, expect cmd: " + expect_cmd);
}
if (header[PROTO_HEADER_STATUS_INDEX] != 0) {
return new RecvHeaderInfo(header[PROTO_HEADER_STATUS_INDEX], 0);
}
pkg_len = ProtoCommon.buff2long(header, 0);
if (pkg_len < 0) {
throw new IOException("recv body length: " + pkg_len + " < 0!");
}
if (expect_body_len >= 0 && pkg_len != expect_body_len) {
throw new IOException("recv body length: " + pkg_len + " is not correct, expect length: " + expect_body_len);
}
return new RecvHeaderInfo((byte) 0, pkg_len);
}
if (expect_body_len >= 0 && pkg_len != expect_body_len) {
throw new IOException("recv body length: " + pkg_len + " is not correct, expect length: " + expect_body_len);
/**
* receive whole pack
*
* @param in input stream
* @param expect_cmd expect response command
* @param expect_body_len expect response package body length
* @return RecvPackageInfo: errno and reponse body(byte buff)
*/
public static RecvPackageInfo recvPackage(InputStream in, byte expect_cmd, long expect_body_len) throws IOException {
RecvHeaderInfo header = recvHeader(in, expect_cmd, expect_body_len);
if (header.errno != 0) {
return new RecvPackageInfo(header.errno, null);
}
byte[] body = new byte[(int) header.body_len];
int totalBytes = 0;
int remainBytes = (int) header.body_len;
int bytes;
while (totalBytes < header.body_len) {
if ((bytes = in.read(body, totalBytes, remainBytes)) < 0) {
break;
}
totalBytes += bytes;
remainBytes -= bytes;
}
if (totalBytes != header.body_len) {
throw new IOException("recv package size " + totalBytes + " != " + header.body_len);
}
return new RecvPackageInfo((byte) 0, body);
}
return new RecvHeaderInfo((byte) 0, pkg_len);
}
/**
* receive whole pack
*
* @param in input stream
* @param expect_cmd expect response command
* @param expect_body_len expect response package body length
* @return RecvPackageInfo: errno and reponse body(byte buff)
*/
public static RecvPackageInfo recvPackage(InputStream in, byte expect_cmd, long expect_body_len) throws IOException {
RecvHeaderInfo header = recvHeader(in, expect_cmd, expect_body_len);
if (header.errno != 0) {
return new RecvPackageInfo(header.errno, null);
/**
* split metadata to name value pair array
*
* @param meta_buff metadata
* @return name value pair array
*/
public static NameValuePair[] split_metadata(String meta_buff) {
return split_metadata(meta_buff, FDFS_RECORD_SEPERATOR, FDFS_FIELD_SEPERATOR);
}
byte[] body = new byte[(int) header.body_len];
int totalBytes = 0;
int remainBytes = (int) header.body_len;
int bytes;
/**
* split metadata to name value pair array
*
* @param meta_buff metadata
* @param recordSeperator record/row seperator
* @param filedSeperator field/column seperator
* @return name value pair array
*/
public static NameValuePair[] split_metadata(String meta_buff,
String recordSeperator, String filedSeperator) {
String[] rows;
String[] cols;
NameValuePair[] meta_list;
while (totalBytes < header.body_len) {
if ((bytes = in.read(body, totalBytes, remainBytes)) < 0) {
break;
}
rows = meta_buff.split(recordSeperator);
meta_list = new NameValuePair[rows.length];
for (int i = 0; i < rows.length; i++) {
cols = rows[i].split(filedSeperator, 2);
meta_list[i] = new NameValuePair(cols[0]);
if (cols.length == 2) {
meta_list[i].setValue(cols[1]);
}
}
totalBytes += bytes;
remainBytes -= bytes;
return meta_list;
}
if (totalBytes != header.body_len) {
throw new IOException("recv package size " + totalBytes + " != " + header.body_len);
/**
* pack metadata array to string
*
* @param meta_list metadata array
* @return packed metadata
*/
public static String pack_metadata(NameValuePair[] meta_list) {
if (meta_list.length == 0) {
return "";
}
StringBuffer sb = new StringBuffer(32 * meta_list.length);
sb.append(meta_list[0].getName()).append(FDFS_FIELD_SEPERATOR).append(meta_list[0].getValue());
for (int i = 1; i < meta_list.length; i++) {
sb.append(FDFS_RECORD_SEPERATOR);
sb.append(meta_list[i].getName()).append(FDFS_FIELD_SEPERATOR).append(meta_list[i].getValue());
}
return sb.toString();
}
return new RecvPackageInfo((byte) 0, body);
}
/**
* split metadata to name value pair array
*
* @param meta_buff metadata
* @return name value pair array
*/
public static NameValuePair[] split_metadata(String meta_buff) {
return split_metadata(meta_buff, FDFS_RECORD_SEPERATOR, FDFS_FIELD_SEPERATOR);
}
/**
* split metadata to name value pair array
*
* @param meta_buff metadata
* @param recordSeperator record/row seperator
* @param filedSeperator field/column seperator
* @return name value pair array
*/
public static NameValuePair[] split_metadata(String meta_buff,
String recordSeperator, String filedSeperator) {
String[] rows;
String[] cols;
NameValuePair[] meta_list;
rows = meta_buff.split(recordSeperator);
meta_list = new NameValuePair[rows.length];
for (int i = 0; i < rows.length; i++) {
cols = rows[i].split(filedSeperator, 2);
meta_list[i] = new NameValuePair(cols[0]);
if (cols.length == 2) {
meta_list[i].setValue(cols[1]);
}
/**
* send quit command to server and close socket
*
* @param sock the Socket object
*/
public static void closeSocket(Socket sock) throws IOException {
byte[] header;
header = packHeader(FDFS_PROTO_CMD_QUIT, 0, (byte) 0);
sock.getOutputStream().write(header);
sock.close();
}
return meta_list;
}
/**
* send ACTIVE_TEST command to server, test if network is ok and the server is alive
*
* @param sock the Socket object
*/
public static boolean activeTest(Socket sock) throws IOException {
byte[] header;
header = packHeader(FDFS_PROTO_CMD_ACTIVE_TEST, 0, (byte) 0);
sock.getOutputStream().write(header);
/**
* pack metadata array to string
*
* @param meta_list metadata array
* @return packed metadata
*/
public static String pack_metadata(NameValuePair[] meta_list) {
if (meta_list.length == 0) {
return "";
RecvHeaderInfo headerInfo = recvHeader(sock.getInputStream(), TRACKER_PROTO_CMD_RESP, 0);
return headerInfo.errno == 0 ? true : false;
}
StringBuffer sb = new StringBuffer(32 * meta_list.length);
sb.append(meta_list[0].getName()).append(FDFS_FIELD_SEPERATOR).append(meta_list[0].getValue());
for (int i = 1; i < meta_list.length; i++) {
sb.append(FDFS_RECORD_SEPERATOR);
sb.append(meta_list[i].getName()).append(FDFS_FIELD_SEPERATOR).append(meta_list[i].getValue());
/**
* long convert to buff (big-endian)
*
* @param n long number
* @return 8 bytes buff
*/
public static byte[] long2buff(long n) {
byte[] bs;
bs = new byte[8];
bs[0] = (byte) ((n >> 56) & 0xFF);
bs[1] = (byte) ((n >> 48) & 0xFF);
bs[2] = (byte) ((n >> 40) & 0xFF);
bs[3] = (byte) ((n >> 32) & 0xFF);
bs[4] = (byte) ((n >> 24) & 0xFF);
bs[5] = (byte) ((n >> 16) & 0xFF);
bs[6] = (byte) ((n >> 8) & 0xFF);
bs[7] = (byte) (n & 0xFF);
return bs;
}
return sb.toString();
}
/**
* send quit command to server and close socket
*
* @param sock the Socket object
*/
public static void closeSocket(Socket sock) throws IOException {
byte[] header;
header = packHeader(FDFS_PROTO_CMD_QUIT, 0, (byte) 0);
sock.getOutputStream().write(header);
sock.close();
}
/**
* send ACTIVE_TEST command to server, test if network is ok and the server is alive
*
* @param sock the Socket object
*/
public static boolean activeTest(Socket sock) throws IOException {
byte[] header;
header = packHeader(FDFS_PROTO_CMD_ACTIVE_TEST, 0, (byte) 0);
sock.getOutputStream().write(header);
RecvHeaderInfo headerInfo = recvHeader(sock.getInputStream(), TRACKER_PROTO_CMD_RESP, 0);
return headerInfo.errno == 0 ? true : false;
}
/**
* long convert to buff (big-endian)
*
* @param n long number
* @return 8 bytes buff
*/
public static byte[] long2buff(long n) {
byte[] bs;
bs = new byte[8];
bs[0] = (byte) ((n >> 56) & 0xFF);
bs[1] = (byte) ((n >> 48) & 0xFF);
bs[2] = (byte) ((n >> 40) & 0xFF);
bs[3] = (byte) ((n >> 32) & 0xFF);
bs[4] = (byte) ((n >> 24) & 0xFF);
bs[5] = (byte) ((n >> 16) & 0xFF);
bs[6] = (byte) ((n >> 8) & 0xFF);
bs[7] = (byte) (n & 0xFF);
return bs;
}
/**
* buff convert to long
*
* @param bs the buffer (big-endian)
* @param offset the start position based 0
* @return long number
*/
public static long buff2long(byte[] bs, int offset) {
return (((long) (bs[offset] >= 0 ? bs[offset] : 256 + bs[offset])) << 56) |
(((long) (bs[offset + 1] >= 0 ? bs[offset + 1] : 256 + bs[offset + 1])) << 48) |
(((long) (bs[offset + 2] >= 0 ? bs[offset + 2] : 256 + bs[offset + 2])) << 40) |
(((long) (bs[offset + 3] >= 0 ? bs[offset + 3] : 256 + bs[offset + 3])) << 32) |
(((long) (bs[offset + 4] >= 0 ? bs[offset + 4] : 256 + bs[offset + 4])) << 24) |
(((long) (bs[offset + 5] >= 0 ? bs[offset + 5] : 256 + bs[offset + 5])) << 16) |
(((long) (bs[offset + 6] >= 0 ? bs[offset + 6] : 256 + bs[offset + 6])) << 8) |
((long) (bs[offset + 7] >= 0 ? bs[offset + 7] : 256 + bs[offset + 7]));
}
/**
* buff convert to int
*
* @param bs the buffer (big-endian)
* @param offset the start position based 0
* @return int number
*/
public static int buff2int(byte[] bs, int offset) {
return (((int) (bs[offset] >= 0 ? bs[offset] : 256 + bs[offset])) << 24) |
(((int) (bs[offset + 1] >= 0 ? bs[offset + 1] : 256 + bs[offset + 1])) << 16) |
(((int) (bs[offset + 2] >= 0 ? bs[offset + 2] : 256 + bs[offset + 2])) << 8) |
((int) (bs[offset + 3] >= 0 ? bs[offset + 3] : 256 + bs[offset + 3]));
}
/**
* buff convert to ip address
*
* @param bs the buffer (big-endian)
* @param offset the start position based 0
* @return ip address
*/
public static String getIpAddress(byte[] bs, int offset) {
if (bs[0] == 0 || bs[3] == 0) //storage server ID
{
return "";
/**
* buff convert to long
*
* @param bs the buffer (big-endian)
* @param offset the start position based 0
* @return long number
*/
public static long buff2long(byte[] bs, int offset) {
return (((long) (bs[offset] >= 0 ? bs[offset] : 256 + bs[offset])) << 56) |
(((long) (bs[offset + 1] >= 0 ? bs[offset + 1] : 256 + bs[offset + 1])) << 48) |
(((long) (bs[offset + 2] >= 0 ? bs[offset + 2] : 256 + bs[offset + 2])) << 40) |
(((long) (bs[offset + 3] >= 0 ? bs[offset + 3] : 256 + bs[offset + 3])) << 32) |
(((long) (bs[offset + 4] >= 0 ? bs[offset + 4] : 256 + bs[offset + 4])) << 24) |
(((long) (bs[offset + 5] >= 0 ? bs[offset + 5] : 256 + bs[offset + 5])) << 16) |
(((long) (bs[offset + 6] >= 0 ? bs[offset + 6] : 256 + bs[offset + 6])) << 8) |
((long) (bs[offset + 7] >= 0 ? bs[offset + 7] : 256 + bs[offset + 7]));
}
int n;
StringBuilder sbResult = new StringBuilder(16);
for (int i = offset; i < offset + 4; i++) {
n = (bs[i] >= 0) ? bs[i] : 256 + bs[i];
if (sbResult.length() > 0) {
sbResult.append(".");
}
sbResult.append(String.valueOf(n));
/**
* buff convert to int
*
* @param bs the buffer (big-endian)
* @param offset the start position based 0
* @return int number
*/
public static int buff2int(byte[] bs, int offset) {
return (((int) (bs[offset] >= 0 ? bs[offset] : 256 + bs[offset])) << 24) |
(((int) (bs[offset + 1] >= 0 ? bs[offset + 1] : 256 + bs[offset + 1])) << 16) |
(((int) (bs[offset + 2] >= 0 ? bs[offset + 2] : 256 + bs[offset + 2])) << 8) |
((int) (bs[offset + 3] >= 0 ? bs[offset + 3] : 256 + bs[offset + 3]));
}
return sbResult.toString();
}
/**
* buff convert to ip address
*
* @param bs the buffer (big-endian)
* @param offset the start position based 0
* @return ip address
*/
public static String getIpAddress(byte[] bs, int offset) {
if (bs[0] == 0 || bs[3] == 0) //storage server ID
{
return "";
}
/**
* md5 function
*
* @param source the input buffer
* @return md5 string
*/
public static String md5(byte[] source) throws NoSuchAlgorithmException {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
md.update(source);
byte tmp[] = md.digest();
char str[] = new char[32];
int k = 0;
for (int i = 0; i < 16; i++) {
str[k++] = hexDigits[tmp[i] >>> 4 & 0xf];
str[k++] = hexDigits[tmp[i] & 0xf];
int n;
StringBuilder sbResult = new StringBuilder(16);
for (int i = offset; i < offset + 4; i++) {
n = (bs[i] >= 0) ? bs[i] : 256 + bs[i];
if (sbResult.length() > 0) {
sbResult.append(".");
}
sbResult.append(String.valueOf(n));
}
return sbResult.toString();
}
return new String(str);
}
/**
* md5 function
*
* @param source the input buffer
* @return md5 string
*/
public static String md5(byte[] source) throws NoSuchAlgorithmException {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
md.update(source);
byte tmp[] = md.digest();
char str[] = new char[32];
int k = 0;
for (int i = 0; i < 16; i++) {
str[k++] = hexDigits[tmp[i] >>> 4 & 0xf];
str[k++] = hexDigits[tmp[i] & 0xf];
}
/**
* get token for file URL
*
* @param remote_filename the filename return by FastDFS server
* @param ts unix timestamp, unit: second
* @param secret_key the secret key
* @return token string
*/
public static String getToken(String remote_filename, int ts, String secret_key) throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException {
byte[] bsFilename = remote_filename.getBytes(ClientGlobal.g_charset);
byte[] bsKey = secret_key.getBytes(ClientGlobal.g_charset);
byte[] bsTimestamp = (new Integer(ts)).toString().getBytes(ClientGlobal.g_charset);
byte[] buff = new byte[bsFilename.length + bsKey.length + bsTimestamp.length];
System.arraycopy(bsFilename, 0, buff, 0, bsFilename.length);
System.arraycopy(bsKey, 0, buff, bsFilename.length, bsKey.length);
System.arraycopy(bsTimestamp, 0, buff, bsFilename.length + bsKey.length, bsTimestamp.length);
return md5(buff);
}
/**
* generate slave filename
*
* @param master_filename the master filename to generate the slave filename
* @param prefix_name the prefix name to generate the slave filename
* @param ext_name the extension name of slave filename, null for same as the master extension name
* @return slave filename string
*/
public static String genSlaveFilename(String master_filename,
String prefix_name, String ext_name) throws MyException {
String true_ext_name;
int dotIndex;
if (master_filename.length() < 28 + FDFS_FILE_EXT_NAME_MAX_LEN) {
throw new MyException("master filename \"" + master_filename + "\" is invalid");
return new String(str);
}
dotIndex = master_filename.indexOf('.', master_filename.length() - (FDFS_FILE_EXT_NAME_MAX_LEN + 1));
if (ext_name != null) {
if (ext_name.length() == 0) {
true_ext_name = "";
} else if (ext_name.charAt(0) == '.') {
true_ext_name = ext_name;
} else {
true_ext_name = "." + ext_name;
}
} else {
if (dotIndex < 0) {
true_ext_name = "";
} else {
true_ext_name = master_filename.substring(dotIndex);
}
/**
* get token for file URL
*
* @param remote_filename the filename return by FastDFS server
* @param ts unix timestamp, unit: second
* @param secret_key the secret key
* @return token string
*/
public static String getToken(String remote_filename, int ts, String secret_key) throws UnsupportedEncodingException, NoSuchAlgorithmException, MyException {
byte[] bsFilename = remote_filename.getBytes(ClientGlobal.g_charset);
byte[] bsKey = secret_key.getBytes(ClientGlobal.g_charset);
byte[] bsTimestamp = (new Integer(ts)).toString().getBytes(ClientGlobal.g_charset);
byte[] buff = new byte[bsFilename.length + bsKey.length + bsTimestamp.length];
System.arraycopy(bsFilename, 0, buff, 0, bsFilename.length);
System.arraycopy(bsKey, 0, buff, bsFilename.length, bsKey.length);
System.arraycopy(bsTimestamp, 0, buff, bsFilename.length + bsKey.length, bsTimestamp.length);
return md5(buff);
}
if (true_ext_name.length() == 0 && prefix_name.equals("-m")) {
throw new MyException("prefix_name \"" + prefix_name + "\" is invalid");
/**
* generate slave filename
*
* @param master_filename the master filename to generate the slave filename
* @param prefix_name the prefix name to generate the slave filename
* @param ext_name the extension name of slave filename, null for same as the master extension name
* @return slave filename string
*/
public static String genSlaveFilename(String master_filename,
String prefix_name, String ext_name) throws MyException {
String true_ext_name;
int dotIndex;
if (master_filename.length() < 28 + FDFS_FILE_EXT_NAME_MAX_LEN) {
throw new MyException("master filename \"" + master_filename + "\" is invalid");
}
dotIndex = master_filename.indexOf('.', master_filename.length() - (FDFS_FILE_EXT_NAME_MAX_LEN + 1));
if (ext_name != null) {
if (ext_name.length() == 0) {
true_ext_name = "";
} else if (ext_name.charAt(0) == '.') {
true_ext_name = ext_name;
} else {
true_ext_name = "." + ext_name;
}
} else {
if (dotIndex < 0) {
true_ext_name = "";
} else {
true_ext_name = master_filename.substring(dotIndex);
}
}
if (true_ext_name.length() == 0 && prefix_name.equals("-m")) {
throw new MyException("prefix_name \"" + prefix_name + "\" is invalid");
}
if (dotIndex < 0) {
return master_filename + prefix_name + true_ext_name;
} else {
return master_filename.substring(0, dotIndex) + prefix_name + true_ext_name;
}
}
if (dotIndex < 0) {
return master_filename + prefix_name + true_ext_name;
} else {
return master_filename.substring(0, dotIndex) + prefix_name + true_ext_name;
/**
* receive package info
*/
public static class RecvPackageInfo {
public byte errno;
public byte[] body;
public RecvPackageInfo(byte errno, byte[] body) {
this.errno = errno;
this.body = body;
}
}
}
/**
* receive package info
*/
public static class RecvPackageInfo {
public byte errno;
public byte[] body;
/**
* receive header info
*/
public static class RecvHeaderInfo {
public byte errno;
public long body_len;
public RecvPackageInfo(byte errno, byte[] body) {
this.errno = errno;
this.body = body;
public RecvHeaderInfo(byte errno, long body_len) {
this.errno = errno;
this.body_len = body_len;
}
}
}
/**
* receive header info
*/
public static class RecvHeaderInfo {
public byte errno;
public long body_len;
public RecvHeaderInfo(byte errno, long body_len) {
this.errno = errno;
this.body_len = body_len;
}
}
}
@@ -8,10 +8,10 @@
package csource.fastdfs;
import org.csource.common.Base64;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.pool.Connection;
import csource.common.Base64;
import csource.common.MyException;
import csource.common.NameValuePair;
import csource.fastdfs.pool.Connection;
import java.io.*;
import java.util.Arrays;
@@ -705,8 +705,8 @@ public class StorageClient {
* return null if fail
*/
public String[] do_upload_file(byte cmd, String group_name, String master_filename,
String prefix_name, String file_ext_name, long file_size, UploadCallback callback,
NameValuePair[] meta_list) throws IOException, MyException {
String prefix_name, String file_ext_name, long file_size, UploadCallback callback,
NameValuePair[] meta_list) throws IOException, MyException {
byte[] header;
byte[] ext_name_bs;
String new_group_name;
@@ -1797,6 +1797,7 @@ public class StorageClient {
* @param out output stream for writing file content
* @return 0 success, return none zero(errno) if fail
*/
@Override
public int send(OutputStream out) throws IOException {
out.write(this.fileBuff, this.offset, this.length);
@@ -8,8 +8,9 @@
package csource.fastdfs;
import org.csource.common.MyException;
import org.csource.fastdfs.pool.Connection;
import csource.common.MyException;
import csource.fastdfs.pool.Connection;
import java.io.IOException;
import java.io.OutputStream;
@@ -8,10 +8,10 @@
package csource.fastdfs;
import org.csource.common.MyException;
import org.csource.fastdfs.pool.Connection;
import org.csource.fastdfs.pool.ConnectionFactory;
import org.csource.fastdfs.pool.ConnectionPool;
import csource.common.MyException;
import csource.fastdfs.pool.Connection;
import csource.fastdfs.pool.ConnectionFactory;
import csource.fastdfs.pool.ConnectionPool;
import java.io.IOException;
import java.net.InetSocketAddress;
@@ -46,6 +46,7 @@ public class TrackerServer {
}
return connection;
}
/**
* get the server info
*
@@ -11,45 +11,45 @@ import java.io.OutputStream;
* @version Version 1.11
*/
public class UploadStream implements UploadCallback {
private InputStream inputStream; //input stream for reading
private long fileSize = 0; //size of the uploaded file
private InputStream inputStream; //input stream for reading
private long fileSize = 0; //size of the uploaded file
/**
* constructor
*
* @param inputStream input stream for uploading
* @param fileSize size of uploaded file
*/
public UploadStream(InputStream inputStream, long fileSize) {
super();
this.inputStream = inputStream;
this.fileSize = fileSize;
}
/**
* send file content callback function, be called only once when the file uploaded
*
* @param out output stream for writing file content
* @return 0 success, return none zero(errno) if fail
*/
public int send(OutputStream out) throws IOException {
long remainBytes = fileSize;
byte[] buff = new byte[256 * 1024];
int bytes;
while (remainBytes > 0) {
try {
if ((bytes = inputStream.read(buff, 0, remainBytes > buff.length ? buff.length : (int) remainBytes)) < 0) {
return -1;
}
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}
out.write(buff, 0, bytes);
remainBytes -= bytes;
/**
* constructor
*
* @param inputStream input stream for uploading
* @param fileSize size of uploaded file
*/
public UploadStream(InputStream inputStream, long fileSize) {
super();
this.inputStream = inputStream;
this.fileSize = fileSize;
}
return 0;
}
/**
* send file content callback function, be called only once when the file uploaded
*
* @param out output stream for writing file content
* @return 0 success, return none zero(errno) if fail
*/
public int send(OutputStream out) throws IOException {
long remainBytes = fileSize;
byte[] buff = new byte[256 * 1024];
int bytes;
while (remainBytes > 0) {
try {
if ((bytes = inputStream.read(buff, 0, remainBytes > buff.length ? buff.length : (int) remainBytes)) < 0) {
return -1;
}
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}
out.write(buff, 0, bytes);
remainBytes -= bytes;
}
return 0;
}
}
@@ -1,7 +1,8 @@
package csource.fastdfs.pool;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ProtoCommon;
import csource.fastdfs.ClientGlobal;
import csource.fastdfs.ProtoCommon;
import java.io.IOException;
import java.io.InputStream;
@@ -48,7 +49,6 @@ public class Connection {
}
/**
*
* @throws IOException
*/
public void close() throws IOException {
@@ -97,6 +97,7 @@ public class Connection {
}
return isConnected;
}
public boolean isAvaliable() {
if (isConnected()) {
if (sock.getPort() == 0) {
@@ -1,5 +1,7 @@
package csource.fastdfs.pool;
import csource.common.MyException;
import csource.fastdfs.ClientGlobal;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
@@ -1,7 +1,7 @@
package csource.fastdfs.pool;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import csource.common.MyException;
import csource.fastdfs.ClientGlobal;
import java.io.IOException;
import java.net.InetSocketAddress;
@@ -1,6 +1,6 @@
package csource.fastdfs.pool;
import org.csource.common.MyException;
import csource.common.MyException;
import java.io.IOException;
import java.net.InetSocketAddress;
+3 -3
View File
@@ -34,9 +34,9 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>${fastdfs-client-java.version}</version>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-fast-dfs-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>