From feae52fe040a17e785cc1dbed8abf8a80a0fa512 Mon Sep 17 00:00:00 2001 From: wyl <959814898@qq.com> Date: Sun, 30 Apr 2023 15:14:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pom.xml | 32 +- .../aivfo-dfs-client-spring-boot-core/pom.xml | 4 +- .../aivfo-fast-dfs-client/pom.xml | 7 +- .../java/csource/fastdfs/ClientGlobal.java | 584 +++---- .../csource/fastdfs/DownloadCallback.java | 18 +- .../java/csource/fastdfs/DownloadStream.java | 1 + .../main/java/csource/fastdfs/FileInfo.java | 278 ++-- .../java/csource/fastdfs/ProtoCommon.java | 869 +++++------ .../java/csource/fastdfs/StorageClient.java | 13 +- .../java/csource/fastdfs/StorageClient1.java | 1367 +++++++++-------- .../java/csource/fastdfs/TrackerClient.java | 5 +- .../java/csource/fastdfs/TrackerServer.java | 9 +- .../java/csource/fastdfs/UploadStream.java | 76 +- .../java/csource/fastdfs/pool/Connection.java | 7 +- .../fastdfs/pool/ConnectionFactory.java | 2 + .../fastdfs/pool/ConnectionManager.java | 4 +- .../csource/fastdfs/pool/ConnectionPool.java | 2 +- module/aivfo-dfs-client-spring-boot/pom.xml | 6 +- 18 files changed, 1634 insertions(+), 1650 deletions(-) diff --git a/module/aivfo-dependencies-spring-boot-module/pom.xml b/module/aivfo-dependencies-spring-boot-module/pom.xml index 8150695..850d95f 100644 --- a/module/aivfo-dependencies-spring-boot-module/pom.xml +++ b/module/aivfo-dependencies-spring-boot-module/pom.xml @@ -18,16 +18,6 @@ aivfo-log-spring-boot-starter 1.0.0-SNAPSHOT - - com.aivfo - aivfo-log-spring-boot-core - 1.0.0-SNAPSHOT - - - com.aivfo - aivfo-log-spring-boot-autoconfigure - 1.0.0-SNAPSHOT - com.aivfo aivfo-mqtt-spring-boot-starter @@ -35,27 +25,7 @@ com.aivfo - aivfo-mqtt-spring-boot-core - 1.0.0-SNAPSHOT - - - com.aivfo - aivfo-mqtt-spring-boot-autoconfigure - 1.0.0-SNAPSHOT - - - com.aivfo - aivfo-fastDFS-spring-boot-starter - 1.0.0-SNAPSHOT - - - com.aivfo - aivfo-fastDFS-spring-boot-core - 1.0.0-SNAPSHOT - - - com.aivfo - aivfo-fastDFS-spring-boot-autoconfigure + aivfo-fast-dfs-client 1.0.0-SNAPSHOT diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-dfs-client-spring-boot-core/pom.xml b/module/aivfo-dfs-client-spring-boot/aivfo-dfs-client-spring-boot-core/pom.xml index 67045ab..8cfce83 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-dfs-client-spring-boot-core/pom.xml +++ b/module/aivfo-dfs-client-spring-boot/aivfo-dfs-client-spring-boot-core/pom.xml @@ -13,8 +13,8 @@ - org.csource - fastdfs-client-java + com.aivfo + aivfo-fast-dfs-client \ No newline at end of file diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/pom.xml b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/pom.xml index bff834a..155cdab 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/pom.xml +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/pom.xml @@ -10,6 +10,11 @@ 4.0.0 aivfo-fast-dfs-client - + + + org.apache.commons + commons-pool2 + + \ No newline at end of file diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/ClientGlobal.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/ClientGlobal.java index 4ca48ee..a219bdb 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/ClientGlobal.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/ClientGlobal.java @@ -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 list = new ArrayList(); + 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 list = new ArrayList(); - 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}"; - } } diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/DownloadCallback.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/DownloadCallback.java index cf4ff3e..2d08268 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/DownloadCallback.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/DownloadCallback.java @@ -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); } diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/DownloadStream.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/DownloadStream.java index 0f5e888..b3628b8 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/DownloadStream.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/DownloadStream.java @@ -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); diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/FileInfo.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/FileInfo.java index c4dc592..4466a77 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/FileInfo.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/FileInfo.java @@ -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; + } } diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/ProtoCommon.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/ProtoCommon.java index eb8cea9..e206225 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/ProtoCommon.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/ProtoCommon.java @@ -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; - } - } } diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/StorageClient.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/StorageClient.java index 3f6b1e4..2406f6a 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/StorageClient.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/StorageClient.java @@ -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); diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/StorageClient1.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/StorageClient1.java index 7f54aec..8364b8f 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/StorageClient1.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/StorageClient1.java @@ -8,758 +8,759 @@ 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; /** * Storage client for 1 field file id: combined group name and filename * Note: the instance of this class is NOT thread safe !!! - * if not necessary, do NOT set storage server instance + * if not necessary, do NOT set storage server instance * * @author Happy Fish / YuQing * @version Version 1.27 */ public class StorageClient1 extends StorageClient { - public static final String SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR = "/"; + public static final String SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR = "/"; - /** - * constructor - */ - public StorageClient1() { - super(); - } - - /** - * constructor with trackerServer - * - * @param trackerServer the tracker server, can be null - */ - public StorageClient1(TrackerServer trackerServer) { - super(trackerServer); - } - - /** - * constructor with trackerServer and storageServer - * NOTE: if not necessary, do NOT set storage server instance - * - * @param trackerServer the tracker server, can be null - * @param storageServer the storage server, can be null - */ - public StorageClient1(TrackerServer trackerServer, StorageServer storageServer) { - super(trackerServer, storageServer); - } - - public static byte split_file_id(String file_id, String[] results) { - int pos = file_id.indexOf(SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR); - if ((pos <= 0) || (pos == file_id.length() - 1)) { - return ProtoCommon.ERR_NO_EINVAL; + /** + * constructor + */ + public StorageClient1() { + super(); } - results[0] = file_id.substring(0, pos); //group name - results[1] = file_id.substring(pos + 1); //file name - return 0; - } - - /** - * upload file to storage server (by file name) - * - * @param local_filename local filename to upload - * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_file(local_filename, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload file to storage server (by file name) - * - * @param group_name the group name to upload file to, can be empty - * @param local_filename local filename to upload - * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(String group_name, String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_file(group_name, local_filename, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload file to storage server (by file buff) - * - * @param file_buff file content/buff - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(byte[] file_buff, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_file(file_buff, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload file to storage server (by file buff) - * - * @param group_name the group name to upload file to, can be empty - * @param file_buff file content/buff - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(String group_name, byte[] file_buff, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_file(group_name, file_buff, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload file to storage server (by callback) - * - * @param group_name the group name to upload file to, can be empty - * @param file_size the file size - * @param callback the write data callback object - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(String group_name, long file_size, - UploadCallback callback, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_file(group_name, file_size, callback, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload appender file to storage server (by file name) - * - * @param local_filename local filename to upload - * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_appender_file1(String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_appender_file(local_filename, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload appender file to storage server (by file name) - * - * @param group_name the group name to upload file to, can be empty - * @param local_filename local filename to upload - * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_appender_file1(String group_name, String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_appender_file(group_name, local_filename, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload appender file to storage server (by file buff) - * - * @param file_buff file content/buff - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_appender_file1(byte[] file_buff, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_appender_file(file_buff, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload appender file to storage server (by file buff) - * - * @param group_name the group name to upload file to, can be empty - * @param file_buff file content/buff - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_appender_file1(String group_name, byte[] file_buff, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_appender_file(group_name, file_buff, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload appender file to storage server (by callback) - * - * @param group_name the group name to upload file to, can be empty - * @param file_size the file size - * @param callback the write data callback object - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_appender_file1(String group_name, long file_size, - UploadCallback callback, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String parts[] = this.upload_appender_file(group_name, file_size, callback, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload file to storage server (by file name, slave file mode) - * - * @param master_file_id the master file id to generate the slave file - * @param prefix_name the prefix name to generate the slave file - * @param local_filename local filename to upload - * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(String master_file_id, String prefix_name, - String local_filename, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(master_file_id, parts); - if (this.errno != 0) { - return null; + /** + * constructor with trackerServer + * + * @param trackerServer the tracker server, can be null + */ + public StorageClient1(TrackerServer trackerServer) { + super(trackerServer); } - parts = this.upload_file(parts[0], parts[1], prefix_name, - local_filename, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload file to storage server (by file buff, slave file mode) - * - * @param master_file_id the master file id to generate the slave file - * @param prefix_name the prefix name to generate the slave file - * @param file_buff file content/buff - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(String master_file_id, String prefix_name, - byte[] file_buff, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(master_file_id, parts); - if (this.errno != 0) { - return null; + /** + * constructor with trackerServer and storageServer + * NOTE: if not necessary, do NOT set storage server instance + * + * @param trackerServer the tracker server, can be null + * @param storageServer the storage server, can be null + */ + public StorageClient1(TrackerServer trackerServer, StorageServer storageServer) { + super(trackerServer, storageServer); } - parts = this.upload_file(parts[0], parts[1], prefix_name, file_buff, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } + public static byte split_file_id(String file_id, String[] results) { + int pos = file_id.indexOf(SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR); + if ((pos <= 0) || (pos == file_id.length() - 1)) { + return ProtoCommon.ERR_NO_EINVAL; + } - /** - * upload file to storage server (by file buff, slave file mode) - * - * @param master_file_id the master file id to generate the slave file - * @param prefix_name the prefix name to generate the slave file - * @param file_buff file content/buff - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(String master_file_id, String prefix_name, - byte[] file_buff, int offset, int length, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(master_file_id, parts); - if (this.errno != 0) { - return null; + results[0] = file_id.substring(0, pos); //group name + results[1] = file_id.substring(pos + 1); //file name + return 0; } - parts = this.upload_file(parts[0], parts[1], prefix_name, file_buff, - offset, length, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * upload file to storage server (by callback) - * - * @param master_file_id the master file id to generate the slave file - * @param prefix_name the prefix name to generate the slave file - * @param file_size the file size - * @param callback the write data callback object - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return file id(including group name and filename) if success,
- * return null if fail - */ - public String upload_file1(String master_file_id, String prefix_name, long file_size, - UploadCallback callback, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(master_file_id, parts); - if (this.errno != 0) { - return null; + /** + * upload file to storage server (by file name) + * + * @param local_filename local filename to upload + * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_file(local_filename, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - parts = this.upload_file(parts[0], parts[1], prefix_name, file_size, callback, file_ext_name, meta_list); - if (parts != null) { - return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; - } else { - return null; - } - } - - /** - * append file to storage server (by file name) - * - * @param appender_file_id the appender file id - * @param local_filename local filename to append - * @return 0 for success, != 0 for error (error no) - */ - public int append_file1(String appender_file_id, String local_filename) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + /** + * upload file to storage server (by file name) + * + * @param group_name the group name to upload file to, can be empty + * @param local_filename local filename to upload + * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(String group_name, String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_file(group_name, local_filename, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.append_file(parts[0], parts[1], local_filename); - } - - /** - * append file to storage server (by file buff) - * - * @param appender_file_id the appender file id - * @param file_buff file content/buff - * @return 0 for success, != 0 for error (error no) - */ - public int append_file1(String appender_file_id, byte[] file_buff) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + /** + * upload file to storage server (by file buff) + * + * @param file_buff file content/buff + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(byte[] file_buff, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_file(file_buff, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.append_file(parts[0], parts[1], file_buff); - } - - /** - * append file to storage server (by file buff) - * - * @param appender_file_id the appender file id - * @param file_buff file content/buffer - * @param offset start offset of the buffer - * @param length the length of the buffer to append - * @return 0 for success, != 0 for error (error no) - */ - public int append_file1(String appender_file_id, byte[] file_buff, int offset, int length) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + /** + * upload file to storage server (by file buff) + * + * @param group_name the group name to upload file to, can be empty + * @param file_buff file content/buff + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(String group_name, byte[] file_buff, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_file(group_name, file_buff, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.append_file(parts[0], parts[1], file_buff, offset, length); - } - - /** - * append file to storage server (by callback) - * - * @param appender_file_id the appender file id - * @param file_size the file size - * @param callback the write data callback object - * @return 0 for success, != 0 for error (error no) - */ - public int append_file1(String appender_file_id, long file_size, UploadCallback callback) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + /** + * upload file to storage server (by callback) + * + * @param group_name the group name to upload file to, can be empty + * @param file_size the file size + * @param callback the write data callback object + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(String group_name, long file_size, + UploadCallback callback, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_file(group_name, file_size, callback, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.append_file(parts[0], parts[1], file_size, callback); - } - - /** - * modify appender file to storage server (by file name) - * - * @param appender_file_id the appender file id - * @param file_offset the offset of appender file - * @param local_filename local filename to append - * @return 0 for success, != 0 for error (error no) - */ - public int modify_file1(String appender_file_id, - long file_offset, String local_filename) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + /** + * upload appender file to storage server (by file name) + * + * @param local_filename local filename to upload + * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_appender_file1(String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_appender_file(local_filename, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.modify_file(parts[0], parts[1], file_offset, local_filename); - } - - /** - * modify appender file to storage server (by file buff) - * - * @param appender_file_id the appender file id - * @param file_offset the offset of appender file - * @param file_buff file content/buff - * @return 0 for success, != 0 for error (error no) - */ - public int modify_file1(String appender_file_id, - long file_offset, byte[] file_buff) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + /** + * upload appender file to storage server (by file name) + * + * @param group_name the group name to upload file to, can be empty + * @param local_filename local filename to upload + * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_appender_file1(String group_name, String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_appender_file(group_name, local_filename, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.modify_file(parts[0], parts[1], file_offset, file_buff); - } - - /** - * modify appender file to storage server (by file buff) - * - * @param appender_file_id the appender file id - * @param file_offset the offset of appender file - * @param file_buff file content/buff - * @param buffer_offset start offset of the buff - * @param buffer_length the length of buff to modify - * @return 0 for success, != 0 for error (error no) - */ - public int modify_file1(String appender_file_id, - long file_offset, byte[] file_buff, int buffer_offset, int buffer_length) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + /** + * upload appender file to storage server (by file buff) + * + * @param file_buff file content/buff + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_appender_file1(byte[] file_buff, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_appender_file(file_buff, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.modify_file(parts[0], parts[1], file_offset, - file_buff, buffer_offset, buffer_length); - } - - /** - * modify appender file to storage server (by callback) - * - * @param appender_file_id the appender file id - * @param file_offset the offset of appender file - * @param modify_size the modify size - * @param callback the write data callback object - * @return 0 for success, != 0 for error (error no) - */ - public int modify_file1(String appender_file_id, - long file_offset, long modify_size, UploadCallback callback) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + /** + * upload appender file to storage server (by file buff) + * + * @param group_name the group name to upload file to, can be empty + * @param file_buff file content/buff + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_appender_file1(String group_name, byte[] file_buff, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_appender_file(group_name, file_buff, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.modify_file(parts[0], parts[1], file_offset, modify_size, callback); - } - - /** - * regenerate filename for appender file - * - * @param appender_file_id the appender file id - * @return the regenerated file id, return null if fail - */ - public String regenerate_appender_filename1(String appender_file_id) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return null; + /** + * upload appender file to storage server (by callback) + * + * @param group_name the group name to upload file to, can be empty + * @param file_size the file size + * @param callback the write data callback object + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_appender_file1(String group_name, long file_size, + UploadCallback callback, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String parts[] = this.upload_appender_file(group_name, file_size, callback, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - String[] new_parts = this.regenerate_appender_filename(parts[0], parts[1]); - if (new_parts != null) { - return new_parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + new_parts[1]; - } else { - return null; - } - } + /** + * upload file to storage server (by file name, slave file mode) + * + * @param master_file_id the master file id to generate the slave file + * @param prefix_name the prefix name to generate the slave file + * @param local_filename local filename to upload + * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(String master_file_id, String prefix_name, + String local_filename, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(master_file_id, parts); + if (this.errno != 0) { + return null; + } - /** - * delete file from storage server - * - * @param file_id the file id(including group name and filename) - * @return 0 for success, none zero for fail (error code) - */ - public int delete_file1(String file_id) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(file_id, parts); - if (this.errno != 0) { - return this.errno; + parts = this.upload_file(parts[0], parts[1], prefix_name, + local_filename, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.delete_file(parts[0], parts[1]); - } + /** + * upload file to storage server (by file buff, slave file mode) + * + * @param master_file_id the master file id to generate the slave file + * @param prefix_name the prefix name to generate the slave file + * @param file_buff file content/buff + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(String master_file_id, String prefix_name, + byte[] file_buff, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(master_file_id, parts); + if (this.errno != 0) { + return null; + } - /** - * truncate appender file to size 0 from storage server - * - * @param appender_file_id the appender file id - * @return 0 for success, none zero for fail (error code) - */ - public int truncate_file1(String appender_file_id) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + parts = this.upload_file(parts[0], parts[1], prefix_name, file_buff, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.truncate_file(parts[0], parts[1]); - } + /** + * upload file to storage server (by file buff, slave file mode) + * + * @param master_file_id the master file id to generate the slave file + * @param prefix_name the prefix name to generate the slave file + * @param file_buff file content/buff + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(String master_file_id, String prefix_name, + byte[] file_buff, int offset, int length, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(master_file_id, parts); + if (this.errno != 0) { + return null; + } - /** - * truncate appender file from storage server - * - * @param appender_file_id the appender file id - * @param truncated_file_size truncated file size - * @return 0 for success, none zero for fail (error code) - */ - public int truncate_file1(String appender_file_id, long truncated_file_size) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(appender_file_id, parts); - if (this.errno != 0) { - return this.errno; + parts = this.upload_file(parts[0], parts[1], prefix_name, file_buff, + offset, length, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.truncate_file(parts[0], parts[1], truncated_file_size); - } + /** + * upload file to storage server (by callback) + * + * @param master_file_id the master file id to generate the slave file + * @param prefix_name the prefix name to generate the slave file + * @param file_size the file size + * @param callback the write data callback object + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return file id(including group name and filename) if success,
+ * return null if fail + */ + public String upload_file1(String master_file_id, String prefix_name, long file_size, + UploadCallback callback, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(master_file_id, parts); + if (this.errno != 0) { + return null; + } - /** - * download file from storage server - * - * @param file_id the file id(including group name and filename) - * @return file content/buffer, return null if fail - */ - public byte[] download_file1(String file_id) throws IOException, MyException { - final long file_offset = 0; - final long download_bytes = 0; - - return this.download_file1(file_id, file_offset, download_bytes); - } - - /** - * download file from storage server - * - * @param file_id the file id(including group name and filename) - * @param file_offset the start offset of the file - * @param download_bytes download bytes, 0 for remain bytes from offset - * @return file content/buff, return null if fail - */ - public byte[] download_file1(String file_id, long file_offset, long download_bytes) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(file_id, parts); - if (this.errno != 0) { - return null; + parts = this.upload_file(parts[0], parts[1], prefix_name, file_size, callback, file_ext_name, meta_list); + if (parts != null) { + return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1]; + } else { + return null; + } } - return this.download_file(parts[0], parts[1], file_offset, download_bytes); - } + /** + * append file to storage server (by file name) + * + * @param appender_file_id the appender file id + * @param local_filename local filename to append + * @return 0 for success, != 0 for error (error no) + */ + public int append_file1(String appender_file_id, String local_filename) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } - /** - * download file from storage server - * - * @param file_id the file id(including group name and filename) - * @param local_filename the filename on local - * @return 0 success, return none zero errno if fail - */ - public int download_file1(String file_id, String local_filename) throws IOException, MyException { - final long file_offset = 0; - final long download_bytes = 0; - - return this.download_file1(file_id, file_offset, download_bytes, local_filename); - } - - /** - * download file from storage server - * - * @param file_id the file id(including group name and filename) - * @param file_offset the start offset of the file - * @param download_bytes download bytes, 0 for remain bytes from offset - * @param local_filename the filename on local - * @return 0 success, return none zero errno if fail - */ - public int download_file1(String file_id, long file_offset, long download_bytes, String local_filename) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(file_id, parts); - if (this.errno != 0) { - return this.errno; + return this.append_file(parts[0], parts[1], local_filename); } - return this.download_file(parts[0], parts[1], file_offset, download_bytes, local_filename); - } + /** + * append file to storage server (by file buff) + * + * @param appender_file_id the appender file id + * @param file_buff file content/buff + * @return 0 for success, != 0 for error (error no) + */ + public int append_file1(String appender_file_id, byte[] file_buff) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } - /** - * download file from storage server - * - * @param file_id the file id(including group name and filename) - * @param callback the callback object, will call callback.recv() when data arrive - * @return 0 success, return none zero errno if fail - */ - public int download_file1(String file_id, DownloadCallback callback) throws IOException, MyException { - final long file_offset = 0; - final long download_bytes = 0; - - return this.download_file1(file_id, file_offset, download_bytes, callback); - } - - /** - * download file from storage server - * - * @param file_id the file id(including group name and filename) - * @param file_offset the start offset of the file - * @param download_bytes download bytes, 0 for remain bytes from offset - * @param callback the callback object, will call callback.recv() when data arrive - * @return 0 success, return none zero errno if fail - */ - public int download_file1(String file_id, long file_offset, long download_bytes, DownloadCallback callback) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(file_id, parts); - if (this.errno != 0) { - return this.errno; + return this.append_file(parts[0], parts[1], file_buff); } - return this.download_file(parts[0], parts[1], file_offset, download_bytes, callback); - } + /** + * append file to storage server (by file buff) + * + * @param appender_file_id the appender file id + * @param file_buff file content/buffer + * @param offset start offset of the buffer + * @param length the length of the buffer to append + * @return 0 for success, != 0 for error (error no) + */ + public int append_file1(String appender_file_id, byte[] file_buff, int offset, int length) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } - /** - * get all metadata items from storage server - * - * @param file_id the file id(including group name and filename) - * @return meta info array, return null if fail - */ - public NameValuePair[] get_metadata1(String file_id) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(file_id, parts); - if (this.errno != 0) { - return null; + return this.append_file(parts[0], parts[1], file_buff, offset, length); } - return this.get_metadata(parts[0], parts[1]); - } + /** + * append file to storage server (by callback) + * + * @param appender_file_id the appender file id + * @param file_size the file size + * @param callback the write data callback object + * @return 0 for success, != 0 for error (error no) + */ + public int append_file1(String appender_file_id, long file_size, UploadCallback callback) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } - /** - * set metadata items to storage server - * - * @param file_id the file id(including group name and filename) - * @param meta_list meta item array - * @param op_flag flag, can be one of following values:
- *
  • ProtoCommon.STORAGE_SET_METADATA_FLAG_OVERWRITE: overwrite all old - * metadata items
- *
  • ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE: merge, insert when - * the metadata item not exist, otherwise update it
- * @return 0 for success, !=0 fail (error code) - */ - public int set_metadata1(String file_id, NameValuePair[] meta_list, byte op_flag) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(file_id, parts); - if (this.errno != 0) { - return this.errno; + return this.append_file(parts[0], parts[1], file_size, callback); } - return this.set_metadata(parts[0], parts[1], meta_list, op_flag); - } + /** + * modify appender file to storage server (by file name) + * + * @param appender_file_id the appender file id + * @param file_offset the offset of appender file + * @param local_filename local filename to append + * @return 0 for success, != 0 for error (error no) + */ + public int modify_file1(String appender_file_id, + long file_offset, String local_filename) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } - /** - * get file info from storage server - * - * @param file_id the file id(including group name and filename) - * @return FileInfo object for success, return null for fail - */ - public FileInfo query_file_info1(String file_id) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(file_id, parts); - if (this.errno != 0) { - return null; + return this.modify_file(parts[0], parts[1], file_offset, local_filename); } - return this.query_file_info(parts[0], parts[1]); - } + /** + * modify appender file to storage server (by file buff) + * + * @param appender_file_id the appender file id + * @param file_offset the offset of appender file + * @param file_buff file content/buff + * @return 0 for success, != 0 for error (error no) + */ + public int modify_file1(String appender_file_id, + long file_offset, byte[] file_buff) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } - /** - * get file info decoded from filename - * - * @param file_id the file id(including group name and filename) - * @return FileInfo object for success, return null for fail - */ - public FileInfo get_file_info1(String file_id) throws IOException, MyException { - String[] parts = new String[2]; - this.errno = this.split_file_id(file_id, parts); - if (this.errno != 0) { - return null; + return this.modify_file(parts[0], parts[1], file_offset, file_buff); } - return this.get_file_info(parts[0], parts[1]); - } + /** + * modify appender file to storage server (by file buff) + * + * @param appender_file_id the appender file id + * @param file_offset the offset of appender file + * @param file_buff file content/buff + * @param buffer_offset start offset of the buff + * @param buffer_length the length of buff to modify + * @return 0 for success, != 0 for error (error no) + */ + public int modify_file1(String appender_file_id, + long file_offset, byte[] file_buff, int buffer_offset, int buffer_length) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } + + return this.modify_file(parts[0], parts[1], file_offset, + file_buff, buffer_offset, buffer_length); + } + + /** + * modify appender file to storage server (by callback) + * + * @param appender_file_id the appender file id + * @param file_offset the offset of appender file + * @param modify_size the modify size + * @param callback the write data callback object + * @return 0 for success, != 0 for error (error no) + */ + public int modify_file1(String appender_file_id, + long file_offset, long modify_size, UploadCallback callback) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } + + return this.modify_file(parts[0], parts[1], file_offset, modify_size, callback); + } + + /** + * regenerate filename for appender file + * + * @param appender_file_id the appender file id + * @return the regenerated file id, return null if fail + */ + public String regenerate_appender_filename1(String appender_file_id) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return null; + } + + String[] new_parts = this.regenerate_appender_filename(parts[0], parts[1]); + if (new_parts != null) { + return new_parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + new_parts[1]; + } else { + return null; + } + } + + /** + * delete file from storage server + * + * @param file_id the file id(including group name and filename) + * @return 0 for success, none zero for fail (error code) + */ + public int delete_file1(String file_id) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(file_id, parts); + if (this.errno != 0) { + return this.errno; + } + + return this.delete_file(parts[0], parts[1]); + } + + /** + * truncate appender file to size 0 from storage server + * + * @param appender_file_id the appender file id + * @return 0 for success, none zero for fail (error code) + */ + public int truncate_file1(String appender_file_id) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } + + return this.truncate_file(parts[0], parts[1]); + } + + /** + * truncate appender file from storage server + * + * @param appender_file_id the appender file id + * @param truncated_file_size truncated file size + * @return 0 for success, none zero for fail (error code) + */ + public int truncate_file1(String appender_file_id, long truncated_file_size) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(appender_file_id, parts); + if (this.errno != 0) { + return this.errno; + } + + return this.truncate_file(parts[0], parts[1], truncated_file_size); + } + + /** + * download file from storage server + * + * @param file_id the file id(including group name and filename) + * @return file content/buffer, return null if fail + */ + public byte[] download_file1(String file_id) throws IOException, MyException { + final long file_offset = 0; + final long download_bytes = 0; + + return this.download_file1(file_id, file_offset, download_bytes); + } + + /** + * download file from storage server + * + * @param file_id the file id(including group name and filename) + * @param file_offset the start offset of the file + * @param download_bytes download bytes, 0 for remain bytes from offset + * @return file content/buff, return null if fail + */ + public byte[] download_file1(String file_id, long file_offset, long download_bytes) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(file_id, parts); + if (this.errno != 0) { + return null; + } + + return this.download_file(parts[0], parts[1], file_offset, download_bytes); + } + + /** + * download file from storage server + * + * @param file_id the file id(including group name and filename) + * @param local_filename the filename on local + * @return 0 success, return none zero errno if fail + */ + public int download_file1(String file_id, String local_filename) throws IOException, MyException { + final long file_offset = 0; + final long download_bytes = 0; + + return this.download_file1(file_id, file_offset, download_bytes, local_filename); + } + + /** + * download file from storage server + * + * @param file_id the file id(including group name and filename) + * @param file_offset the start offset of the file + * @param download_bytes download bytes, 0 for remain bytes from offset + * @param local_filename the filename on local + * @return 0 success, return none zero errno if fail + */ + public int download_file1(String file_id, long file_offset, long download_bytes, String local_filename) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(file_id, parts); + if (this.errno != 0) { + return this.errno; + } + + return this.download_file(parts[0], parts[1], file_offset, download_bytes, local_filename); + } + + /** + * download file from storage server + * + * @param file_id the file id(including group name and filename) + * @param callback the callback object, will call callback.recv() when data arrive + * @return 0 success, return none zero errno if fail + */ + public int download_file1(String file_id, DownloadCallback callback) throws IOException, MyException { + final long file_offset = 0; + final long download_bytes = 0; + + return this.download_file1(file_id, file_offset, download_bytes, callback); + } + + /** + * download file from storage server + * + * @param file_id the file id(including group name and filename) + * @param file_offset the start offset of the file + * @param download_bytes download bytes, 0 for remain bytes from offset + * @param callback the callback object, will call callback.recv() when data arrive + * @return 0 success, return none zero errno if fail + */ + public int download_file1(String file_id, long file_offset, long download_bytes, DownloadCallback callback) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(file_id, parts); + if (this.errno != 0) { + return this.errno; + } + + return this.download_file(parts[0], parts[1], file_offset, download_bytes, callback); + } + + /** + * get all metadata items from storage server + * + * @param file_id the file id(including group name and filename) + * @return meta info array, return null if fail + */ + public NameValuePair[] get_metadata1(String file_id) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(file_id, parts); + if (this.errno != 0) { + return null; + } + + return this.get_metadata(parts[0], parts[1]); + } + + /** + * set metadata items to storage server + * + * @param file_id the file id(including group name and filename) + * @param meta_list meta item array + * @param op_flag flag, can be one of following values:
+ *
  • ProtoCommon.STORAGE_SET_METADATA_FLAG_OVERWRITE: overwrite all old + * metadata items
+ *
  • ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE: merge, insert when + * the metadata item not exist, otherwise update it
+ * @return 0 for success, !=0 fail (error code) + */ + public int set_metadata1(String file_id, NameValuePair[] meta_list, byte op_flag) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(file_id, parts); + if (this.errno != 0) { + return this.errno; + } + + return this.set_metadata(parts[0], parts[1], meta_list, op_flag); + } + + /** + * get file info from storage server + * + * @param file_id the file id(including group name and filename) + * @return FileInfo object for success, return null for fail + */ + public FileInfo query_file_info1(String file_id) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(file_id, parts); + if (this.errno != 0) { + return null; + } + + return this.query_file_info(parts[0], parts[1]); + } + + /** + * get file info decoded from filename + * + * @param file_id the file id(including group name and filename) + * @return FileInfo object for success, return null for fail + */ + public FileInfo get_file_info1(String file_id) throws IOException, MyException { + String[] parts = new String[2]; + this.errno = this.split_file_id(file_id, parts); + if (this.errno != 0) { + return null; + } + + return this.get_file_info(parts[0], parts[1]); + } } diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/TrackerClient.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/TrackerClient.java index 8fe49ec..865353d 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/TrackerClient.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/TrackerClient.java @@ -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; diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/TrackerServer.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/TrackerServer.java index ae76ab6..364935e 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/TrackerServer.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/TrackerServer.java @@ -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 * diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/UploadStream.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/UploadStream.java index d9c6041..eac5556 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/UploadStream.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/UploadStream.java @@ -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; + } } diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/Connection.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/Connection.java index 42c05d5..46652a0 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/Connection.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/Connection.java @@ -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) { diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionFactory.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionFactory.java index 8d39ccd..48b9e82 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionFactory.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionFactory.java @@ -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; diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionManager.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionManager.java index 81e2bfd..e66c887 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionManager.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionManager.java @@ -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; diff --git a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionPool.java b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionPool.java index ce68818..0bfabfa 100644 --- a/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionPool.java +++ b/module/aivfo-dfs-client-spring-boot/aivfo-fast-dfs-client/src/main/java/csource/fastdfs/pool/ConnectionPool.java @@ -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; diff --git a/module/aivfo-dfs-client-spring-boot/pom.xml b/module/aivfo-dfs-client-spring-boot/pom.xml index 12b3c91..97e7a6a 100644 --- a/module/aivfo-dfs-client-spring-boot/pom.xml +++ b/module/aivfo-dfs-client-spring-boot/pom.xml @@ -34,9 +34,9 @@ ${project.version} - org.csource - fastdfs-client-java - ${fastdfs-client-java.version} + com.aivfo + aivfo-fast-dfs-client + ${project.version}