From 75931a8053a9b19c19ec835807ad42f58f46692c Mon Sep 17 00:00:00 2001 From: veryben Date: Fri, 19 May 2017 09:56:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E6=89=80=E6=9C=89java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/csource/common/Base64.java | 846 ++-- .../org/csource/common/IniFileReader.java | 229 +- .../java/org/csource/common/MyException.java | 26 +- .../org/csource/common/NameValuePair.java | 65 +- .../org/csource/fastdfs/ClientGlobal.java | 356 +- .../org/csource/fastdfs/DownloadCallback.java | 46 +- .../org/csource/fastdfs/DownloadStream.java | 78 +- .../java/org/csource/fastdfs/FileInfo.java | 226 +- .../java/org/csource/fastdfs/ProtoCommon.java | 986 ++--- .../csource/fastdfs/ProtoStructDecoder.java | 87 +- .../java/org/csource/fastdfs/ServerInfo.java | 119 +- .../org/csource/fastdfs/StorageClient.java | 3776 ++++++++--------- .../org/csource/fastdfs/StorageClient1.java | 1487 ++++--- .../org/csource/fastdfs/StorageServer.java | 104 +- .../java/org/csource/fastdfs/StructBase.java | 135 +- .../org/csource/fastdfs/StructGroupStat.java | 403 +- .../csource/fastdfs/StructStorageStat.java | 1789 ++++---- .../org/csource/fastdfs/TrackerClient.java | 1753 ++++---- .../org/csource/fastdfs/TrackerGroup.java | 209 +- .../org/csource/fastdfs/TrackerServer.java | 154 +- .../org/csource/fastdfs/UploadCallback.java | 43 +- .../org/csource/fastdfs/UploadStream.java | 100 +- .../fastdfs/test/DownloadFileWriter.java | 113 +- .../org/csource/fastdfs/test/Monitor.java | 404 +- .../java/org/csource/fastdfs/test/Test.java | 124 +- .../java/org/csource/fastdfs/test/Test1.java | 84 +- .../csource/fastdfs/test/TestAppender.java | 608 ++- .../csource/fastdfs/test/TestAppender1.java | 568 ++- .../org/csource/fastdfs/test/TestClient.java | 603 ++- .../org/csource/fastdfs/test/TestClient1.java | 533 ++- .../org/csource/fastdfs/test/TestLoad.java | 549 ++- .../fastdfs/test/UploadLocalFileSender.java | 103 +- 32 files changed, 7826 insertions(+), 8880 deletions(-) diff --git a/src/main/java/org/csource/common/Base64.java b/src/main/java/org/csource/common/Base64.java index 3549d22..2df19a1 100644 --- a/src/main/java/org/csource/common/Base64.java +++ b/src/main/java/org/csource/common/Base64.java @@ -19,7 +19,7 @@ import java.io.IOException; * Optrak Distribution Software Ltd. * http://www.optrak.co.uk * and Kevin Kelley's http://www.ruralnet.net/~kelley/java/Base64.java - * + *

* Base64 is a way of encoding 8-bit characters using only ASCII printable * characters similar to UUENCODE. UUENCODE includes a filename where BASE64 does not. * The spec is described in RFC 2045. Base64 is a scheme where @@ -34,452 +34,129 @@ import java.io.IOException; * If you don't like this code, there is another implementation at http://www.ruffboy.com/download.htm * Sun has an undocumented method called sun.misc.Base64Encoder.encode. * You could use hex, simpler to code, but not as compact. - * + *

* If you wanted to encode a giant file, you could do it in large chunks that * are even multiples of 3 bytes, except for the last chunk, and append the outputs. - * + *

* To encode a string, rather than binary data java.net.URLEncoder may be better. See * printable characters in the Java glossary for a discussion of the differences. - * + *

* version 1.4 2002 February 15 -- correct bugs with uneven line lengths, - * allow you to configure line separator. - * now need Base64 object and instance methods. - * new mailing address. + * allow you to configure line separator. + * now need Base64 object and instance methods. + * new mailing address. * version 1.3 2000 September 12 -- fix problems with estimating output length in encode * version 1.2 2000 September 09 -- now handles decode as well. * version 1.1 1999 December 04 -- more symmetrical encoding algorithm. - * more accurate StringBuffer allocation size. + * more accurate StringBuffer allocation size. * version 1.0 1999 December 03 -- posted in comp.lang.java.programmer. * Futures Streams or files. */ -public class Base64 -{ +public class Base64 { - /** - * how we separate lines, e.g. \n, \r\n, \r etc. - */ - private String lineSeparator = System.getProperty( "line.separator" ); + /** + * Marker value for chars we just ignore, e.g. \n \r high ascii + */ + static final int IGNORE = -1; + /** + * Marker for = trailing pad + */ + static final int PAD = -2; + /** + * used to disable test driver + */ + private static final boolean debug = true; + /** + * how we separate lines, e.g. \n, \r\n, \r etc. + */ + private String lineSeparator = System.getProperty("line.separator"); + /** + * max chars per line, excluding lineSeparator. A multiple of 4. + */ + private int lineLength = 72; + private char[] valueToChar = new char[64]; + /** + * binary value encoded by a given letter of the alphabet 0..63 + */ + private int[] charToValue = new int[256]; + private int[] charToPad = new int[4]; - /** - * max chars per line, excluding lineSeparator. A multiple of 4. - */ - private int lineLength = 72; + /* constructor */ + public Base64() { + this.init('+', '/', '='); + } - private char[] valueToChar = new char[64]; + /* constructor */ + public Base64(char chPlus, char chSplash, char chPad, int lineLength) { + this.init(chPlus, chSplash, chPad); + this.lineLength = lineLength; + } - /** - * binary value encoded by a given letter of the alphabet 0..63 - */ - private int[] charToValue = new int[256]; - - private int[] charToPad = new int[4]; - - /* constructor */ - public Base64() - { - this.init('+', '/', '='); - } - - /* constructor */ - public Base64(char chPlus, char chSplash, char chPad, int lineLength) - { - this.init(chPlus, chSplash, chPad); - this.lineLength = lineLength; - } + public Base64(int lineLength) { + this.lineLength = lineLength; + } - public Base64(int lineLength) - { - this.lineLength = lineLength; - } + /** + * debug display array + */ + public static void show(byte[] b) { + int count = 0; + int rows = 0; - /* initialise defaultValueToChar and defaultCharToValue tables */ - private void init(char chPlus, char chSplash, char chPad) - { - int index = 0; - // build translate this.valueToChar table only once. - // 0..25 -> 'A'..'Z' - for ( int i='A'; i<='Z'; i++) { - this.valueToChar[index++] = (char)i; + + for (int i = 0; i < b.length; i++) { + if (count == 8) { + System.out.print(" "); + } else if (count == 16) { + System.out.println(""); + count = 0; + continue; } - - // 26..51 -> 'a'..'z' - for ( int i='a'; i<='z'; i++ ) { - this.valueToChar[index++] = (char)i; + System.out.print(Integer.toHexString(b[i] & 0xFF).toUpperCase() + " "); + count++; + + } + System.out.println(); + } + + /** + * debug display array + */ + public static void display(byte[] b) { + for (int i = 0; i < b.length; i++) { + System.out.print((char) b[i]); + } + System.out.println(); + } + + /** + * test driver + */ + public static void main(String[] args) { + test(); + System.exit(1); + + if (debug) { + try { + Base64 b64 = new Base64(); + String str = "agfrtu¿¦etʲ1234¼Ù´óerty¿Õ234·¢¿¦2344ʲµÄ"; + String str64 = ""; + + //encode + str64 = b64.encode(str.getBytes()); + System.out.println(str64); + + //decode + byte[] theBytes = b64.decode(str64); + show(theBytes); + String rst = new String(theBytes); + System.out.println(rst); + System.out.println(str); + } catch (Exception e) { + e.printStackTrace(); } - - // 52..61 -> '0'..'9' - for ( int i='0'; i<='9'; i++) { - this.valueToChar[index++] = (char)i; - } - - this.valueToChar[index++] = chPlus; - this.valueToChar[index++] = chSplash; - - // build translate defaultCharToValue table only once. - for ( int i=0; i<256; i++ ) - { - this.charToValue[i] = IGNORE; // default is to ignore - } - - for ( int i=0; i<64; i++ ) - { - this.charToValue[this.valueToChar[i]] = i; - } - - this.charToValue[chPad] = PAD; - java.util.Arrays.fill(this.charToPad, chPad); - } - - /** - * Encode an arbitrary array of bytes as Base64 printable ASCII. - * It will be broken into lines of 72 chars each. The last line is not - * terminated with a line separator. - * The output will always have an even multiple of data characters, - * exclusive of \n. It is padded out with =. - */ - public String encode(byte[] b) throws IOException - { - // Each group or partial group of 3 bytes becomes four chars - // covered quotient - int outputLength = ((b.length + 2) / 3) * 4; - - // account for trailing newlines, on all but the very last line - if ( lineLength != 0 ) - { - int lines = ( outputLength + lineLength -1 ) / lineLength - 1; - if ( lines > 0 ) - { - outputLength += lines * lineSeparator.length(); - } - } - - // must be local for recursion to work. - StringBuffer sb = new StringBuffer( outputLength ); - - // must be local for recursion to work. - int linePos = 0; - - // first deal with even multiples of 3 bytes. - int len = (b.length / 3) * 3; - int leftover = b.length - len; - for ( int i=0; i lineLength ) - { - if ( lineLength != 0 ) - { - sb.append(lineSeparator); - } - linePos = 4; - } - - // get next three bytes in unsigned form lined up, - // in big-endian order - int combined = b[i+0] & 0xff; - combined <<= 8; - combined |= b[i+1] & 0xff; - combined <<= 8; - combined |= b[i+2] & 0xff; - - // break those 24 bits into a 4 groups of 6 bits, - // working LSB to MSB. - int c3 = combined & 0x3f; - combined >>>= 6; - int c2 = combined & 0x3f; - combined >>>= 6; - int c1 = combined & 0x3f; - combined >>>= 6; - int c0 = combined & 0x3f; - - // Translate into the equivalent alpha character - // emitting them in big-endian order. - sb.append( valueToChar[c0]); - sb.append( valueToChar[c1]); - sb.append( valueToChar[c2]); - sb.append( valueToChar[c3]); - } - - // deal with leftover bytes - switch ( leftover ) - { - case 0: - default: - // nothing to do - break; - - case 1: - // One leftover byte generates xx== - // Start a new line if next 4 chars won't fit on the current line - linePos += 4; - if ( linePos > lineLength ) - { - - if ( lineLength != 0 ) - { - sb.append(lineSeparator); - } - linePos = 4; - } - - // Handle this recursively with a faked complete triple. - // Throw away last two chars and replace with == - sb.append(encode(new byte[] {b[len], 0, 0} - ).substring(0,2)); - sb.append("=="); - break; - - case 2: - // Two leftover bytes generates xxx= - // Start a new line if next 4 chars won't fit on the current line - linePos += 4; - if ( linePos > lineLength ) - { - if ( lineLength != 0 ) - { - sb.append(lineSeparator); - } - linePos = 4; - } - // Handle this recursively with a faked complete triple. - // Throw away last char and replace with = - sb.append(encode(new byte[] {b[len], b[len+1], 0} - ).substring(0,3)); - sb.append("="); - break; - - } // end switch; - - if ( outputLength != sb.length() ) - { - System.out.println("oops: minor program flaw: output length mis-estimated"); - System.out.println("estimate:" + outputLength); - System.out.println("actual:" + sb.length()); - } - return sb.toString(); - }// end encode - - /** - * decode a well-formed complete Base64 string back into an array of bytes. - * It must have an even multiple of 4 data characters (not counting \n), - * padded out with = as needed. - */ - public byte[] decodeAuto( String s) { - int nRemain = s.length() % 4; - if (nRemain == 0) { - return this.decode(s); - } else { - return this.decode(s + new String(this.charToPad, 0, 4 - nRemain)); - } - } - - /** - * decode a well-formed complete Base64 string back into an array of bytes. - * It must have an even multiple of 4 data characters (not counting \n), - * padded out with = as needed. - */ - public byte[] decode( String s) - { - - // estimate worst case size of output array, no embedded newlines. - byte[] b = new byte[(s.length() / 4) * 3]; - - // tracks where we are in a cycle of 4 input chars. - int cycle = 0; - - // where we combine 4 groups of 6 bits and take apart as 3 groups of 8. - int combined = 0; - - // how many bytes we have prepared. - int j = 0; - // will be an even multiple of 4 chars, plus some embedded \n - int len = s.length(); - int dummies = 0; - for ( int i=0; i>>= 8; - b[j+1] = (byte)combined; - combined >>>= 8; - b[j] = (byte)combined; - j += 3; - cycle = 0; - break; - } - break; - } - } // end for - if ( cycle != 0 ) - { - throw new ArrayIndexOutOfBoundsException ("Input to decode not an even multiple of 4 characters; pad with =."); - } - j -= dummies; - if ( b.length != j ) - { - byte[] b2 = new byte[j]; - System.arraycopy(b, 0, b2, 0, j); - b = b2; - } - return b; - - }// end decode - - /** - * determines how long the lines are that are generated by encode. - * Ignored by decode. - * @param length 0 means no newlines inserted. Must be a multiple of 4. - */ - public void setLineLength(int length) - { - this.lineLength = (length/4) * 4; - } - - /** - * How lines are separated. - * Ignored by decode. - * @param lineSeparator may be "" but not null. - * Usually contains only a combination of chars \n and \r. - * Could be any chars not in set A-Z a-z 0-9 + /. - */ - public void setLineSeparator(String lineSeparator) - { - this.lineSeparator = lineSeparator; - } - - /** - * Marker value for chars we just ignore, e.g. \n \r high ascii - */ - static final int IGNORE = -1; - - /** - * Marker for = trailing pad - */ - static final int PAD = -2; - - /** - * used to disable test driver - */ - private static final boolean debug = true; - - /** - * debug display array - */ - public static void show (byte[] b) - { - int count = 0; - int rows = 0; - - - for ( int i=0; i 'A'..'Z' + for (int i = 'A'; i <= 'Z'; i++) { + this.valueToChar[index++] = (char) i; + } + + // 26..51 -> 'a'..'z' + for (int i = 'a'; i <= 'z'; i++) { + this.valueToChar[index++] = (char) i; + } + + // 52..61 -> '0'..'9' + for (int i = '0'; i <= '9'; i++) { + this.valueToChar[index++] = (char) i; + } + + this.valueToChar[index++] = chPlus; + this.valueToChar[index++] = chSplash; + + // build translate defaultCharToValue table only once. + for (int i = 0; i < 256; i++) { + this.charToValue[i] = IGNORE; // default is to ignore + } + + for (int i = 0; i < 64; i++) { + this.charToValue[this.valueToChar[i]] = i; + } + + this.charToValue[chPad] = PAD; + java.util.Arrays.fill(this.charToPad, chPad); + } + + /** + * Encode an arbitrary array of bytes as Base64 printable ASCII. + * It will be broken into lines of 72 chars each. The last line is not + * terminated with a line separator. + * The output will always have an even multiple of data characters, + * exclusive of \n. It is padded out with =. + */ + public String encode(byte[] b) throws IOException { + // Each group or partial group of 3 bytes becomes four chars + // covered quotient + int outputLength = ((b.length + 2) / 3) * 4; + + // account for trailing newlines, on all but the very last line + if (lineLength != 0) { + int lines = (outputLength + lineLength - 1) / lineLength - 1; + if (lines > 0) { + outputLength += lines * lineSeparator.length(); + } + } + + // must be local for recursion to work. + StringBuffer sb = new StringBuffer(outputLength); + + // must be local for recursion to work. + int linePos = 0; + + // first deal with even multiples of 3 bytes. + int len = (b.length / 3) * 3; + int leftover = b.length - len; + for (int i = 0; i < len; i += 3) { + // Start a new line if next 4 chars won't fit on the current line + // We can't encapsulete the following code since the variable need to + // be local to this incarnation of encode. + linePos += 4; + if (linePos > lineLength) { + if (lineLength != 0) { + sb.append(lineSeparator); + } + linePos = 4; + } + + // get next three bytes in unsigned form lined up, + // in big-endian order + int combined = b[i + 0] & 0xff; + combined <<= 8; + combined |= b[i + 1] & 0xff; + combined <<= 8; + combined |= b[i + 2] & 0xff; + + // break those 24 bits into a 4 groups of 6 bits, + // working LSB to MSB. + int c3 = combined & 0x3f; + combined >>>= 6; + int c2 = combined & 0x3f; + combined >>>= 6; + int c1 = combined & 0x3f; + combined >>>= 6; + int c0 = combined & 0x3f; + + // Translate into the equivalent alpha character + // emitting them in big-endian order. + sb.append(valueToChar[c0]); + sb.append(valueToChar[c1]); + sb.append(valueToChar[c2]); + sb.append(valueToChar[c3]); + } + + // deal with leftover bytes + switch (leftover) { + case 0: + default: + // nothing to do + break; + + case 1: + // One leftover byte generates xx== + // Start a new line if next 4 chars won't fit on the current line + linePos += 4; + if (linePos > lineLength) { + + if (lineLength != 0) { + sb.append(lineSeparator); + } + linePos = 4; + } + + // Handle this recursively with a faked complete triple. + // Throw away last two chars and replace with == + sb.append(encode(new byte[]{b[len], 0, 0} + ).substring(0, 2)); + sb.append("=="); + break; + + case 2: + // Two leftover bytes generates xxx= + // Start a new line if next 4 chars won't fit on the current line + linePos += 4; + if (linePos > lineLength) { + if (lineLength != 0) { + sb.append(lineSeparator); + } + linePos = 4; + } + // Handle this recursively with a faked complete triple. + // Throw away last char and replace with = + sb.append(encode(new byte[]{b[len], b[len + 1], 0} + ).substring(0, 3)); + sb.append("="); + break; + + } // end switch; + + if (outputLength != sb.length()) { + System.out.println("oops: minor program flaw: output length mis-estimated"); + System.out.println("estimate:" + outputLength); + System.out.println("actual:" + sb.length()); + } + return sb.toString(); + }// end encode + + /** + * decode a well-formed complete Base64 string back into an array of bytes. + * It must have an even multiple of 4 data characters (not counting \n), + * padded out with = as needed. + */ + public byte[] decodeAuto(String s) { + int nRemain = s.length() % 4; + if (nRemain == 0) { + return this.decode(s); + } else { + return this.decode(s + new String(this.charToPad, 0, 4 - nRemain)); + } + } + + /** + * decode a well-formed complete Base64 string back into an array of bytes. + * It must have an even multiple of 4 data characters (not counting \n), + * padded out with = as needed. + */ + public byte[] decode(String s) { + + // estimate worst case size of output array, no embedded newlines. + byte[] b = new byte[(s.length() / 4) * 3]; + + // tracks where we are in a cycle of 4 input chars. + int cycle = 0; + + // where we combine 4 groups of 6 bits and take apart as 3 groups of 8. + int combined = 0; + + // how many bytes we have prepared. + int j = 0; + // will be an even multiple of 4 chars, plus some embedded \n + int len = s.length(); + int dummies = 0; + for (int i = 0; i < len; i++) { + + int c = s.charAt(i); + int value = (c <= 255) ? charToValue[c] : IGNORE; + // there are two magic values PAD (=) and IGNORE. + switch (value) { + case IGNORE: + // e.g. \n, just ignore it. + break; + + case PAD: + value = 0; + dummies++; + // fallthrough + default: + /* regular value character */ + switch (cycle) { + case 0: + combined = value; + cycle = 1; + break; + + case 1: + combined <<= 6; + combined |= value; + cycle = 2; + break; + + case 2: + combined <<= 6; + combined |= value; + cycle = 3; + break; + + case 3: + combined <<= 6; + combined |= value; + // we have just completed a cycle of 4 chars. + // the four 6-bit values are in combined in big-endian order + // peel them off 8 bits at a time working lsb to msb + // to get our original 3 8-bit bytes back + + b[j + 2] = (byte) combined; + combined >>>= 8; + b[j + 1] = (byte) combined; + combined >>>= 8; + b[j] = (byte) combined; + j += 3; + cycle = 0; + break; + } + break; + } + } // end for + if (cycle != 0) { + throw new ArrayIndexOutOfBoundsException("Input to decode not an even multiple of 4 characters; pad with =."); + } + j -= dummies; + if (b.length != j) { + byte[] b2 = new byte[j]; + System.arraycopy(b, 0, b2, 0, j); + b = b2; + } + return b; + + }// end decode + + /** + * determines how long the lines are that are generated by encode. + * Ignored by decode. + * + * @param length 0 means no newlines inserted. Must be a multiple of 4. + */ + public void setLineLength(int length) { + this.lineLength = (length / 4) * 4; + } + + /** + * How lines are separated. + * Ignored by decode. + * + * @param lineSeparator may be "" but not null. + * Usually contains only a combination of chars \n and \r. + * Could be any chars not in set A-Z a-z 0-9 + /. + */ + public void setLineSeparator(String lineSeparator) { + this.lineSeparator = lineSeparator; + } } // end Base64 diff --git a/src/main/java/org/csource/common/IniFileReader.java b/src/main/java/org/csource/common/IniFileReader.java index e872829..34681f8 100644 --- a/src/main/java/org/csource/common/IniFileReader.java +++ b/src/main/java/org/csource/common/IniFileReader.java @@ -1,130 +1,131 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.common; import java.io.*; -import java.util.*; -import org.csource.common.*; +import java.util.ArrayList; +import java.util.Hashtable; /** -* ini file reader / parser -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ -public class IniFileReader -{ - private Hashtable paramTable; - private String conf_filename; + * ini file reader / parser + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ +public class IniFileReader { + private Hashtable paramTable; + private String conf_filename; -/** -* @param conf_filename config filename -*/ - public IniFileReader(String conf_filename) throws FileNotFoundException, IOException - { - this.conf_filename = conf_filename; - loadFromFile(conf_filename); - } + /** + * @param conf_filename config filename + */ + public IniFileReader(String conf_filename) throws FileNotFoundException, IOException { + this.conf_filename = conf_filename; + loadFromFile(conf_filename); + } -/** -* get the config filename -* @return config filename -*/ - public String getConfFilename() - { - return this.conf_filename; - } + private static ClassLoader classLoader() { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + if (loader == null) { + loader = ClassLoader.getSystemClassLoader(); + } + return loader; + } -/** -* get string value from config file -* @param name item name in config file -* @return string value -*/ - public String getStrValue(String name) - { - Object obj; - obj = this.paramTable.get(name); - if (obj == null) - { - return null; - } + /** + * get the config filename + * + * @return config filename + */ + public String getConfFilename() { + return this.conf_filename; + } - if (obj instanceof String) - { - return (String)obj; - } + /** + * get string value from config file + * + * @param name item name in config file + * @return string value + */ + public String getStrValue(String name) { + Object obj; + obj = this.paramTable.get(name); + if (obj == null) { + return null; + } - return (String)((ArrayList)obj).get(0); - } + if (obj instanceof String) { + return (String) obj; + } -/** -* get int value from config file -* @param name item name in config file -* @param default_value the default value -* @return int value -*/ - public int getIntValue(String name, int default_value) - { - String szValue = this.getStrValue(name); - if (szValue == null) - { - return default_value; - } + return (String) ((ArrayList) obj).get(0); + } - return Integer.parseInt(szValue); - } + /** + * get int value from config file + * + * @param name item name in config file + * @param default_value the default value + * @return int value + */ + public int getIntValue(String name, int default_value) { + String szValue = this.getStrValue(name); + if (szValue == null) { + return default_value; + } -/** -* get boolean value from config file -* @param name item name in config file -* @param default_value the default value -* @return boolean value -*/ - public boolean getBoolValue(String name, boolean default_value) - { - String szValue = this.getStrValue(name); - if (szValue == null) - { - return default_value; - } + return Integer.parseInt(szValue); + } - return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") || - szValue.equalsIgnoreCase("true") || szValue.equals("1"); - } + /** + * get boolean value from config file + * + * @param name item name in config file + * @param default_value the default value + * @return boolean value + */ + public boolean getBoolValue(String name, boolean default_value) { + String szValue = this.getStrValue(name); + if (szValue == null) { + return default_value; + } -/** -* get all values from config file -* @param name item name in config file -* @return string values (array) -*/ - public String[] getValues(String name) - { - Object obj; - String[] values; + return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") || + szValue.equalsIgnoreCase("true") || szValue.equals("1"); + } - obj = this.paramTable.get(name); - if (obj == null) - { - return null; - } + /** + * get all values from config file + * + * @param name item name in config file + * @return string values (array) + */ + public String[] getValues(String name) { + Object obj; + String[] values; - if (obj instanceof String) - { - values = new String[1]; - values[0] = (String)obj; - return values; - } + obj = this.paramTable.get(name); + if (obj == null) { + return null; + } - Object[] objs = ((ArrayList)obj).toArray(); - values = new String[objs.length]; - System.arraycopy(objs, 0, values, 0, objs.length); - return values; - } + if (obj instanceof String) { + values = new String[1]; + values[0] = (String) obj; + return values; + } + + Object[] objs = ((ArrayList) obj).toArray(); + values = new String[objs.length]; + System.arraycopy(objs, 0, values, 0, objs.length); + return values; + } private void loadFromFile(String confFilePath) throws IOException { InputStream in = null; @@ -144,7 +145,7 @@ public class IniFileReader ex.printStackTrace(); } finally { try { - if(in != null) in.close(); + if (in != null) in.close(); //System.out.println("loadFrom...finally...in.close(); done"); } catch (Exception ex) { ex.printStackTrace(); @@ -193,8 +194,8 @@ public class IniFileReader ex.printStackTrace(); } finally { try { - if(bufferedReader != null) bufferedReader.close(); - if(inReader != null) inReader.close(); + if (bufferedReader != null) bufferedReader.close(); + if (inReader != null) inReader.close(); //System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done"); } catch (Exception ex) { ex.printStackTrace(); @@ -202,12 +203,4 @@ public class IniFileReader } } - private static ClassLoader classLoader() { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - if (loader == null) { - loader = ClassLoader.getSystemClassLoader(); - } - return loader; - } - } diff --git a/src/main/java/org/csource/common/MyException.java b/src/main/java/org/csource/common/MyException.java index 346ff3c..a4bd8d3 100644 --- a/src/main/java/org/csource/common/MyException.java +++ b/src/main/java/org/csource/common/MyException.java @@ -9,18 +9,16 @@ package org.csource.common; /** -* My Exception -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ -public class MyException extends Exception -{ - public MyException() - { - } - - public MyException(String message) - { - super(message); - } + * My Exception + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ +public class MyException extends Exception { + public MyException() { + } + + public MyException(String message) { + super(message); + } } diff --git a/src/main/java/org/csource/common/NameValuePair.java b/src/main/java/org/csource/common/NameValuePair.java index 1419874..0b9d2cb 100644 --- a/src/main/java/org/csource/common/NameValuePair.java +++ b/src/main/java/org/csource/common/NameValuePair.java @@ -9,47 +9,40 @@ package org.csource.common; /** -* name(key) and value pair model -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ -public class NameValuePair -{ - protected String name; - protected String value; + * name(key) and value pair model + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ +public class NameValuePair { + protected String name; + protected String value; - public NameValuePair() - { - } + public NameValuePair() { + } - public NameValuePair(String name) - { - this.name = name; - } + public NameValuePair(String name) { + this.name = name; + } - public NameValuePair(String name, String value) - { - this.name = name; - this.value = value; - } + public NameValuePair(String name, String value) { + this.name = name; + this.value = value; + } - public String getName() - { - return this.name; - } + public String getName() { + return this.name; + } - public String getValue() - { - return this.value; - } + public void setName(String name) { + this.name = name; + } - public void setName(String name) - { - this.name = name; - } + public String getValue() { + return this.value; + } - public void setValue(String value) - { - this.value = value; - } + public void setValue(String value) { + this.value = value; + } } diff --git a/src/main/java/org/csource/fastdfs/ClientGlobal.java b/src/main/java/org/csource/fastdfs/ClientGlobal.java index 63fce64..9e4a8e9 100644 --- a/src/main/java/org/csource/fastdfs/ClientGlobal.java +++ b/src/main/java/org/csource/fastdfs/ClientGlobal.java @@ -1,198 +1,176 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs; -import java.net.*; -import java.io.*; -import java.net.*; -import org.csource.common.*; +import org.csource.common.IniFileReader; +import org.csource.common.MyException; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; /** -* Global variables -* @author Happy Fish / YuQing -* @version Version 1.11 -*/ -public class ClientGlobal -{ - public static int g_connect_timeout; //millisecond - public static int g_network_timeout; //millisecond - public static String g_charset; - public static int g_tracker_http_port; - public static boolean g_anti_steal_token; //if anti-steal token - public static String g_secret_key; //generage token secret key - public static TrackerGroup g_tracker_group; - - public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second - public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second - - private ClientGlobal() - { - } - -/** -* load global variables -* @param conf_filename config filename -*/ - public static void init(String conf_filename) throws FileNotFoundException, IOException, MyException - { - IniFileReader iniReader; - String[] szTrackerServers; - String[] parts; - - iniReader = new IniFileReader(conf_filename); + * Global variables + * + * @author Happy Fish / YuQing + * @version Version 1.11 + */ +public class ClientGlobal { + public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second + public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second + public static int g_connect_timeout; //millisecond + public static int g_network_timeout; //millisecond + public static String g_charset; + public static int g_tracker_http_port; + public static boolean g_anti_steal_token; //if anti-steal token + public static String g_secret_key; //generage token secret key + public static TrackerGroup g_tracker_group; - 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 + private ClientGlobal() { + } - 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 + * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.common.*; - /** -* Download file callback interface -* @author Happy Fish / YuQing -* @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); + * Download file callback interface + * + * @author Happy Fish / YuQing + * @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); } diff --git a/src/main/java/org/csource/fastdfs/DownloadStream.java b/src/main/java/org/csource/fastdfs/DownloadStream.java index d1eb8ce..a848948 100644 --- a/src/main/java/org/csource/fastdfs/DownloadStream.java +++ b/src/main/java/org/csource/fastdfs/DownloadStream.java @@ -2,49 +2,43 @@ package org.csource.fastdfs; import java.io.IOException; import java.io.OutputStream; -import org.csource.fastdfs.DownloadCallback; /** -* Download file by stream (download callback class) -* @author zhouzezhong & Happy Fish / YuQing -* @version Version 1.11 -*/ -public class DownloadStream implements DownloadCallback -{ - private OutputStream out; - private long currentBytes = 0; - - public DownloadStream(OutputStream out) - { - super(); - this.out = out; - } + * Download file by stream (download callback class) + * + * @author zhouzezhong & Happy Fish / YuQing + * @version Version 1.11 + */ +public class DownloadStream implements DownloadCallback { + private OutputStream out; + private long currentBytes = 0; - /** - * recv file content callback function, may be called more than once when the file downloaded - * @param fileSize file size - * @param data data buff - * @param bytes data bytes - * @return 0 success, return none zero(errno) if fail - */ - public int recv(long fileSize, byte[] data, int bytes) - { - try - { - out.write(data, 0, bytes); - } - catch(IOException ex) - { - ex.printStackTrace(); - return -1; - } - - currentBytes += bytes; - if (this.currentBytes == fileSize) - { - this.currentBytes = 0; - } - - return 0; - } + public DownloadStream(OutputStream out) { + super(); + this.out = out; + } + + /** + * recv file content callback function, may be called more than once when the file downloaded + * + * @param fileSize file size + * @param data data buff + * @param bytes data bytes + * @return 0 success, return none zero(errno) if fail + */ + public int recv(long fileSize, byte[] data, int bytes) { + try { + out.write(data, 0, bytes); + } catch (IOException ex) { + ex.printStackTrace(); + return -1; + } + + currentBytes += bytes; + if (this.currentBytes == fileSize) { + this.currentBytes = 0; + } + + return 0; + } } diff --git a/src/main/java/org/csource/fastdfs/FileInfo.java b/src/main/java/org/csource/fastdfs/FileInfo.java index b1280e9..431b872 100644 --- a/src/main/java/org/csource/fastdfs/FileInfo.java +++ b/src/main/java/org/csource/fastdfs/FileInfo.java @@ -1,125 +1,125 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.util.Date; import java.text.SimpleDateFormat; +import java.util.Date; /** -* Server Info -* @author Happy Fish / YuQing -* @version Version 1.23 -*/ -public class FileInfo -{ - protected String source_ip_addr; - protected long file_size; - protected Date create_timestamp; - protected int crc32; + * Server Info + * + * @author Happy Fish / YuQing + * @version Version 1.23 + */ +public class FileInfo { + protected String source_ip_addr; + protected long file_size; + protected Date create_timestamp; + protected int crc32; -/** -* Constructor -* @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(long file_size, int create_timestamp, int crc32, String source_ip_addr) - { - 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 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(long file_size, int create_timestamp, int crc32, String source_ip_addr) { + this.file_size = file_size; + this.create_timestamp = new Date(create_timestamp * 1000L); + this.crc32 = crc32; + 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 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 file size -* @param file_size the file size -*/ - public void setFileSize(long file_size) - { - this.file_size = file_size; - } - -/** -* get the file size -* @return the file size -*/ - public long getFileSize() - { - return this.file_size; - } + /** + * 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 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 create timestamp of the file -* @return the create timestamp of the file -*/ - public Date getCreateTimestamp() - { - return this.create_timestamp; - } + /** + * 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 create timestamp of the file -* @param crc32 the crc32 signature -*/ - public void setCrc32(int crc32) - { - this.crc32 = crc32; - } - -/** -* get the file CRC32 signature -* @return the file CRC32 signature -*/ - public long getCrc32() - { - return this.crc32; - } - -/** -* to string -* @return string -*/ - public String toString() - { - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - return "source_ip_addr = " + this.source_ip_addr + ", " + - "file_size = " + this.file_size + ", " + - "create_timestamp = " + df.format(this.create_timestamp) + ", " + - "crc32 = " + this.crc32; - } + /** + * 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; + } + + /** + * 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); + } + + /** + * 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; + } + + /** + * to string + * + * @return string + */ + public String toString() { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return "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/src/main/java/org/csource/fastdfs/ProtoCommon.java b/src/main/java/org/csource/fastdfs/ProtoCommon.java index abfe860..f1aecbb 100644 --- a/src/main/java/org/csource/fastdfs/ProtoCommon.java +++ b/src/main/java/org/csource/fastdfs/ProtoCommon.java @@ -1,553 +1,503 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs; -import java.io.InputStream; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; -import java.net.Socket; import org.csource.common.MyException; import org.csource.common.NameValuePair; -/** -* protocol common functions -* @author Happy Fish / YuQing -* @version Version 1.18 -*/ -public class ProtoCommon -{ - /** - * 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 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; - } - } - - 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_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; - - 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 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; - - 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"; - } -} +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.net.Socket; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; /** -* 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; - } + * protocol common functions + * + * @author Happy Fish / YuQing + * @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_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; -/** -* 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); - } + private ProtoCommon() { + } -/** -* 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); - } + 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"; + } + } -/** -* 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); - } + /** + * 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; -/** -* 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> 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])); - } + /** + * 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; -/** -* 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 ""; - } - - int n; - StringBuilder sbResult = new StringBuilder(16); - for (int i=offset; i= 0) ? bs[i] : 256 + bs[i]; - if (sbResult.length() > 0) - { - sbResult.append("."); - } - sbResult.append(String.valueOf(n)); - } - - return sbResult.toString(); - } - - /** -* 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'}; + 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); + } + + /** + * 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); + } + + /** + * 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]); + } + } + + return meta_list; + } + + /** + * 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(); + } + + /** + * 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 ""; + } + + 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(); + } + + /** + * 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]; + for (int i = 0; i < 16; i++) { + str[k++] = hexDigits[tmp[i] >>> 4 & 0xf]; + str[k++] = hexDigits[tmp[i] & 0xf]; } - - return new String(str); - } - - /** -* 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 - { + + return new String(str); + } + + /** + * 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"); + 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 (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 (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; + if (dotIndex < 0) { + return master_filename + prefix_name + true_ext_name; + } else { + return master_filename.substring(0, dotIndex) + 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 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/src/main/java/org/csource/fastdfs/ProtoStructDecoder.java b/src/main/java/org/csource/fastdfs/ProtoStructDecoder.java index c30cb6a..5f27907 100644 --- a/src/main/java/org/csource/fastdfs/ProtoStructDecoder.java +++ b/src/main/java/org/csource/fastdfs/ProtoStructDecoder.java @@ -1,55 +1,48 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.*; -import java.util.*; -import java.net.*; +import java.io.IOException; import java.lang.reflect.Array; -import org.csource.common.*; /** -* C struct body decoder -* @author Happy Fish / YuQing -* @version Version 1.17 -*/ -public class ProtoStructDecoder -{ -/** -* Constructor -*/ - public ProtoStructDecoder() - { - } - -/** -* decode byte buffer -*/ - public T[] decode(byte[] bs, Class clazz, int fieldsTotalSize) throws Exception - { - if (bs.length % fieldsTotalSize != 0) - { - throw new IOException("byte array length: " + bs.length + " is invalid!"); - } - - int count = bs.length / fieldsTotalSize; - int offset; - T[] results = (T[])Array.newInstance(clazz, count); - - offset = 0; - for (int i=0; i { + /** + * Constructor + */ + public ProtoStructDecoder() { + } + + /** + * decode byte buffer + */ + public T[] decode(byte[] bs, Class clazz, int fieldsTotalSize) throws Exception { + if (bs.length % fieldsTotalSize != 0) { + throw new IOException("byte array length: " + bs.length + " is invalid!"); + } + + int count = bs.length / fieldsTotalSize; + int offset; + T[] results = (T[]) Array.newInstance(clazz, count); + + offset = 0; + for (int i = 0; i < results.length; i++) { + results[i] = clazz.newInstance(); + results[i].setFields(bs, offset); + offset += fieldsTotalSize; + } + + return results; + } } diff --git a/src/main/java/org/csource/fastdfs/ServerInfo.java b/src/main/java/org/csource/fastdfs/ServerInfo.java index 152a9ca..98faefd 100644 --- a/src/main/java/org/csource/fastdfs/ServerInfo.java +++ b/src/main/java/org/csource/fastdfs/ServerInfo.java @@ -1,67 +1,66 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.common.*; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; /** -* Server Info -* @author Happy Fish / YuQing -* @version Version 1.7 -*/ -public class ServerInfo -{ - protected String ip_addr; - protected int port; - -/** -* Constructor -* @param ip_addr address of the server -* @param port the port of the server -*/ - public ServerInfo(String ip_addr, int port) - { - this.ip_addr = ip_addr; - this.port = port; - } - -/** -* return the ip address -* @return the ip address -*/ - public String getIpAddr() - { - return this.ip_addr; - } - -/** -* return the port of the server -* @return the port of the server -*/ - public int getPort() - { - return this.port; - } - -/** -* connect to server -* @return connected Socket object -*/ - public Socket connect() throws IOException - { - Socket sock = new Socket(); - sock.setReuseAddress(true); - sock.setSoTimeout(ClientGlobal.g_network_timeout); - sock.connect(new InetSocketAddress(this.ip_addr, this.port), ClientGlobal.g_connect_timeout); - return sock; - } + * Server Info + * + * @author Happy Fish / YuQing + * @version Version 1.7 + */ +public class ServerInfo { + protected String ip_addr; + protected int port; + + /** + * Constructor + * + * @param ip_addr address of the server + * @param port the port of the server + */ + public ServerInfo(String ip_addr, int port) { + this.ip_addr = ip_addr; + this.port = port; + } + + /** + * return the ip address + * + * @return the ip address + */ + public String getIpAddr() { + return this.ip_addr; + } + + /** + * return the port of the server + * + * @return the port of the server + */ + public int getPort() { + return this.port; + } + + /** + * connect to server + * + * @return connected Socket object + */ + public Socket connect() throws IOException { + Socket sock = new Socket(); + sock.setReuseAddress(true); + sock.setSoTimeout(ClientGlobal.g_network_timeout); + sock.connect(new InetSocketAddress(this.ip_addr, this.port), ClientGlobal.g_connect_timeout); + return sock; + } } diff --git a/src/main/java/org/csource/fastdfs/StorageClient.java b/src/main/java/org/csource/fastdfs/StorageClient.java index 209800f..dec5b1b 100644 --- a/src/main/java/org/csource/fastdfs/StorageClient.java +++ b/src/main/java/org/csource/fastdfs/StorageClient.java @@ -1,2076 +1,1786 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.IOException; -import java.io.OutputStream; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.FileInputStream; -import java.io.File; -import java.util.Arrays; -import java.net.Socket; +import org.csource.common.Base64; import org.csource.common.MyException; import org.csource.common.NameValuePair; -import org.csource.common.Base64; + +import java.io.*; +import java.net.Socket; +import java.util.Arrays; /** -* Storage client for 2 fields file id: group name and filename -* @author Happy Fish / YuQing -* @version Version 1.24 -*/ -public class StorageClient -{ - /** - * Upload file by file buff - * @author Happy Fish / YuQing - * @version Version 1.12 - */ - public static class UploadBuff implements UploadCallback - { - private byte[] fileBuff; - private int offset; - private int length; - - /** - * constructor - * @param fileBuff the file buff for uploading - */ - public UploadBuff(byte[] fileBuff, int offset, int length) - { - super(); - this.fileBuff = fileBuff; - this.offset = offset; - this.length = length; - } - - /** - * 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 - { - out.write(this.fileBuff, this.offset, this.length); - - return 0; - } - } - - public final static Base64 base64 = new Base64('-', '_', '.', 0); - protected TrackerServer trackerServer; - protected StorageServer storageServer; - protected byte errno; - -/** -* constructor using global settings in class ClientGlobal -*/ - public StorageClient() - { - this.trackerServer = null; - this.storageServer = null; - } - -/** -* constructor with tracker server and storage server -* @param trackerServer the tracker server, can be null -* @param storageServer the storage server, can be null -*/ - public StorageClient(TrackerServer trackerServer, StorageServer storageServer) - { - this.trackerServer = trackerServer; - this.storageServer = storageServer; - } + * Storage client for 2 fields file id: group name and filename + * + * @author Happy Fish / YuQing + * @version Version 1.24 + */ +public class StorageClient { + public final static Base64 base64 = new Base64('-', '_', '.', 0); + protected TrackerServer trackerServer; + protected StorageServer storageServer; + protected byte errno; -/** -* get the error code of last call -* @return the error code of last call -*/ - public byte getErrorCode() - { - return this.errno; - } + /** + * constructor using global settings in class ClientGlobal + */ + public StorageClient() { + this.trackerServer = null; + this.storageServer = 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 2 elements string array if success:
- *

- * - * return null if fail - */ - public String[] upload_file(String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - final String group_name = null; - return this.upload_file(group_name, local_filename, file_ext_name, meta_list); - } - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - protected String[] upload_file(String group_name, String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - final byte cmd = ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE; - return this.upload_file(cmd, group_name, local_filename, file_ext_name, meta_list); - } + /** + * constructor with tracker server and storage server + * + * @param trackerServer the tracker server, can be null + * @param storageServer the storage server, can be null + */ + public StorageClient(TrackerServer trackerServer, StorageServer storageServer) { + this.trackerServer = trackerServer; + this.storageServer = storageServer; + } - /** - * upload file to storage server (by file name) - * @param cmd the command - * @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 2 elements string array if success:
- * - * - * return null if fail - */ - protected String[] upload_file(byte cmd, String group_name, String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - File f = new File(local_filename); - FileInputStream fis = new FileInputStream(f); - - if (file_ext_name == null) - { - int nPos = local_filename.lastIndexOf('.'); - if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) - { - file_ext_name = local_filename.substring(nPos+1); - } - } - - try - { - return this.do_upload_file(cmd, group_name, null, null, file_ext_name, - f.length(), new UploadStream(fis, f.length()), meta_list); - } - finally - { - fis.close(); - } - } + /** + * get the error code of last call + * + * @return the error code of last call + */ + public byte getErrorCode() { + return this.errno; + } - /** - * upload file to storage server (by file buff) - * @param file_buff file content/buff - * @param offset start offset of the buff - * @param length the length of buff to upload - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(byte[] file_buff, int offset, int length, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - final String group_name = null; - return this.upload_file(group_name, file_buff, offset, length, file_ext_name, meta_list); - } - - /** - * 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 offset start offset of the buff - * @param length the length of buff to upload - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(String group_name, byte[] file_buff, int offset, int length, - String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE, group_name, null, null, file_ext_name, - length, new UploadBuff(file_buff, offset, length), meta_list); - } - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(byte[] file_buff, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - final String group_name = null; - return this.upload_file(group_name, file_buff, 0, file_buff.length, file_ext_name, meta_list); - } - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(String group_name, byte[] file_buff, - String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE, group_name, null, null, file_ext_name, - file_buff.length, new UploadBuff(file_buff, 0, file_buff.length), meta_list); - } - - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(String group_name, long file_size, UploadCallback callback, - String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - final String master_filename = null; - final String prefix_name = null; - - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE, group_name, master_filename, prefix_name, - file_ext_name, file_size, callback, meta_list); - } - - /** - * upload file to storage server (by file name, slave file mode) - * @param group_name the group name of master file - * @param master_filename the master file name 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(String group_name, String master_filename, String prefix_name, - String local_filename, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - if ((group_name == null || group_name.length() == 0) || - (master_filename == null || master_filename.length() == 0) || - (prefix_name == null)) - { - throw new MyException("invalid arguement"); - } - - File f = new File(local_filename); - FileInputStream fis = new FileInputStream(f); - - if (file_ext_name == null) - { - int nPos = local_filename.lastIndexOf('.'); - if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) - { - file_ext_name = local_filename.substring(nPos+1); - } - } - - try - { - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name, - file_ext_name, f.length(), new UploadStream(fis, f.length()), meta_list); + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + final String group_name = null; + return this.upload_file(group_name, local_filename, file_ext_name, meta_list); + } + + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + protected String[] upload_file(String group_name, String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + final byte cmd = ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE; + return this.upload_file(cmd, group_name, local_filename, file_ext_name, meta_list); + } + + /** + * upload file to storage server (by file name) + * + * @param cmd the command + * @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 2 elements string array if success:
+ * + * + * return null if fail + */ + protected String[] upload_file(byte cmd, String group_name, String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + File f = new File(local_filename); + FileInputStream fis = new FileInputStream(f); + + if (file_ext_name == null) { + int nPos = local_filename.lastIndexOf('.'); + if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) { + file_ext_name = local_filename.substring(nPos + 1); + } } - finally - { - fis.close(); + + try { + return this.do_upload_file(cmd, group_name, null, null, file_ext_name, + f.length(), new UploadStream(fis, f.length()), meta_list); + } finally { + fis.close(); } - } - - /** - * upload file to storage server (by file buff, slave file mode) - * @param group_name the group name of master file - * @param master_filename the master file name 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(String group_name, String master_filename, String prefix_name, - byte[] file_buff, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - if ((group_name == null || group_name.length() == 0) || - (master_filename == null || master_filename.length() == 0) || - (prefix_name == null)) - { - throw new MyException("invalid arguement"); - } + } - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name, - file_ext_name, file_buff.length, new UploadBuff(file_buff, 0, file_buff.length), meta_list); - } - - /** - * upload file to storage server (by file buff, slave file mode) - * @param group_name the group name of master file - * @param master_filename the master file name to generate the slave file - * @param prefix_name the prefix name to generate the slave file - * @param file_buff file content/buff - * @param offset start offset of the buff - * @param length the length of buff to upload - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(String group_name, String master_filename, String prefix_name, - byte[] file_buff, int offset, int length, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - if ((group_name == null || group_name.length() == 0) || - (master_filename == null || master_filename.length() == 0) || - (prefix_name == null)) - { - throw new MyException("invalid arguement"); - } + /** + * upload file to storage server (by file buff) + * + * @param file_buff file content/buff + * @param offset start offset of the buff + * @param length the length of buff to upload + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(byte[] file_buff, int offset, int length, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + final String group_name = null; + return this.upload_file(group_name, file_buff, offset, length, file_ext_name, meta_list); + } - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name, - file_ext_name, length, new UploadBuff(file_buff, offset, length), meta_list); - } + /** + * 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 offset start offset of the buff + * @param length the length of buff to upload + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(String group_name, byte[] file_buff, int offset, int length, + String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE, group_name, null, null, file_ext_name, + length, new UploadBuff(file_buff, offset, length), meta_list); + } - /** - * upload file to storage server (by callback, slave file mode) - * @param group_name the group name to upload file to, can be empty - * @param master_filename the master file name 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_file(String group_name, String master_filename, - String prefix_name, long file_size, UploadCallback callback, - String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name, - file_ext_name, file_size, callback, meta_list); - } - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_appender_file(String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - final String group_name = null; - return this.upload_appender_file(group_name, local_filename, file_ext_name, meta_list); - } - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - protected String[] upload_appender_file(String group_name, String local_filename, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - final byte cmd = ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE; - return this.upload_file(cmd, group_name, local_filename, file_ext_name, meta_list); - } + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(byte[] file_buff, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + final String group_name = null; + return this.upload_file(group_name, file_buff, 0, file_buff.length, file_ext_name, meta_list); + } - /** - * upload appender file to storage server (by file buff) - * @param file_buff file content/buff - * @param offset start offset of the buff - * @param length the length of buff to upload - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_appender_file(byte[] file_buff, int offset, int length, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - final String group_name = null; - return this.upload_appender_file(group_name, file_buff, offset, length, file_ext_name, meta_list); - } - - /** - * 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 offset start offset of the buff - * @param length the length of buff to upload - * @param file_ext_name file ext name, do not include dot(.) - * @param meta_list meta info array - * @return 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_appender_file(String group_name, byte[] file_buff, int offset, int length, - String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, group_name, null, null, file_ext_name, - length, new UploadBuff(file_buff, offset, length), meta_list); - } - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_appender_file(byte[] file_buff, String file_ext_name, - NameValuePair[] meta_list) throws IOException, MyException - { - final String group_name = null; - return this.upload_appender_file(group_name, file_buff, 0, file_buff.length, file_ext_name, meta_list); - } - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_appender_file(String group_name, byte[] file_buff, - String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, group_name, null, null, file_ext_name, - file_buff.length, new UploadBuff(file_buff, 0, file_buff.length), meta_list); - } - - /** - * 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 2 elements string array if success:
- * - * - * return null if fail - */ - public String[] upload_appender_file(String group_name, long file_size, UploadCallback callback, - String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException - { - final String master_filename = null; - final String prefix_name = null; - - return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, group_name, master_filename, prefix_name, - file_ext_name, file_size, callback, meta_list); - } + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(String group_name, byte[] file_buff, + String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE, group_name, null, null, file_ext_name, + file_buff.length, new UploadBuff(file_buff, 0, file_buff.length), meta_list); + } - /** - * append file to storage server (by file name) - * @param group_name the group name of appender file - * @param appender_filename the appender filename - * @param local_filename local filename to append - * @return 0 for success, != 0 for error (error no) - */ - public int append_file(String group_name, String appender_filename, String local_filename) throws IOException, MyException - { - File f = new File(local_filename); - FileInputStream fis = new FileInputStream(f); - - try - { - return this.do_append_file(group_name, appender_filename, f.length(), new UploadStream(fis, f.length())); - } - finally - { - fis.close(); - } - } - - /** - * append file to storage server (by file buff) - * @param group_name the group name of appender file - * @param appender_filename the appender filename - * @param file_buff file content/buff - * @return 0 for success, != 0 for error (error no) - */ - public int append_file(String group_name, String appender_filename, byte[] file_buff) throws IOException, MyException - { - return this.do_append_file(group_name, appender_filename, file_buff.length, new UploadBuff(file_buff, 0, file_buff.length)); - } - - /** - * append file to storage server (by file buff) - * @param group_name the group name of appender file - * @param appender_filename the appender filename - * @param file_buff file content/buff - * @param offset start offset of the buff - * @param length the length of buff to append - * @return 0 for success, != 0 for error (error no) - */ - public int append_file(String group_name, String appender_filename, - byte[] file_buff, int offset, int length) throws IOException, MyException - { - return this.do_append_file(group_name, appender_filename, length, new UploadBuff(file_buff, offset, length)); - } + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(String group_name, long file_size, UploadCallback callback, + String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + final String master_filename = null; + final String prefix_name = null; - /** - * append file to storage server (by callback) - * @param group_name the group name to append file to - * @param appender_filename the appender filename - * @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_file(String group_name, String appender_filename, - long file_size, UploadCallback callback) throws IOException, MyException - { - return this.do_append_file(group_name, appender_filename, file_size, callback); - } + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_FILE, group_name, master_filename, prefix_name, + file_ext_name, file_size, callback, meta_list); + } - /** - * modify appender file to storage server (by file name) - * @param group_name the group name of appender file - * @param appender_filename the appender filename - * @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_file(String group_name, String appender_filename, - long file_offset, String local_filename) throws IOException, MyException - { - File f = new File(local_filename); - FileInputStream fis = new FileInputStream(f); - - try - { - return this.do_modify_file(group_name, appender_filename, file_offset, - f.length(), new UploadStream(fis, f.length())); - } - finally - { - fis.close(); - } - } - - /** - * modify appender file to storage server (by file buff) - * @param group_name the group name of appender file - * @param appender_filename the appender filename - * @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_file(String group_name, String appender_filename, - long file_offset, byte[] file_buff) throws IOException, MyException - { - return this.do_modify_file(group_name, appender_filename, file_offset, - file_buff.length, new UploadBuff(file_buff, 0, file_buff.length)); - } - - /** - * modify appender file to storage server (by file buff) - * @param group_name the group name of appender file - * @param appender_filename the appender filename - * @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_file(String group_name, String appender_filename, - long file_offset, byte[] file_buff, int buffer_offset, int buffer_length) throws IOException, MyException - { - return this.do_modify_file(group_name, appender_filename, file_offset, - buffer_length, new UploadBuff(file_buff, buffer_offset, buffer_length)); - } + /** + * upload file to storage server (by file name, slave file mode) + * + * @param group_name the group name of master file + * @param master_filename the master file name 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(String group_name, String master_filename, String prefix_name, + String local_filename, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + if ((group_name == null || group_name.length() == 0) || + (master_filename == null || master_filename.length() == 0) || + (prefix_name == null)) { + throw new MyException("invalid arguement"); + } - /** - * modify appender file to storage server (by callback) - * @param group_name the group name to modify file to - * @param appender_filename the appender filename - * @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_file(String group_name, String appender_filename, - long file_offset, long modify_size, UploadCallback callback) throws IOException, MyException - { - return this.do_modify_file(group_name, appender_filename, file_offset, - modify_size, callback); - } - - /** - * upload file to storage server - * @param cmd the command code - * @param group_name the group name to upload file to, can be empty - * @param master_filename the master file name to generate the slave file - * @param prefix_name the prefix name to generate the slave file - * @param file_ext_name file ext name, do not include dot(.) - * @param file_size the file size - * @param callback the write data callback object - * @param meta_list meta info array - * @return 2 elements string array if success:
- * - * - * return null if fail - */ - protected 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 - { - byte[] header; - byte[] ext_name_bs; - String new_group_name; - String remote_filename; - boolean bNewConnection; - Socket storageSocket; - byte[] sizeBytes; - byte[] hexLenBytes; - byte[] masterFilenameBytes; - boolean bUploadSlave; - int offset; - long body_len; + File f = new File(local_filename); + FileInputStream fis = new FileInputStream(f); - bUploadSlave = ((group_name != null && group_name.length() > 0) && - (master_filename != null && master_filename.length() > 0) && - (prefix_name != null)); - if (bUploadSlave) - { - bNewConnection = this.newUpdatableStorageConnection(group_name, master_filename); - } - else - { - bNewConnection = this.newWritableStorageConnection(group_name); - } - - try - { - storageSocket = this.storageServer.getSocket(); - - ext_name_bs = new byte[ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN]; - Arrays.fill(ext_name_bs, (byte)0); - if (file_ext_name != null && file_ext_name.length() > 0) - { - byte[] bs = file_ext_name.getBytes(ClientGlobal.g_charset); - int ext_name_len = bs.length; - if (ext_name_len > ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN) - { - ext_name_len = ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN; - } - System.arraycopy(bs, 0, ext_name_bs, 0, ext_name_len); - } - - if (bUploadSlave) - { - masterFilenameBytes = master_filename.getBytes(ClientGlobal.g_charset); - - sizeBytes = new byte[2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE]; - body_len = sizeBytes.length + ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN - + masterFilenameBytes.length + file_size; - - hexLenBytes = ProtoCommon.long2buff(master_filename.length()); - System.arraycopy(hexLenBytes, 0, sizeBytes, 0, hexLenBytes.length); - offset = hexLenBytes.length; - } - else - { - masterFilenameBytes = null; - sizeBytes = new byte[1 + 1 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE]; + if (file_ext_name == null) { + int nPos = local_filename.lastIndexOf('.'); + if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) { + file_ext_name = local_filename.substring(nPos + 1); + } + } + + try { + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name, + file_ext_name, f.length(), new UploadStream(fis, f.length()), meta_list); + } finally { + fis.close(); + } + } + + /** + * upload file to storage server (by file buff, slave file mode) + * + * @param group_name the group name of master file + * @param master_filename the master file name 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(String group_name, String master_filename, String prefix_name, + byte[] file_buff, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + if ((group_name == null || group_name.length() == 0) || + (master_filename == null || master_filename.length() == 0) || + (prefix_name == null)) { + throw new MyException("invalid arguement"); + } + + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name, + file_ext_name, file_buff.length, new UploadBuff(file_buff, 0, file_buff.length), meta_list); + } + + /** + * upload file to storage server (by file buff, slave file mode) + * + * @param group_name the group name of master file + * @param master_filename the master file name to generate the slave file + * @param prefix_name the prefix name to generate the slave file + * @param file_buff file content/buff + * @param offset start offset of the buff + * @param length the length of buff to upload + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(String group_name, String master_filename, String prefix_name, + byte[] file_buff, int offset, int length, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + if ((group_name == null || group_name.length() == 0) || + (master_filename == null || master_filename.length() == 0) || + (prefix_name == null)) { + throw new MyException("invalid arguement"); + } + + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name, + file_ext_name, length, new UploadBuff(file_buff, offset, length), meta_list); + } + + /** + * upload file to storage server (by callback, slave file mode) + * + * @param group_name the group name to upload file to, can be empty + * @param master_filename the master file name 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_file(String group_name, String master_filename, + String prefix_name, long file_size, UploadCallback callback, + String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name, + file_ext_name, file_size, callback, meta_list); + } + + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_appender_file(String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + final String group_name = null; + return this.upload_appender_file(group_name, local_filename, file_ext_name, meta_list); + } + + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + protected String[] upload_appender_file(String group_name, String local_filename, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + final byte cmd = ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE; + return this.upload_file(cmd, group_name, local_filename, file_ext_name, meta_list); + } + + /** + * upload appender file to storage server (by file buff) + * + * @param file_buff file content/buff + * @param offset start offset of the buff + * @param length the length of buff to upload + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_appender_file(byte[] file_buff, int offset, int length, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + final String group_name = null; + return this.upload_appender_file(group_name, file_buff, offset, length, file_ext_name, meta_list); + } + + /** + * 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 offset start offset of the buff + * @param length the length of buff to upload + * @param file_ext_name file ext name, do not include dot(.) + * @param meta_list meta info array + * @return 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_appender_file(String group_name, byte[] file_buff, int offset, int length, + String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, group_name, null, null, file_ext_name, + length, new UploadBuff(file_buff, offset, length), meta_list); + } + + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_appender_file(byte[] file_buff, String file_ext_name, + NameValuePair[] meta_list) throws IOException, MyException { + final String group_name = null; + return this.upload_appender_file(group_name, file_buff, 0, file_buff.length, file_ext_name, meta_list); + } + + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_appender_file(String group_name, byte[] file_buff, + String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, group_name, null, null, file_ext_name, + file_buff.length, new UploadBuff(file_buff, 0, file_buff.length), meta_list); + } + + /** + * 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 2 elements string array if success:
+ * + * + * return null if fail + */ + public String[] upload_appender_file(String group_name, long file_size, UploadCallback callback, + String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException { + final String master_filename = null; + final String prefix_name = null; + + return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, group_name, master_filename, prefix_name, + file_ext_name, file_size, callback, meta_list); + } + + /** + * append file to storage server (by file name) + * + * @param group_name the group name of appender file + * @param appender_filename the appender filename + * @param local_filename local filename to append + * @return 0 for success, != 0 for error (error no) + */ + public int append_file(String group_name, String appender_filename, String local_filename) throws IOException, MyException { + File f = new File(local_filename); + FileInputStream fis = new FileInputStream(f); + + try { + return this.do_append_file(group_name, appender_filename, f.length(), new UploadStream(fis, f.length())); + } finally { + fis.close(); + } + } + + /** + * append file to storage server (by file buff) + * + * @param group_name the group name of appender file + * @param appender_filename the appender filename + * @param file_buff file content/buff + * @return 0 for success, != 0 for error (error no) + */ + public int append_file(String group_name, String appender_filename, byte[] file_buff) throws IOException, MyException { + return this.do_append_file(group_name, appender_filename, file_buff.length, new UploadBuff(file_buff, 0, file_buff.length)); + } + + /** + * append file to storage server (by file buff) + * + * @param group_name the group name of appender file + * @param appender_filename the appender filename + * @param file_buff file content/buff + * @param offset start offset of the buff + * @param length the length of buff to append + * @return 0 for success, != 0 for error (error no) + */ + public int append_file(String group_name, String appender_filename, + byte[] file_buff, int offset, int length) throws IOException, MyException { + return this.do_append_file(group_name, appender_filename, length, new UploadBuff(file_buff, offset, length)); + } + + /** + * append file to storage server (by callback) + * + * @param group_name the group name to append file to + * @param appender_filename the appender filename + * @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_file(String group_name, String appender_filename, + long file_size, UploadCallback callback) throws IOException, MyException { + return this.do_append_file(group_name, appender_filename, file_size, callback); + } + + /** + * modify appender file to storage server (by file name) + * + * @param group_name the group name of appender file + * @param appender_filename the appender filename + * @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_file(String group_name, String appender_filename, + long file_offset, String local_filename) throws IOException, MyException { + File f = new File(local_filename); + FileInputStream fis = new FileInputStream(f); + + try { + return this.do_modify_file(group_name, appender_filename, file_offset, + f.length(), new UploadStream(fis, f.length())); + } finally { + fis.close(); + } + } + + /** + * modify appender file to storage server (by file buff) + * + * @param group_name the group name of appender file + * @param appender_filename the appender filename + * @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_file(String group_name, String appender_filename, + long file_offset, byte[] file_buff) throws IOException, MyException { + return this.do_modify_file(group_name, appender_filename, file_offset, + file_buff.length, new UploadBuff(file_buff, 0, file_buff.length)); + } + + /** + * modify appender file to storage server (by file buff) + * + * @param group_name the group name of appender file + * @param appender_filename the appender filename + * @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_file(String group_name, String appender_filename, + long file_offset, byte[] file_buff, int buffer_offset, int buffer_length) throws IOException, MyException { + return this.do_modify_file(group_name, appender_filename, file_offset, + buffer_length, new UploadBuff(file_buff, buffer_offset, buffer_length)); + } + + /** + * modify appender file to storage server (by callback) + * + * @param group_name the group name to modify file to + * @param appender_filename the appender filename + * @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_file(String group_name, String appender_filename, + long file_offset, long modify_size, UploadCallback callback) throws IOException, MyException { + return this.do_modify_file(group_name, appender_filename, file_offset, + modify_size, callback); + } + + /** + * upload file to storage server + * + * @param cmd the command code + * @param group_name the group name to upload file to, can be empty + * @param master_filename the master file name to generate the slave file + * @param prefix_name the prefix name to generate the slave file + * @param file_ext_name file ext name, do not include dot(.) + * @param file_size the file size + * @param callback the write data callback object + * @param meta_list meta info array + * @return 2 elements string array if success:
+ * + * + * return null if fail + */ + protected 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 { + byte[] header; + byte[] ext_name_bs; + String new_group_name; + String remote_filename; + boolean bNewConnection; + Socket storageSocket; + byte[] sizeBytes; + byte[] hexLenBytes; + byte[] masterFilenameBytes; + boolean bUploadSlave; + int offset; + long body_len; + + bUploadSlave = ((group_name != null && group_name.length() > 0) && + (master_filename != null && master_filename.length() > 0) && + (prefix_name != null)); + if (bUploadSlave) { + bNewConnection = this.newUpdatableStorageConnection(group_name, master_filename); + } else { + bNewConnection = this.newWritableStorageConnection(group_name); + } + + try { + storageSocket = this.storageServer.getSocket(); + + ext_name_bs = new byte[ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN]; + Arrays.fill(ext_name_bs, (byte) 0); + if (file_ext_name != null && file_ext_name.length() > 0) { + byte[] bs = file_ext_name.getBytes(ClientGlobal.g_charset); + int ext_name_len = bs.length; + if (ext_name_len > ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN) { + ext_name_len = ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN; + } + System.arraycopy(bs, 0, ext_name_bs, 0, ext_name_len); + } + + if (bUploadSlave) { + masterFilenameBytes = master_filename.getBytes(ClientGlobal.g_charset); + + sizeBytes = new byte[2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE]; + body_len = sizeBytes.length + ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + + masterFilenameBytes.length + file_size; + + hexLenBytes = ProtoCommon.long2buff(master_filename.length()); + System.arraycopy(hexLenBytes, 0, sizeBytes, 0, hexLenBytes.length); + offset = hexLenBytes.length; + } else { + masterFilenameBytes = null; + sizeBytes = new byte[1 + 1 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE]; body_len = sizeBytes.length + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + file_size; - sizeBytes[0] = (byte)this.storageServer.getStorePathIndex(); - offset = 1; - } - - hexLenBytes = ProtoCommon.long2buff(file_size); - System.arraycopy(hexLenBytes, 0, sizeBytes, offset, hexLenBytes.length); - - OutputStream out = storageSocket.getOutputStream(); - header = ProtoCommon.packHeader(cmd, body_len, (byte)0); - byte[] wholePkg = new byte[(int)(header.length + body_len - file_size)]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length); - offset = header.length + sizeBytes.length; - if (bUploadSlave) - { - byte[] prefix_name_bs = new byte[ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN]; - byte[] bs = prefix_name.getBytes(ClientGlobal.g_charset); - int prefix_name_len = bs.length; - Arrays.fill(prefix_name_bs, (byte)0); - if (prefix_name_len > ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN) - { - prefix_name_len = ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN; - } - if (prefix_name_len > 0) - { - System.arraycopy(bs, 0, prefix_name_bs, 0, prefix_name_len); - } - - System.arraycopy(prefix_name_bs, 0, wholePkg, offset, prefix_name_bs.length); - offset += prefix_name_bs.length; - } - - System.arraycopy(ext_name_bs, 0, wholePkg, offset, ext_name_bs.length); - offset += ext_name_bs.length; - - if (bUploadSlave) - { - System.arraycopy(masterFilenameBytes, 0, wholePkg, offset, masterFilenameBytes.length); - offset += masterFilenameBytes.length; - } - - out.write(wholePkg); - - if ((this.errno=(byte)callback.send(out)) != 0) - { - return null; - } - - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return null; - } - - if (pkgInfo.body.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) - { - throw new MyException("body length: " + pkgInfo.body.length + " <= " + ProtoCommon.FDFS_GROUP_NAME_MAX_LEN); - } - - new_group_name = new String(pkgInfo.body, 0, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN).trim(); - remote_filename = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, pkgInfo.body.length - ProtoCommon.FDFS_GROUP_NAME_MAX_LEN); - String[] results = new String[2]; - results[0] = new_group_name; - results[1] = remote_filename; - - if (meta_list == null || meta_list.length == 0) - { - return results; - } - - int result = 0; - try - { - result = this.set_metadata(new_group_name, remote_filename, - meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_OVERWRITE); - } - catch(IOException ex) - { - result = 5; - throw ex; - } - finally - { - if (result != 0) - { - this.errno = (byte)result; - this.delete_file(new_group_name, remote_filename); - return null; - } - } - - return results; - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } + sizeBytes[0] = (byte) this.storageServer.getStorePathIndex(); + offset = 1; + } - /** - * append file to storage server - * @param group_name the group name of appender file - * @param appender_filename the appender filename - * @param file_size the file size - * @param callback the write data callback object - * @return return true for success, false for fail - */ - protected int do_append_file(String group_name, String appender_filename, - long file_size, UploadCallback callback) throws IOException, MyException - { - byte[] header; - boolean bNewConnection; - Socket storageSocket; - byte[] hexLenBytes; - byte[] appenderFilenameBytes; - int offset; - long body_len; + hexLenBytes = ProtoCommon.long2buff(file_size); + System.arraycopy(hexLenBytes, 0, sizeBytes, offset, hexLenBytes.length); - if ((group_name == null || group_name.length() == 0) || - (appender_filename == null || appender_filename.length() == 0)) - { - this.errno = ProtoCommon.ERR_NO_EINVAL; - return this.errno; - } - - bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename); - - try - { - storageSocket = this.storageServer.getSocket(); - - appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset); - body_len = 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + file_size; - - header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_APPEND_FILE, body_len, (byte)0); - byte[] wholePkg = new byte[(int)(header.length + body_len - file_size)]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - offset = header.length; - - hexLenBytes = ProtoCommon.long2buff(appender_filename.length()); - System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); - offset += hexLenBytes.length; - - hexLenBytes = ProtoCommon.long2buff(file_size); - System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); - offset += hexLenBytes.length; - - OutputStream out = storageSocket.getOutputStream(); - - System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length); - offset += appenderFilenameBytes.length; - - out.write(wholePkg); - if ((this.errno=(byte)callback.send(out)) != 0) - { - return this.errno; - } - - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return this.errno; - } - - return 0; - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } + OutputStream out = storageSocket.getOutputStream(); + header = ProtoCommon.packHeader(cmd, body_len, (byte) 0); + byte[] wholePkg = new byte[(int) (header.length + body_len - file_size)]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length); + offset = header.length + sizeBytes.length; + if (bUploadSlave) { + byte[] prefix_name_bs = new byte[ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN]; + byte[] bs = prefix_name.getBytes(ClientGlobal.g_charset); + int prefix_name_len = bs.length; + Arrays.fill(prefix_name_bs, (byte) 0); + if (prefix_name_len > ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN) { + prefix_name_len = ProtoCommon.FDFS_FILE_PREFIX_MAX_LEN; + } + if (prefix_name_len > 0) { + System.arraycopy(bs, 0, prefix_name_bs, 0, prefix_name_len); + } - /** - * modify appender file to storage server - * @param group_name the group name of appender file - * @param appender_filename the appender filename - * @param file_offset the offset of appender file - * @param modify_size the modify size - * @param callback the write data callback object - * @return return true for success, false for fail - */ - protected int do_modify_file(String group_name, String appender_filename, - long file_offset, long modify_size, UploadCallback callback) throws IOException, MyException - { - byte[] header; - boolean bNewConnection; - Socket storageSocket; - byte[] hexLenBytes; - byte[] appenderFilenameBytes; - int offset; - long body_len; + System.arraycopy(prefix_name_bs, 0, wholePkg, offset, prefix_name_bs.length); + offset += prefix_name_bs.length; + } - if ((group_name == null || group_name.length() == 0) || - (appender_filename == null || appender_filename.length() == 0)) - { - this.errno = ProtoCommon.ERR_NO_EINVAL; - return this.errno; - } - - bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename); - - try - { - storageSocket = this.storageServer.getSocket(); - - appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset); - body_len = 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + modify_size; - - header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_MODIFY_FILE, body_len, (byte)0); - byte[] wholePkg = new byte[(int)(header.length + body_len - modify_size)]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - offset = header.length; - - hexLenBytes = ProtoCommon.long2buff(appender_filename.length()); - System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); - offset += hexLenBytes.length; + System.arraycopy(ext_name_bs, 0, wholePkg, offset, ext_name_bs.length); + offset += ext_name_bs.length; - hexLenBytes = ProtoCommon.long2buff(file_offset); - System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); - offset += hexLenBytes.length; - - hexLenBytes = ProtoCommon.long2buff(modify_size); - System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); - offset += hexLenBytes.length; - - OutputStream out = storageSocket.getOutputStream(); - - System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length); - offset += appenderFilenameBytes.length; - - out.write(wholePkg); - if ((this.errno=(byte)callback.send(out)) != 0) - { - return this.errno; - } - - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return this.errno; - } - - return 0; - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } - - /** - * delete file from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @return 0 for success, none zero for fail (error code) - */ - public int delete_file(String group_name, String remote_filename) throws IOException, MyException - { - boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename); - Socket storageSocket = this.storageServer.getSocket(); - - try - { - this.send_package(ProtoCommon.STORAGE_PROTO_CMD_DELETE_FILE, group_name, remote_filename); - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); - - this.errno = pkgInfo.errno; - return pkgInfo.errno; - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } - - /** - * truncate appender file to size 0 from storage server - * @param group_name the group name of storage server - * @param appender_filename the appender filename - * @return 0 for success, none zero for fail (error code) - */ - public int truncate_file(String group_name, String appender_filename) throws IOException, MyException - { - final long truncated_file_size = 0; - return this.truncate_file(group_name, appender_filename, truncated_file_size); - } - - /** - * truncate appender file from storage server - * @param group_name the group name of storage server - * @param appender_filename the appender filename - * @param truncated_file_size truncated file size - * @return 0 for success, none zero for fail (error code) - */ - public int truncate_file(String group_name, String appender_filename, - long truncated_file_size) throws IOException, MyException - { - byte[] header; - boolean bNewConnection; - Socket storageSocket; - byte[] hexLenBytes; - byte[] appenderFilenameBytes; - int offset; - int body_len; + if (bUploadSlave) { + System.arraycopy(masterFilenameBytes, 0, wholePkg, offset, masterFilenameBytes.length); + offset += masterFilenameBytes.length; + } - if ((group_name == null || group_name.length() == 0) || - (appender_filename == null || appender_filename.length() == 0)) - { - this.errno = ProtoCommon.ERR_NO_EINVAL; - return this.errno; - } - - bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename); - - try - { - storageSocket = this.storageServer.getSocket(); - - appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset); - body_len = 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length; - - header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_TRUNCATE_FILE, body_len, (byte)0); - byte[] wholePkg = new byte[header.length + body_len]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - offset = header.length; - - hexLenBytes = ProtoCommon.long2buff(appender_filename.length()); - System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); - offset += hexLenBytes.length; - - hexLenBytes = ProtoCommon.long2buff(truncated_file_size); - System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); - offset += hexLenBytes.length; - - OutputStream out = storageSocket.getOutputStream(); - - System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length); - offset += appenderFilenameBytes.length; - - out.write(wholePkg); - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); - this.errno = pkgInfo.errno; - return pkgInfo.errno; - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } - - /** - * download file from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @return file content/buff, return null if fail - */ - public byte[] download_file(String group_name, String remote_filename) throws IOException, MyException - { - final long file_offset = 0; - final long download_bytes = 0; - - return this.download_file(group_name, remote_filename, file_offset, download_bytes); - } - - /** - * download file from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @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_file(String group_name, String remote_filename, long file_offset, long download_bytes) throws IOException, MyException - { - boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename); - Socket storageSocket = this.storageServer.getSocket(); - - try - { - ProtoCommon.RecvPackageInfo pkgInfo; - - this.send_download_package(group_name, remote_filename, file_offset, download_bytes); - pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); - - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return null; - } - - return pkgInfo.body; - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } + out.write(wholePkg); - /** - * download file from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @param local_filename filename on local - * @return 0 success, return none zero errno if fail - */ - public int download_file(String group_name, String remote_filename, - String local_filename) throws IOException, MyException - { - final long file_offset = 0; - final long download_bytes = 0; - return this.download_file(group_name, remote_filename, - file_offset, download_bytes, local_filename); - } - - /** - * download file from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @param file_offset the start offset of the file - * @param download_bytes download bytes, 0 for remain bytes from offset - * @param local_filename filename on local - * @return 0 success, return none zero errno if fail - */ - public int download_file(String group_name, String remote_filename, - long file_offset, long download_bytes, - String local_filename) throws IOException, MyException - { - boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename); - Socket storageSocket = this.storageServer.getSocket(); - try - { - ProtoCommon.RecvHeaderInfo header; - FileOutputStream out = new FileOutputStream(local_filename); - try - { - this.errno = 0; - this.send_download_package(group_name, remote_filename, file_offset, download_bytes); - - InputStream in = storageSocket.getInputStream(); - header = ProtoCommon.recvHeader(in, ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); - this.errno = header.errno; - if (header.errno != 0) - { - return header.errno; - } - - byte[] buff = new byte[256 * 1024]; - long remainBytes = header.body_len; - int bytes; - - //System.out.println("expect_body_len=" + header.body_len); - - while (remainBytes > 0) - { - if ((bytes=in.read(buff, 0, remainBytes > buff.length ? buff.length : (int)remainBytes)) < 0) - { - throw new IOException("recv package size " + (header.body_len - remainBytes) + " != " + header.body_len); - } - - out.write(buff, 0, bytes); - remainBytes -= bytes; - - //System.out.println("totalBytes=" + (header.body_len - remainBytes)); - } - - return 0; - } - catch(IOException ex) - { - if (this.errno == 0) - { - this.errno = ProtoCommon.ERR_NO_EIO; - } - - throw ex; - } - finally - { - out.close(); - if (this.errno != 0) - { - (new File(local_filename)).delete(); - } - } - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } - - /** - * download file from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @param callback call callback.recv() when data arrive - * @return 0 success, return none zero errno if fail - */ - public int download_file(String group_name, String remote_filename, - DownloadCallback callback) throws IOException, MyException - { - final long file_offset = 0; - final long download_bytes = 0; - return this.download_file(group_name, remote_filename, - file_offset, download_bytes, callback); - } - - /** - * download file from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @param file_offset the start offset of the file - * @param download_bytes download bytes, 0 for remain bytes from offset - * @param callback call callback.recv() when data arrive - * @return 0 success, return none zero errno if fail - */ - public int download_file(String group_name, String remote_filename, - long file_offset, long download_bytes, - DownloadCallback callback) throws IOException, MyException - { - int result; - boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename); - Socket storageSocket = this.storageServer.getSocket(); - - try - { - ProtoCommon.RecvHeaderInfo header; - this.send_download_package(group_name, remote_filename, file_offset, download_bytes); - - InputStream in = storageSocket.getInputStream(); - header = ProtoCommon.recvHeader(in, ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); - this.errno = header.errno; - if (header.errno != 0) - { - return header.errno; - } - - byte[] buff = new byte[2 * 1024]; - long remainBytes = header.body_len; - int bytes; - - //System.out.println("expect_body_len=" + header.body_len); - - while (remainBytes > 0) - { - if ((bytes=in.read(buff, 0, remainBytes > buff.length ? buff.length : (int)remainBytes)) < 0) - { - throw new IOException("recv package size " + (header.body_len - remainBytes) + " != " + header.body_len); - } - - if ((result=callback.recv(header.body_len, buff, bytes)) != 0) - { - this.errno = (byte)result; - return result; - } - - remainBytes -= bytes; - //System.out.println("totalBytes=" + (header.body_len - remainBytes)); - } - - return 0; - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } - - /** - * get all metadata items from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @return meta info array, return null if fail - */ - public NameValuePair[] get_metadata(String group_name, String remote_filename)throws IOException, MyException - { - boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename); - Socket storageSocket = this.storageServer.getSocket(); - - try - { - ProtoCommon.RecvPackageInfo pkgInfo; - - this.send_package(ProtoCommon.STORAGE_PROTO_CMD_GET_METADATA, group_name, remote_filename); - pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); - - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return null; - } - - return ProtoCommon.split_metadata(new String(pkgInfo.body, ClientGlobal.g_charset)); - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } - - /** - * set metadata items to storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @param meta_list meta item array - * @param op_flag flag, can be one of following values:
- * - * - * @return 0 for success, !=0 fail (error code) - */ - public int set_metadata(String group_name, String remote_filename, - NameValuePair[] meta_list, byte op_flag) throws IOException, MyException - { - boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename); - Socket storageSocket = this.storageServer.getSocket(); - - try - { - byte[] header; - byte[] groupBytes; - byte[] filenameBytes; - byte[] meta_buff; - byte[] bs; - int groupLen; - byte[] sizeBytes; - ProtoCommon.RecvPackageInfo pkgInfo; - - if (meta_list == null) - { - meta_buff = new byte[0]; - } - else - { - meta_buff = ProtoCommon.pack_metadata(meta_list).getBytes(ClientGlobal.g_charset); - } - - filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset); - sizeBytes = new byte[2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE]; - Arrays.fill(sizeBytes, (byte)0); - - bs = ProtoCommon.long2buff(filenameBytes.length); - System.arraycopy(bs, 0, sizeBytes, 0, bs.length); - bs = ProtoCommon.long2buff(meta_buff.length); - System.arraycopy(bs, 0, sizeBytes, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, bs.length); - - groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - bs = group_name.getBytes(ClientGlobal.g_charset); - - Arrays.fill(groupBytes, (byte)0); - if (bs.length <= groupBytes.length) - { - groupLen = bs.length; - } - else - { - groupLen = groupBytes.length; - } - System.arraycopy(bs, 0, groupBytes, 0, groupLen); - - header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_SET_METADATA, - 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + 1 + groupBytes.length - + filenameBytes.length + meta_buff.length, (byte)0); - OutputStream out = storageSocket.getOutputStream(); - byte[] wholePkg = new byte[header.length + sizeBytes.length + 1 + groupBytes.length + filenameBytes.length]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length); - wholePkg[header.length+sizeBytes.length] = op_flag; - System.arraycopy(groupBytes, 0, wholePkg, header.length+sizeBytes.length+1, groupBytes.length); - System.arraycopy(filenameBytes, 0, wholePkg, header.length+sizeBytes.length+1+groupBytes.length, filenameBytes.length); - out.write(wholePkg); - if (meta_buff.length > 0) - { - out.write(meta_buff); - } - - pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); - - this.errno = pkgInfo.errno; - return pkgInfo.errno; - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } + if ((this.errno = (byte) callback.send(out)) != 0) { + return null; + } - /** - * get file info decoded from the filename, fetch from the storage if necessary - * @param group_name the group name - * @param remote_filename the filename - * @return FileInfo object for success, return null for fail - */ - public FileInfo get_file_info(String group_name, String remote_filename) throws IOException, MyException - { - if (remote_filename.length() < ProtoCommon.FDFS_FILE_PATH_LEN + ProtoCommon.FDFS_FILENAME_BASE64_LENGTH - + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) - { - this.errno = ProtoCommon.ERR_NO_EINVAL; - return null; - } - - byte[] buff = base64.decodeAuto(remote_filename.substring(ProtoCommon.FDFS_FILE_PATH_LEN, - ProtoCommon.FDFS_FILE_PATH_LEN + ProtoCommon.FDFS_FILENAME_BASE64_LENGTH)); - - long file_size = ProtoCommon.buff2long(buff, 4 * 2); - if (((remote_filename.length() > ProtoCommon.TRUNK_LOGIC_FILENAME_LENGTH) || - ((remote_filename.length() > ProtoCommon.NORMAL_LOGIC_FILENAME_LENGTH) && ((file_size & ProtoCommon.TRUNK_FILE_MARK_SIZE) == 0))) || - ((file_size & ProtoCommon.APPENDER_FILE_SIZE) != 0)) - { //slave file or appender file - FileInfo fi = this.query_file_info(group_name, remote_filename); - if (fi == null) - { - return null; - } - return fi; - } - - FileInfo fileInfo = new FileInfo(file_size, 0, 0, ProtoCommon.getIpAddress(buff, 0)); + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return null; + } + + if (pkgInfo.body.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) { + throw new MyException("body length: " + pkgInfo.body.length + " <= " + ProtoCommon.FDFS_GROUP_NAME_MAX_LEN); + } + + new_group_name = new String(pkgInfo.body, 0, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN).trim(); + remote_filename = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, pkgInfo.body.length - ProtoCommon.FDFS_GROUP_NAME_MAX_LEN); + String[] results = new String[2]; + results[0] = new_group_name; + results[1] = remote_filename; + + if (meta_list == null || meta_list.length == 0) { + return results; + } + + int result = 0; + try { + result = this.set_metadata(new_group_name, remote_filename, + meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_OVERWRITE); + } catch (IOException ex) { + result = 5; + throw ex; + } finally { + if (result != 0) { + this.errno = (byte) result; + this.delete_file(new_group_name, remote_filename); + return null; + } + } + + return results; + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * append file to storage server + * + * @param group_name the group name of appender file + * @param appender_filename the appender filename + * @param file_size the file size + * @param callback the write data callback object + * @return return true for success, false for fail + */ + protected int do_append_file(String group_name, String appender_filename, + long file_size, UploadCallback callback) throws IOException, MyException { + byte[] header; + boolean bNewConnection; + Socket storageSocket; + byte[] hexLenBytes; + byte[] appenderFilenameBytes; + int offset; + long body_len; + + if ((group_name == null || group_name.length() == 0) || + (appender_filename == null || appender_filename.length() == 0)) { + this.errno = ProtoCommon.ERR_NO_EINVAL; + return this.errno; + } + + bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename); + + try { + storageSocket = this.storageServer.getSocket(); + + appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset); + body_len = 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + file_size; + + header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_APPEND_FILE, body_len, (byte) 0); + byte[] wholePkg = new byte[(int) (header.length + body_len - file_size)]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + offset = header.length; + + hexLenBytes = ProtoCommon.long2buff(appender_filename.length()); + System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); + offset += hexLenBytes.length; + + hexLenBytes = ProtoCommon.long2buff(file_size); + System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); + offset += hexLenBytes.length; + + OutputStream out = storageSocket.getOutputStream(); + + System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length); + offset += appenderFilenameBytes.length; + + out.write(wholePkg); + if ((this.errno = (byte) callback.send(out)) != 0) { + return this.errno; + } + + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return this.errno; + } + + return 0; + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * modify appender file to storage server + * + * @param group_name the group name of appender file + * @param appender_filename the appender filename + * @param file_offset the offset of appender file + * @param modify_size the modify size + * @param callback the write data callback object + * @return return true for success, false for fail + */ + protected int do_modify_file(String group_name, String appender_filename, + long file_offset, long modify_size, UploadCallback callback) throws IOException, MyException { + byte[] header; + boolean bNewConnection; + Socket storageSocket; + byte[] hexLenBytes; + byte[] appenderFilenameBytes; + int offset; + long body_len; + + if ((group_name == null || group_name.length() == 0) || + (appender_filename == null || appender_filename.length() == 0)) { + this.errno = ProtoCommon.ERR_NO_EINVAL; + return this.errno; + } + + bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename); + + try { + storageSocket = this.storageServer.getSocket(); + + appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset); + body_len = 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length + modify_size; + + header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_MODIFY_FILE, body_len, (byte) 0); + byte[] wholePkg = new byte[(int) (header.length + body_len - modify_size)]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + offset = header.length; + + hexLenBytes = ProtoCommon.long2buff(appender_filename.length()); + System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); + offset += hexLenBytes.length; + + hexLenBytes = ProtoCommon.long2buff(file_offset); + System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); + offset += hexLenBytes.length; + + hexLenBytes = ProtoCommon.long2buff(modify_size); + System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); + offset += hexLenBytes.length; + + OutputStream out = storageSocket.getOutputStream(); + + System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length); + offset += appenderFilenameBytes.length; + + out.write(wholePkg); + if ((this.errno = (byte) callback.send(out)) != 0) { + return this.errno; + } + + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return this.errno; + } + + return 0; + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * delete file from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @return 0 for success, none zero for fail (error code) + */ + public int delete_file(String group_name, String remote_filename) throws IOException, MyException { + boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename); + Socket storageSocket = this.storageServer.getSocket(); + + try { + this.send_package(ProtoCommon.STORAGE_PROTO_CMD_DELETE_FILE, group_name, remote_filename); + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); + + this.errno = pkgInfo.errno; + return pkgInfo.errno; + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * truncate appender file to size 0 from storage server + * + * @param group_name the group name of storage server + * @param appender_filename the appender filename + * @return 0 for success, none zero for fail (error code) + */ + public int truncate_file(String group_name, String appender_filename) throws IOException, MyException { + final long truncated_file_size = 0; + return this.truncate_file(group_name, appender_filename, truncated_file_size); + } + + /** + * truncate appender file from storage server + * + * @param group_name the group name of storage server + * @param appender_filename the appender filename + * @param truncated_file_size truncated file size + * @return 0 for success, none zero for fail (error code) + */ + public int truncate_file(String group_name, String appender_filename, + long truncated_file_size) throws IOException, MyException { + byte[] header; + boolean bNewConnection; + Socket storageSocket; + byte[] hexLenBytes; + byte[] appenderFilenameBytes; + int offset; + int body_len; + + if ((group_name == null || group_name.length() == 0) || + (appender_filename == null || appender_filename.length() == 0)) { + this.errno = ProtoCommon.ERR_NO_EINVAL; + return this.errno; + } + + bNewConnection = this.newUpdatableStorageConnection(group_name, appender_filename); + + try { + storageSocket = this.storageServer.getSocket(); + + appenderFilenameBytes = appender_filename.getBytes(ClientGlobal.g_charset); + body_len = 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + appenderFilenameBytes.length; + + header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_TRUNCATE_FILE, body_len, (byte) 0); + byte[] wholePkg = new byte[header.length + body_len]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + offset = header.length; + + hexLenBytes = ProtoCommon.long2buff(appender_filename.length()); + System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); + offset += hexLenBytes.length; + + hexLenBytes = ProtoCommon.long2buff(truncated_file_size); + System.arraycopy(hexLenBytes, 0, wholePkg, offset, hexLenBytes.length); + offset += hexLenBytes.length; + + OutputStream out = storageSocket.getOutputStream(); + + System.arraycopy(appenderFilenameBytes, 0, wholePkg, offset, appenderFilenameBytes.length); + offset += appenderFilenameBytes.length; + + out.write(wholePkg); + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); + this.errno = pkgInfo.errno; + return pkgInfo.errno; + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * download file from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @return file content/buff, return null if fail + */ + public byte[] download_file(String group_name, String remote_filename) throws IOException, MyException { + final long file_offset = 0; + final long download_bytes = 0; + + return this.download_file(group_name, remote_filename, file_offset, download_bytes); + } + + /** + * download file from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @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_file(String group_name, String remote_filename, long file_offset, long download_bytes) throws IOException, MyException { + boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename); + Socket storageSocket = this.storageServer.getSocket(); + + try { + ProtoCommon.RecvPackageInfo pkgInfo; + + this.send_download_package(group_name, remote_filename, file_offset, download_bytes); + pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); + + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return null; + } + + return pkgInfo.body; + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * download file from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @param local_filename filename on local + * @return 0 success, return none zero errno if fail + */ + public int download_file(String group_name, String remote_filename, + String local_filename) throws IOException, MyException { + final long file_offset = 0; + final long download_bytes = 0; + return this.download_file(group_name, remote_filename, + file_offset, download_bytes, local_filename); + } + + /** + * download file from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @param file_offset the start offset of the file + * @param download_bytes download bytes, 0 for remain bytes from offset + * @param local_filename filename on local + * @return 0 success, return none zero errno if fail + */ + public int download_file(String group_name, String remote_filename, + long file_offset, long download_bytes, + String local_filename) throws IOException, MyException { + boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename); + Socket storageSocket = this.storageServer.getSocket(); + try { + ProtoCommon.RecvHeaderInfo header; + FileOutputStream out = new FileOutputStream(local_filename); + try { + this.errno = 0; + this.send_download_package(group_name, remote_filename, file_offset, download_bytes); + + InputStream in = storageSocket.getInputStream(); + header = ProtoCommon.recvHeader(in, ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); + this.errno = header.errno; + if (header.errno != 0) { + return header.errno; + } + + byte[] buff = new byte[256 * 1024]; + long remainBytes = header.body_len; + int bytes; + + //System.out.println("expect_body_len=" + header.body_len); + + while (remainBytes > 0) { + if ((bytes = in.read(buff, 0, remainBytes > buff.length ? buff.length : (int) remainBytes)) < 0) { + throw new IOException("recv package size " + (header.body_len - remainBytes) + " != " + header.body_len); + } + + out.write(buff, 0, bytes); + remainBytes -= bytes; + + //System.out.println("totalBytes=" + (header.body_len - remainBytes)); + } + + return 0; + } catch (IOException ex) { + if (this.errno == 0) { + this.errno = ProtoCommon.ERR_NO_EIO; + } + + throw ex; + } finally { + out.close(); + if (this.errno != 0) { + (new File(local_filename)).delete(); + } + } + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * download file from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @param callback call callback.recv() when data arrive + * @return 0 success, return none zero errno if fail + */ + public int download_file(String group_name, String remote_filename, + DownloadCallback callback) throws IOException, MyException { + final long file_offset = 0; + final long download_bytes = 0; + return this.download_file(group_name, remote_filename, + file_offset, download_bytes, callback); + } + + /** + * download file from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @param file_offset the start offset of the file + * @param download_bytes download bytes, 0 for remain bytes from offset + * @param callback call callback.recv() when data arrive + * @return 0 success, return none zero errno if fail + */ + public int download_file(String group_name, String remote_filename, + long file_offset, long download_bytes, + DownloadCallback callback) throws IOException, MyException { + int result; + boolean bNewConnection = this.newReadableStorageConnection(group_name, remote_filename); + Socket storageSocket = this.storageServer.getSocket(); + + try { + ProtoCommon.RecvHeaderInfo header; + this.send_download_package(group_name, remote_filename, file_offset, download_bytes); + + InputStream in = storageSocket.getInputStream(); + header = ProtoCommon.recvHeader(in, ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); + this.errno = header.errno; + if (header.errno != 0) { + return header.errno; + } + + byte[] buff = new byte[2 * 1024]; + long remainBytes = header.body_len; + int bytes; + + //System.out.println("expect_body_len=" + header.body_len); + + while (remainBytes > 0) { + if ((bytes = in.read(buff, 0, remainBytes > buff.length ? buff.length : (int) remainBytes)) < 0) { + throw new IOException("recv package size " + (header.body_len - remainBytes) + " != " + header.body_len); + } + + if ((result = callback.recv(header.body_len, buff, bytes)) != 0) { + this.errno = (byte) result; + return result; + } + + remainBytes -= bytes; + //System.out.println("totalBytes=" + (header.body_len - remainBytes)); + } + + return 0; + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * get all metadata items from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @return meta info array, return null if fail + */ + public NameValuePair[] get_metadata(String group_name, String remote_filename) throws IOException, MyException { + boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename); + Socket storageSocket = this.storageServer.getSocket(); + + try { + ProtoCommon.RecvPackageInfo pkgInfo; + + this.send_package(ProtoCommon.STORAGE_PROTO_CMD_GET_METADATA, group_name, remote_filename); + pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, -1); + + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return null; + } + + return ProtoCommon.split_metadata(new String(pkgInfo.body, ClientGlobal.g_charset)); + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * set metadata items to storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @param meta_list meta item array + * @param op_flag flag, can be one of following values:
+ * + * + * @return 0 for success, !=0 fail (error code) + */ + public int set_metadata(String group_name, String remote_filename, + NameValuePair[] meta_list, byte op_flag) throws IOException, MyException { + boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename); + Socket storageSocket = this.storageServer.getSocket(); + + try { + byte[] header; + byte[] groupBytes; + byte[] filenameBytes; + byte[] meta_buff; + byte[] bs; + int groupLen; + byte[] sizeBytes; + ProtoCommon.RecvPackageInfo pkgInfo; + + if (meta_list == null) { + meta_buff = new byte[0]; + } else { + meta_buff = ProtoCommon.pack_metadata(meta_list).getBytes(ClientGlobal.g_charset); + } + + filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset); + sizeBytes = new byte[2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE]; + Arrays.fill(sizeBytes, (byte) 0); + + bs = ProtoCommon.long2buff(filenameBytes.length); + System.arraycopy(bs, 0, sizeBytes, 0, bs.length); + bs = ProtoCommon.long2buff(meta_buff.length); + System.arraycopy(bs, 0, sizeBytes, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, bs.length); + + groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; + bs = group_name.getBytes(ClientGlobal.g_charset); + + Arrays.fill(groupBytes, (byte) 0); + if (bs.length <= groupBytes.length) { + groupLen = bs.length; + } else { + groupLen = groupBytes.length; + } + System.arraycopy(bs, 0, groupBytes, 0, groupLen); + + header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_SET_METADATA, + 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + 1 + groupBytes.length + + filenameBytes.length + meta_buff.length, (byte) 0); + OutputStream out = storageSocket.getOutputStream(); + byte[] wholePkg = new byte[header.length + sizeBytes.length + 1 + groupBytes.length + filenameBytes.length]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + System.arraycopy(sizeBytes, 0, wholePkg, header.length, sizeBytes.length); + wholePkg[header.length + sizeBytes.length] = op_flag; + System.arraycopy(groupBytes, 0, wholePkg, header.length + sizeBytes.length + 1, groupBytes.length); + System.arraycopy(filenameBytes, 0, wholePkg, header.length + sizeBytes.length + 1 + groupBytes.length, filenameBytes.length); + out.write(wholePkg); + if (meta_buff.length > 0) { + out.write(meta_buff); + } + + pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, 0); + + this.errno = pkgInfo.errno; + return pkgInfo.errno; + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } + } + + /** + * get file info decoded from the filename, fetch from the storage if necessary + * + * @param group_name the group name + * @param remote_filename the filename + * @return FileInfo object for success, return null for fail + */ + public FileInfo get_file_info(String group_name, String remote_filename) throws IOException, MyException { + if (remote_filename.length() < ProtoCommon.FDFS_FILE_PATH_LEN + ProtoCommon.FDFS_FILENAME_BASE64_LENGTH + + ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) { + this.errno = ProtoCommon.ERR_NO_EINVAL; + return null; + } + + byte[] buff = base64.decodeAuto(remote_filename.substring(ProtoCommon.FDFS_FILE_PATH_LEN, + ProtoCommon.FDFS_FILE_PATH_LEN + ProtoCommon.FDFS_FILENAME_BASE64_LENGTH)); + + long file_size = ProtoCommon.buff2long(buff, 4 * 2); + if (((remote_filename.length() > ProtoCommon.TRUNK_LOGIC_FILENAME_LENGTH) || + ((remote_filename.length() > ProtoCommon.NORMAL_LOGIC_FILENAME_LENGTH) && ((file_size & ProtoCommon.TRUNK_FILE_MARK_SIZE) == 0))) || + ((file_size & ProtoCommon.APPENDER_FILE_SIZE) != 0)) { //slave file or appender file + FileInfo fi = this.query_file_info(group_name, remote_filename); + if (fi == null) { + return null; + } + return fi; + } + + FileInfo fileInfo = new FileInfo(file_size, 0, 0, ProtoCommon.getIpAddress(buff, 0)); fileInfo.setCreateTimestamp(ProtoCommon.buff2int(buff, 4)); - if ((file_size >> 63) != 0) - { - file_size &= 0xFFFFFFFFL; //low 32 bits is file size - fileInfo.setFileSize(file_size); + if ((file_size >> 63) != 0) { + file_size &= 0xFFFFFFFFL; //low 32 bits is file size + fileInfo.setFileSize(file_size); } fileInfo.setCrc32(ProtoCommon.buff2int(buff, 4 * 4)); - - return fileInfo; - } - /** - * get file info from storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @return FileInfo object for success, return null for fail - */ - public FileInfo query_file_info(String group_name, String remote_filename) throws IOException, MyException - { - boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename); - Socket storageSocket = this.storageServer.getSocket(); - - try - { - byte[] header; - byte[] groupBytes; - byte[] filenameBytes; - byte[] bs; - int groupLen; - ProtoCommon.RecvPackageInfo pkgInfo; - - filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset); - groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - bs = group_name.getBytes(ClientGlobal.g_charset); - - Arrays.fill(groupBytes, (byte)0); - if (bs.length <= groupBytes.length) - { - groupLen = bs.length; - } - else - { - groupLen = groupBytes.length; - } - System.arraycopy(bs, 0, groupBytes, 0, groupLen); - - header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_QUERY_FILE_INFO, - + groupBytes.length + filenameBytes.length, (byte)0); - OutputStream out = storageSocket.getOutputStream(); - byte[] wholePkg = new byte[header.length + groupBytes.length + filenameBytes.length]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - System.arraycopy(groupBytes, 0, wholePkg, header.length, groupBytes.length); - System.arraycopy(filenameBytes, 0, wholePkg, header.length + groupBytes.length, filenameBytes.length); - out.write(wholePkg); - - pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), - ProtoCommon.STORAGE_PROTO_CMD_RESP, - 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + - ProtoCommon.FDFS_IPADDR_SIZE); - - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return null; - } - - long file_size = ProtoCommon.buff2long(pkgInfo.body, 0); - int create_timestamp = (int)ProtoCommon.buff2long(pkgInfo.body, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - int crc32 = (int)ProtoCommon.buff2long(pkgInfo.body, 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - String source_ip_addr = (new String(pkgInfo.body, 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, ProtoCommon.FDFS_IPADDR_SIZE)).trim(); - return new FileInfo(file_size, create_timestamp, crc32, source_ip_addr); - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - this.storageServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - finally - { - this.storageServer = null; - } - } - } - } - - /** - * check storage socket, if null create a new connection - * @param group_name the group name to upload file to, can be empty - * @return true if create a new connection - */ - protected boolean newWritableStorageConnection(String group_name) throws IOException, MyException - { - if (this.storageServer != null) - { - return false; - } - else - { - TrackerClient tracker = new TrackerClient(); - this.storageServer = tracker.getStoreStorage(this.trackerServer, group_name); - if (this.storageServer == null) - { - throw new MyException("getStoreStorage fail, errno code: " + tracker.getErrorCode()); - } - return true; - } + return fileInfo; } - /** - * check storage socket, if null create a new connection - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @return true if create a new connection - */ - protected boolean newReadableStorageConnection(String group_name, String remote_filename) throws IOException, MyException - { - if (this.storageServer != null) - { - return false; - } - else - { - TrackerClient tracker = new TrackerClient(); - this.storageServer = tracker.getFetchStorage(this.trackerServer, group_name, remote_filename); - if (this.storageServer == null) - { - throw new MyException("getStoreStorage fail, errno code: " + tracker.getErrorCode()); - } - return true; - } + /** + * get file info from storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @return FileInfo object for success, return null for fail + */ + public FileInfo query_file_info(String group_name, String remote_filename) throws IOException, MyException { + boolean bNewConnection = this.newUpdatableStorageConnection(group_name, remote_filename); + Socket storageSocket = this.storageServer.getSocket(); + + try { + byte[] header; + byte[] groupBytes; + byte[] filenameBytes; + byte[] bs; + int groupLen; + ProtoCommon.RecvPackageInfo pkgInfo; + + filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset); + groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; + bs = group_name.getBytes(ClientGlobal.g_charset); + + Arrays.fill(groupBytes, (byte) 0); + if (bs.length <= groupBytes.length) { + groupLen = bs.length; + } else { + groupLen = groupBytes.length; + } + System.arraycopy(bs, 0, groupBytes, 0, groupLen); + + header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_QUERY_FILE_INFO, + +groupBytes.length + filenameBytes.length, (byte) 0); + OutputStream out = storageSocket.getOutputStream(); + byte[] wholePkg = new byte[header.length + groupBytes.length + filenameBytes.length]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + System.arraycopy(groupBytes, 0, wholePkg, header.length, groupBytes.length); + System.arraycopy(filenameBytes, 0, wholePkg, header.length + groupBytes.length, filenameBytes.length); + out.write(wholePkg); + + pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(), + ProtoCommon.STORAGE_PROTO_CMD_RESP, + 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE + + ProtoCommon.FDFS_IPADDR_SIZE); + + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return null; + } + + long file_size = ProtoCommon.buff2long(pkgInfo.body, 0); + int create_timestamp = (int) ProtoCommon.buff2long(pkgInfo.body, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + int crc32 = (int) ProtoCommon.buff2long(pkgInfo.body, 2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + String source_ip_addr = (new String(pkgInfo.body, 3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE, ProtoCommon.FDFS_IPADDR_SIZE)).trim(); + return new FileInfo(file_size, create_timestamp, crc32, source_ip_addr); + } catch (IOException ex) { + if (!bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + this.storageServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } finally { + this.storageServer = null; + } + } + } } - /** - * check storage socket, if null create a new connection - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @return true if create a new connection - */ - protected boolean newUpdatableStorageConnection(String group_name, String remote_filename) throws IOException, MyException - { - if (this.storageServer != null) - { - return false; - } - else - { - TrackerClient tracker = new TrackerClient(); - this.storageServer = tracker.getUpdateStorage(this.trackerServer, group_name, remote_filename); - if (this.storageServer == null) - { - throw new MyException("getStoreStorage fail, errno code: " + tracker.getErrorCode()); - } - return true; - } + /** + * check storage socket, if null create a new connection + * + * @param group_name the group name to upload file to, can be empty + * @return true if create a new connection + */ + protected boolean newWritableStorageConnection(String group_name) throws IOException, MyException { + if (this.storageServer != null) { + return false; + } else { + TrackerClient tracker = new TrackerClient(); + this.storageServer = tracker.getStoreStorage(this.trackerServer, group_name); + if (this.storageServer == null) { + throw new MyException("getStoreStorage fail, errno code: " + tracker.getErrorCode()); + } + return true; + } + } + + /** + * check storage socket, if null create a new connection + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @return true if create a new connection + */ + protected boolean newReadableStorageConnection(String group_name, String remote_filename) throws IOException, MyException { + if (this.storageServer != null) { + return false; + } else { + TrackerClient tracker = new TrackerClient(); + this.storageServer = tracker.getFetchStorage(this.trackerServer, group_name, remote_filename); + if (this.storageServer == null) { + throw new MyException("getStoreStorage fail, errno code: " + tracker.getErrorCode()); + } + return true; + } + } + + /** + * check storage socket, if null create a new connection + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @return true if create a new connection + */ + protected boolean newUpdatableStorageConnection(String group_name, String remote_filename) throws IOException, MyException { + if (this.storageServer != null) { + return false; + } else { + TrackerClient tracker = new TrackerClient(); + this.storageServer = tracker.getUpdateStorage(this.trackerServer, group_name, remote_filename); + if (this.storageServer == null) { + throw new MyException("getStoreStorage fail, errno code: " + tracker.getErrorCode()); + } + return true; + } + } + + /** + * send package to storage server + * + * @param cmd which command to send + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + */ + protected void send_package(byte cmd, String group_name, String remote_filename) throws IOException { + byte[] header; + byte[] groupBytes; + byte[] filenameBytes; + byte[] bs; + int groupLen; + + groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; + bs = group_name.getBytes(ClientGlobal.g_charset); + filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset); + + Arrays.fill(groupBytes, (byte) 0); + if (bs.length <= groupBytes.length) { + groupLen = bs.length; + } else { + groupLen = groupBytes.length; + } + System.arraycopy(bs, 0, groupBytes, 0, groupLen); + + header = ProtoCommon.packHeader(cmd, groupBytes.length + filenameBytes.length, (byte) 0); + byte[] wholePkg = new byte[header.length + groupBytes.length + filenameBytes.length]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + System.arraycopy(groupBytes, 0, wholePkg, header.length, groupBytes.length); + System.arraycopy(filenameBytes, 0, wholePkg, header.length + groupBytes.length, filenameBytes.length); + this.storageServer.getSocket().getOutputStream().write(wholePkg); + } + + /** + * send package to storage server + * + * @param group_name the group name of storage server + * @param remote_filename filename on storage server + * @param file_offset the start offset of the file + * @param download_bytes download bytes + */ + protected void send_download_package(String group_name, String remote_filename, long file_offset, long download_bytes) throws IOException { + byte[] header; + byte[] bsOffset; + byte[] bsDownBytes; + byte[] groupBytes; + byte[] filenameBytes; + byte[] bs; + int groupLen; + + bsOffset = ProtoCommon.long2buff(file_offset); + bsDownBytes = ProtoCommon.long2buff(download_bytes); + groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; + bs = group_name.getBytes(ClientGlobal.g_charset); + filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset); + + Arrays.fill(groupBytes, (byte) 0); + if (bs.length <= groupBytes.length) { + groupLen = bs.length; + } else { + groupLen = groupBytes.length; + } + System.arraycopy(bs, 0, groupBytes, 0, groupLen); + + header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_DOWNLOAD_FILE, + bsOffset.length + bsDownBytes.length + groupBytes.length + filenameBytes.length, (byte) 0); + byte[] wholePkg = new byte[header.length + bsOffset.length + bsDownBytes.length + groupBytes.length + filenameBytes.length]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + System.arraycopy(bsOffset, 0, wholePkg, header.length, bsOffset.length); + System.arraycopy(bsDownBytes, 0, wholePkg, header.length + bsOffset.length, bsDownBytes.length); + System.arraycopy(groupBytes, 0, wholePkg, header.length + bsOffset.length + bsDownBytes.length, groupBytes.length); + System.arraycopy(filenameBytes, 0, wholePkg, header.length + bsOffset.length + bsDownBytes.length + groupBytes.length, filenameBytes.length); + this.storageServer.getSocket().getOutputStream().write(wholePkg); + } + + /** + * Upload file by file buff + * + * @author Happy Fish / YuQing + * @version Version 1.12 + */ + public static class UploadBuff implements UploadCallback { + private byte[] fileBuff; + private int offset; + private int length; + + /** + * constructor + * + * @param fileBuff the file buff for uploading + */ + public UploadBuff(byte[] fileBuff, int offset, int length) { + super(); + this.fileBuff = fileBuff; + this.offset = offset; + this.length = length; + } + + /** + * 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 { + out.write(this.fileBuff, this.offset, this.length); + + return 0; + } } - - /** - * send package to storage server - * @param cmd which command to send - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - */ - protected void send_package(byte cmd, String group_name, String remote_filename) throws IOException - { - byte[] header; - byte[] groupBytes; - byte[] filenameBytes; - byte[] bs; - int groupLen; - - groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - bs = group_name.getBytes(ClientGlobal.g_charset); - filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset); - - Arrays.fill(groupBytes, (byte)0); - if (bs.length <= groupBytes.length) - { - groupLen = bs.length; - } - else - { - groupLen = groupBytes.length; - } - System.arraycopy(bs, 0, groupBytes, 0, groupLen); - - header = ProtoCommon.packHeader(cmd, groupBytes.length + filenameBytes.length, (byte)0); - byte[] wholePkg = new byte[header.length + groupBytes.length + filenameBytes.length]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - System.arraycopy(groupBytes, 0, wholePkg, header.length, groupBytes.length); - System.arraycopy(filenameBytes, 0, wholePkg, header.length+groupBytes.length, filenameBytes.length); - this.storageServer.getSocket().getOutputStream().write(wholePkg); - } - - /** - * send package to storage server - * @param group_name the group name of storage server - * @param remote_filename filename on storage server - * @param file_offset the start offset of the file - * @param download_bytes download bytes - */ - protected void send_download_package(String group_name, String remote_filename, long file_offset, long download_bytes) throws IOException - { - byte[] header; - byte[] bsOffset; - byte[] bsDownBytes; - byte[] groupBytes; - byte[] filenameBytes; - byte[] bs; - int groupLen; - - bsOffset = ProtoCommon.long2buff(file_offset); - bsDownBytes = ProtoCommon.long2buff(download_bytes); - groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - bs = group_name.getBytes(ClientGlobal.g_charset); - filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset); - - Arrays.fill(groupBytes, (byte)0); - if (bs.length <= groupBytes.length) - { - groupLen = bs.length; - } - else - { - groupLen = groupBytes.length; - } - System.arraycopy(bs, 0, groupBytes, 0, groupLen); - - header = ProtoCommon.packHeader(ProtoCommon.STORAGE_PROTO_CMD_DOWNLOAD_FILE, - bsOffset.length + bsDownBytes.length + groupBytes.length + filenameBytes.length, (byte)0); - byte[] wholePkg = new byte[header.length + bsOffset.length + bsDownBytes.length + groupBytes.length + filenameBytes.length]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - System.arraycopy(bsOffset, 0, wholePkg, header.length, bsOffset.length); - System.arraycopy(bsDownBytes, 0, wholePkg, header.length+bsOffset.length, bsDownBytes.length); - System.arraycopy(groupBytes, 0, wholePkg, header.length+bsOffset.length+bsDownBytes.length, groupBytes.length); - System.arraycopy(filenameBytes, 0, wholePkg, header.length+bsOffset.length+bsDownBytes.length+groupBytes.length, filenameBytes.length); - this.storageServer.getSocket().getOutputStream().write(wholePkg); - } } diff --git a/src/main/java/org/csource/fastdfs/StorageClient1.java b/src/main/java/org/csource/fastdfs/StorageClient1.java index c6b7f47..51f3bcd 100644 --- a/src/main/java/org/csource/fastdfs/StorageClient1.java +++ b/src/main/java/org/csource/fastdfs/StorageClient1.java @@ -1,799 +1,732 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.common.*; +import org.csource.common.MyException; +import org.csource.common.NameValuePair; + +import java.io.IOException; /** -* Storage client for 1 field file id: combined group name and filename -* @author Happy Fish / YuQing -* @version Version 1.21 -*/ -public class StorageClient1 extends StorageClient -{ - public static final String SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR = "/"; - -/** -* constructor -*/ - public StorageClient1() - { - super(); - } - -/** -* constructor -* @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); - } + * Storage client for 1 field file id: combined group name and filename + * + * @author Happy Fish / YuQing + * @version Version 1.21 + */ +public class StorageClient1 extends StorageClient { + public static final String SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR = "/"; - 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; - } - - results[0] = file_id.substring(0, pos); //group name - results[1] = file_id.substring(pos + 1); //file name - return 0; + /** + * constructor + */ + public StorageClient1() { + super(); } - - /** - * 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; - } - } + /** + * constructor + * + * @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); + } - /** - * 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; - } - } + 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 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; - } - } + results[0] = file_id.substring(0, pos); //group name + results[1] = file_id.substring(pos + 1); //file name + return 0; + } - /** - * 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 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 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 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 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 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 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; - } - - 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; - } - - 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; - } - } + /** + * 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 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; - } - - 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; - } - - 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; - } - } + /** + * 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; + } + } - /** - * 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; - } - - 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; - } - - 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; - } - - return this.append_file(parts[0], parts[1], file_buff, offset, length); - } + /** + * 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; + } + } - /** - * 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; - } - - 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; - } - - 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; - } - - 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; - } - - 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); - } - - /** - * 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]); - } + /** + * 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; + } + } - /** - * 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); - } + /** + * 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; + } + } - /** - * 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); - } + /** + * 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; + } + } - /** - * 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:
- *

- * - * @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]); - } + /** + * 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; + } + + 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; + } + + 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; + } + } + + /** + * 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; + } + + 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; + } + + 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; + } + + 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; + } + + 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; + } + + 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; + } + + 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; + } + + 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; + } + + 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; + } + + 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); + } + + /** + * 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:
+ * + * + * @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/src/main/java/org/csource/fastdfs/StorageServer.java b/src/main/java/org/csource/fastdfs/StorageServer.java index c4f3daf..6495278 100644 --- a/src/main/java/org/csource/fastdfs/StorageServer.java +++ b/src/main/java/org/csource/fastdfs/StorageServer.java @@ -1,63 +1,57 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.common.*; +import java.io.IOException; +import java.net.InetSocketAddress; /** -* Storage Server Info -* @author Happy Fish / YuQing -* @version Version 1.11 -*/ -public class StorageServer extends TrackerServer -{ - protected int store_path_index = 0; - -/** -* Constructor -* @param ip_addr the ip address of storage server -* @param port the port of storage server -* @param store_path the store path index on the storage server -*/ - public StorageServer(String ip_addr, int port, int store_path) throws IOException - { - super(ClientGlobal.getSocket(ip_addr, port), new InetSocketAddress(ip_addr, port)); - this.store_path_index = store_path; - } + * Storage Server Info + * + * @author Happy Fish / YuQing + * @version Version 1.11 + */ +public class StorageServer extends TrackerServer { + protected int store_path_index = 0; -/** -* Constructor -* @param ip_addr the ip address of storage server -* @param port the port of storage server -* @param store_path the store path index on the storage server -*/ - public StorageServer(String ip_addr, int port, byte store_path) throws IOException - { - super(ClientGlobal.getSocket(ip_addr, port), new InetSocketAddress(ip_addr, port)); - if (store_path < 0) - { - this.store_path_index = 256 + store_path; - } - else - { - this.store_path_index = store_path; - } - } - -/** -* @return the store path index on the storage server -*/ - public int getStorePathIndex() - { - return this.store_path_index; - } + /** + * Constructor + * + * @param ip_addr the ip address of storage server + * @param port the port of storage server + * @param store_path the store path index on the storage server + */ + public StorageServer(String ip_addr, int port, int store_path) throws IOException { + super(ClientGlobal.getSocket(ip_addr, port), new InetSocketAddress(ip_addr, port)); + this.store_path_index = store_path; + } + + /** + * Constructor + * + * @param ip_addr the ip address of storage server + * @param port the port of storage server + * @param store_path the store path index on the storage server + */ + public StorageServer(String ip_addr, int port, byte store_path) throws IOException { + super(ClientGlobal.getSocket(ip_addr, port), new InetSocketAddress(ip_addr, port)); + if (store_path < 0) { + this.store_path_index = 256 + store_path; + } else { + this.store_path_index = store_path; + } + } + + /** + * @return the store path index on the storage server + */ + public int getStorePathIndex() { + return this.store_path_index; + } } diff --git a/src/main/java/org/csource/fastdfs/StructBase.java b/src/main/java/org/csource/fastdfs/StructBase.java index bf29bf5..4502c36 100644 --- a/src/main/java/org/csource/fastdfs/StructBase.java +++ b/src/main/java/org/csource/fastdfs/StructBase.java @@ -1,10 +1,10 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; @@ -12,73 +12,62 @@ import java.io.UnsupportedEncodingException; import java.util.Date; /** -* C struct body decoder -* @author Happy Fish / YuQing -* @version Version 1.17 -*/ -public abstract class StructBase -{ - protected static class FieldInfo - { - protected String name; - protected int offset; - protected int size; - - public FieldInfo(String name, int offset, int size) - { - this.name = name; - this.offset = offset; - this.size = size; - } - } - -/** -* set fields -* @param bs byte array -* @param offset start offset -*/ - public abstract void setFields(byte[] bs, int offset); - - protected String stringValue(byte[] bs, int offset, FieldInfo filedInfo) - { - try - { - return (new String(bs, offset + filedInfo.offset, filedInfo.size, ClientGlobal.g_charset)).trim(); - } - catch(UnsupportedEncodingException ex) - { - ex.printStackTrace(); - return null; - } - } - - protected long longValue(byte[] bs, int offset, FieldInfo filedInfo) - { - return ProtoCommon.buff2long(bs, offset + filedInfo.offset); - } - - protected int intValue(byte[] bs, int offset, FieldInfo filedInfo) - { - return (int)ProtoCommon.buff2long(bs, offset + filedInfo.offset); - } - - protected int int32Value(byte[] bs, int offset, FieldInfo filedInfo) - { - return ProtoCommon.buff2int(bs, offset + filedInfo.offset); - } + * C struct body decoder + * + * @author Happy Fish / YuQing + * @version Version 1.17 + */ +public abstract class StructBase { + /** + * set fields + * + * @param bs byte array + * @param offset start offset + */ + public abstract void setFields(byte[] bs, int offset); - protected byte byteValue(byte[] bs, int offset, FieldInfo filedInfo) - { - return bs[offset + filedInfo.offset]; - } + protected String stringValue(byte[] bs, int offset, FieldInfo filedInfo) { + try { + return (new String(bs, offset + filedInfo.offset, filedInfo.size, ClientGlobal.g_charset)).trim(); + } catch (UnsupportedEncodingException ex) { + ex.printStackTrace(); + return null; + } + } - protected boolean booleanValue(byte[] bs, int offset, FieldInfo filedInfo) - { - return bs[offset + filedInfo.offset] != 0; - } - - protected Date dateValue(byte[] bs, int offset, FieldInfo filedInfo) - { - return new Date(ProtoCommon.buff2long(bs, offset + filedInfo.offset) * 1000); - } + protected long longValue(byte[] bs, int offset, FieldInfo filedInfo) { + return ProtoCommon.buff2long(bs, offset + filedInfo.offset); + } + + protected int intValue(byte[] bs, int offset, FieldInfo filedInfo) { + return (int) ProtoCommon.buff2long(bs, offset + filedInfo.offset); + } + + protected int int32Value(byte[] bs, int offset, FieldInfo filedInfo) { + return ProtoCommon.buff2int(bs, offset + filedInfo.offset); + } + + protected byte byteValue(byte[] bs, int offset, FieldInfo filedInfo) { + return bs[offset + filedInfo.offset]; + } + + protected boolean booleanValue(byte[] bs, int offset, FieldInfo filedInfo) { + return bs[offset + filedInfo.offset] != 0; + } + + protected Date dateValue(byte[] bs, int offset, FieldInfo filedInfo) { + return new Date(ProtoCommon.buff2long(bs, offset + filedInfo.offset) * 1000); + } + + protected static class FieldInfo { + protected String name; + protected int offset; + protected int size; + + public FieldInfo(String name, int offset, int size) { + this.name = name; + this.offset = offset; + this.size = size; + } + } } diff --git a/src/main/java/org/csource/fastdfs/StructGroupStat.java b/src/main/java/org/csource/fastdfs/StructGroupStat.java index 41cbb47..34a8d94 100644 --- a/src/main/java/org/csource/fastdfs/StructGroupStat.java +++ b/src/main/java/org/csource/fastdfs/StructGroupStat.java @@ -1,226 +1,225 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; /** -* C struct body decoder -* @author Happy Fish / YuQing -* @version Version 1.18 -*/ -public class StructGroupStat extends StructBase -{ - protected static final int FIELD_INDEX_GROUP_NAME = 0; - protected static final int FIELD_INDEX_TOTAL_MB = 1; - protected static final int FIELD_INDEX_FREE_MB = 2; - protected static final int FIELD_INDEX_TRUNK_FREE_MB = 3; - protected static final int FIELD_INDEX_STORAGE_COUNT = 4; - protected static final int FIELD_INDEX_STORAGE_PORT = 5; - protected static final int FIELD_INDEX_STORAGE_HTTP_PORT = 6; - protected static final int FIELD_INDEX_ACTIVE_COUNT = 7; - protected static final int FIELD_INDEX_CURRENT_WRITE_SERVER = 8; - protected static final int FIELD_INDEX_STORE_PATH_COUNT = 9; - protected static final int FIELD_INDEX_SUBDIR_COUNT_PER_PATH = 10; - protected static final int FIELD_INDEX_CURRENT_TRUNK_FILE_ID = 11; - - protected static int fieldsTotalSize; - protected static StructBase.FieldInfo[] fieldsArray = new StructBase.FieldInfo[12]; - - static - { - int offset = 0; - fieldsArray[FIELD_INDEX_GROUP_NAME] = new StructBase.FieldInfo("groupName", offset, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1); - offset += ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1; - - fieldsArray[FIELD_INDEX_TOTAL_MB] = new StructBase.FieldInfo("totalMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_FREE_MB] = new StructBase.FieldInfo("freeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TRUNK_FREE_MB] = new StructBase.FieldInfo("trunkFreeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + * C struct body decoder + * + * @author Happy Fish / YuQing + * @version Version 1.18 + */ +public class StructGroupStat extends StructBase { + protected static final int FIELD_INDEX_GROUP_NAME = 0; + protected static final int FIELD_INDEX_TOTAL_MB = 1; + protected static final int FIELD_INDEX_FREE_MB = 2; + protected static final int FIELD_INDEX_TRUNK_FREE_MB = 3; + protected static final int FIELD_INDEX_STORAGE_COUNT = 4; + protected static final int FIELD_INDEX_STORAGE_PORT = 5; + protected static final int FIELD_INDEX_STORAGE_HTTP_PORT = 6; + protected static final int FIELD_INDEX_ACTIVE_COUNT = 7; + protected static final int FIELD_INDEX_CURRENT_WRITE_SERVER = 8; + protected static final int FIELD_INDEX_STORE_PATH_COUNT = 9; + protected static final int FIELD_INDEX_SUBDIR_COUNT_PER_PATH = 10; + protected static final int FIELD_INDEX_CURRENT_TRUNK_FILE_ID = 11; - fieldsArray[FIELD_INDEX_STORAGE_COUNT] = new StructBase.FieldInfo("storageCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_STORAGE_PORT] = new StructBase.FieldInfo("storagePort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT] = new StructBase.FieldInfo("storageHttpPort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_ACTIVE_COUNT] = new StructBase.FieldInfo("activeCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_CURRENT_WRITE_SERVER] = new StructBase.FieldInfo("currentWriteServer", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_STORE_PATH_COUNT] = new StructBase.FieldInfo("storePathCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH] = new StructBase.FieldInfo("subdirCountPerPath", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + protected static int fieldsTotalSize; + protected static StructBase.FieldInfo[] fieldsArray = new StructBase.FieldInfo[12]; - fieldsArray[FIELD_INDEX_CURRENT_TRUNK_FILE_ID] = new StructBase.FieldInfo("currentTrunkFileId", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsTotalSize = offset; - } - - protected String groupName; //name of this group - protected long totalMB; //total disk storage in MB - protected long freeMB; //free disk space in MB - protected long trunkFreeMB; //trunk free space in MB - protected int storageCount; //storage server count + static { + int offset = 0; + fieldsArray[FIELD_INDEX_GROUP_NAME] = new StructBase.FieldInfo("groupName", offset, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1); + offset += ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1; + + fieldsArray[FIELD_INDEX_TOTAL_MB] = new StructBase.FieldInfo("totalMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_FREE_MB] = new StructBase.FieldInfo("freeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TRUNK_FREE_MB] = new StructBase.FieldInfo("trunkFreeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_STORAGE_COUNT] = new StructBase.FieldInfo("storageCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_STORAGE_PORT] = new StructBase.FieldInfo("storagePort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT] = new StructBase.FieldInfo("storageHttpPort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_ACTIVE_COUNT] = new StructBase.FieldInfo("activeCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_CURRENT_WRITE_SERVER] = new StructBase.FieldInfo("currentWriteServer", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_STORE_PATH_COUNT] = new StructBase.FieldInfo("storePathCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH] = new StructBase.FieldInfo("subdirCountPerPath", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_CURRENT_TRUNK_FILE_ID] = new StructBase.FieldInfo("currentTrunkFileId", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsTotalSize = offset; + } + + protected String groupName; //name of this group + protected long totalMB; //total disk storage in MB + protected long freeMB; //free disk space in MB + protected long trunkFreeMB; //trunk free space in MB + protected int storageCount; //storage server count protected int storagePort; //storage server port protected int storageHttpPort; //storage server HTTP port protected int activeCount; //active storage server count protected int currentWriteServer; //current storage server index to upload file protected int storePathCount; //store base path count of each storage server protected int subdirCountPerPath; //sub dir count per store path - protected int currentTrunkFileId; //current trunk file id - -/** -* get group name -* @return group name -*/ - public String getGroupName() - { - return this.groupName; + protected int currentTrunkFileId; //current trunk file id + + /** + * get fields total size + * + * @return fields total size + */ + public static int getFieldsTotalSize() { + return fieldsTotalSize; } -/** -* get total disk space in MB -* @return total disk space in MB -*/ - public long getTotalMB() - { - return this.totalMB; - } - -/** -* get free disk space in MB -* @return free disk space in MB -*/ - public long getFreeMB() - { - return this.freeMB; + /** + * get group name + * + * @return group name + */ + public String getGroupName() { + return this.groupName; } -/** -* get trunk free space in MB -* @return trunk free space in MB -*/ - public long getTrunkFreeMB() - { - return this.trunkFreeMB; + /** + * get total disk space in MB + * + * @return total disk space in MB + */ + public long getTotalMB() { + return this.totalMB; } -/** -* get storage server count in this group -* @return storage server count in this group -*/ - public int getStorageCount() - { - return this.storageCount; - } - -/** -* get active storage server count in this group -* @return active storage server count in this group -*/ - public int getActiveCount() - { - return this.activeCount; - } - -/** -* get storage server port -* @return storage server port -*/ - public int getStoragePort() - { - return this.storagePort; - } - -/** -* get storage server HTTP port -* @return storage server HTTP port -*/ - public int getStorageHttpPort() - { - return this.storageHttpPort; + /** + * get free disk space in MB + * + * @return free disk space in MB + */ + public long getFreeMB() { + return this.freeMB; } -/** -* get current storage server index to upload file -* @return current storage server index to upload file -*/ - public int getCurrentWriteServer() - { - return this.currentWriteServer; + /** + * get trunk free space in MB + * + * @return trunk free space in MB + */ + public long getTrunkFreeMB() { + return this.trunkFreeMB; } - -/** -* get store base path count of each storage server -* @return store base path count of each storage server -*/ - public int getStorePathCount() - { - return this.storePathCount; - } - -/** -* get sub dir count per store path -* @return sub dir count per store path -*/ - public int getSubdirCountPerPath() - { - return this.subdirCountPerPath; - } - -/** -* get current trunk file id -* @return current trunk file id -*/ - public int getCurrentTrunkFileId() - { - return this.currentTrunkFileId; - } - -/** -* set fields -* @param bs byte array -* @param offset start offset -*/ - public void setFields(byte[] bs, int offset) - { - this.groupName = stringValue(bs, offset, fieldsArray[FIELD_INDEX_GROUP_NAME]); - this.totalMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MB]); - this.freeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_FREE_MB]); - this.trunkFreeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TRUNK_FREE_MB]); - this.storageCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_COUNT]); - this.storagePort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_PORT]); - this.storageHttpPort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT]); - this.activeCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_ACTIVE_COUNT]); - this.currentWriteServer = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_WRITE_SERVER]); - this.storePathCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORE_PATH_COUNT]); - this.subdirCountPerPath = intValue(bs, offset, fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH]); - this.currentTrunkFileId = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_TRUNK_FILE_ID]); - } -/** -* get fields total size -* @return fields total size -*/ - public static int getFieldsTotalSize() - { - return fieldsTotalSize; - } + /** + * get storage server count in this group + * + * @return storage server count in this group + */ + public int getStorageCount() { + return this.storageCount; + } + + /** + * get active storage server count in this group + * + * @return active storage server count in this group + */ + public int getActiveCount() { + return this.activeCount; + } + + /** + * get storage server port + * + * @return storage server port + */ + public int getStoragePort() { + return this.storagePort; + } + + /** + * get storage server HTTP port + * + * @return storage server HTTP port + */ + public int getStorageHttpPort() { + return this.storageHttpPort; + } + + /** + * get current storage server index to upload file + * + * @return current storage server index to upload file + */ + public int getCurrentWriteServer() { + return this.currentWriteServer; + } + + /** + * get store base path count of each storage server + * + * @return store base path count of each storage server + */ + public int getStorePathCount() { + return this.storePathCount; + } + + /** + * get sub dir count per store path + * + * @return sub dir count per store path + */ + public int getSubdirCountPerPath() { + return this.subdirCountPerPath; + } + + /** + * get current trunk file id + * + * @return current trunk file id + */ + public int getCurrentTrunkFileId() { + return this.currentTrunkFileId; + } + + /** + * set fields + * + * @param bs byte array + * @param offset start offset + */ + public void setFields(byte[] bs, int offset) { + this.groupName = stringValue(bs, offset, fieldsArray[FIELD_INDEX_GROUP_NAME]); + this.totalMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MB]); + this.freeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_FREE_MB]); + this.trunkFreeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TRUNK_FREE_MB]); + this.storageCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_COUNT]); + this.storagePort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_PORT]); + this.storageHttpPort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT]); + this.activeCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_ACTIVE_COUNT]); + this.currentWriteServer = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_WRITE_SERVER]); + this.storePathCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORE_PATH_COUNT]); + this.subdirCountPerPath = intValue(bs, offset, fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH]); + this.currentTrunkFileId = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_TRUNK_FILE_ID]); + } } diff --git a/src/main/java/org/csource/fastdfs/StructStorageStat.java b/src/main/java/org/csource/fastdfs/StructStorageStat.java index eef3e71..1b38448 100644 --- a/src/main/java/org/csource/fastdfs/StructStorageStat.java +++ b/src/main/java/org/csource/fastdfs/StructStorageStat.java @@ -1,983 +1,982 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; import java.util.Date; /** -* C struct body decoder -* @author Happy Fish / YuQing -* @version Version 1.25 -*/ -public class StructStorageStat extends StructBase -{ - protected static final int FIELD_INDEX_STATUS = 0; - protected static final int FIELD_INDEX_ID = 1; - protected static final int FIELD_INDEX_IP_ADDR = 2; - protected static final int FIELD_INDEX_DOMAIN_NAME = 3; - protected static final int FIELD_INDEX_SRC_IP_ADDR = 4; - protected static final int FIELD_INDEX_VERSION = 5; - protected static final int FIELD_INDEX_JOIN_TIME = 6; - protected static final int FIELD_INDEX_UP_TIME = 7; - protected static final int FIELD_INDEX_TOTAL_MB = 8; - protected static final int FIELD_INDEX_FREE_MB = 9; - protected static final int FIELD_INDEX_UPLOAD_PRIORITY = 10; - protected static final int FIELD_INDEX_STORE_PATH_COUNT = 11; - protected static final int FIELD_INDEX_SUBDIR_COUNT_PER_PATH = 12; - protected static final int FIELD_INDEX_CURRENT_WRITE_PATH = 13; - protected static final int FIELD_INDEX_STORAGE_PORT = 14; - protected static final int FIELD_INDEX_STORAGE_HTTP_PORT = 15; + * C struct body decoder + * + * @author Happy Fish / YuQing + * @version Version 1.25 + */ +public class StructStorageStat extends StructBase { + protected static final int FIELD_INDEX_STATUS = 0; + protected static final int FIELD_INDEX_ID = 1; + protected static final int FIELD_INDEX_IP_ADDR = 2; + protected static final int FIELD_INDEX_DOMAIN_NAME = 3; + protected static final int FIELD_INDEX_SRC_IP_ADDR = 4; + protected static final int FIELD_INDEX_VERSION = 5; + protected static final int FIELD_INDEX_JOIN_TIME = 6; + protected static final int FIELD_INDEX_UP_TIME = 7; + protected static final int FIELD_INDEX_TOTAL_MB = 8; + protected static final int FIELD_INDEX_FREE_MB = 9; + protected static final int FIELD_INDEX_UPLOAD_PRIORITY = 10; + protected static final int FIELD_INDEX_STORE_PATH_COUNT = 11; + protected static final int FIELD_INDEX_SUBDIR_COUNT_PER_PATH = 12; + protected static final int FIELD_INDEX_CURRENT_WRITE_PATH = 13; + protected static final int FIELD_INDEX_STORAGE_PORT = 14; + protected static final int FIELD_INDEX_STORAGE_HTTP_PORT = 15; - protected static final int FIELD_INDEX_CONNECTION_ALLOC_COUNT = 16; - protected static final int FIELD_INDEX_CONNECTION_CURRENT_COUNT = 17; - protected static final int FIELD_INDEX_CONNECTION_MAX_COUNT = 18; + protected static final int FIELD_INDEX_CONNECTION_ALLOC_COUNT = 16; + protected static final int FIELD_INDEX_CONNECTION_CURRENT_COUNT = 17; + protected static final int FIELD_INDEX_CONNECTION_MAX_COUNT = 18; - protected static final int FIELD_INDEX_TOTAL_UPLOAD_COUNT = 19; - protected static final int FIELD_INDEX_SUCCESS_UPLOAD_COUNT = 20; - protected static final int FIELD_INDEX_TOTAL_APPEND_COUNT = 21; - protected static final int FIELD_INDEX_SUCCESS_APPEND_COUNT = 22; - protected static final int FIELD_INDEX_TOTAL_MODIFY_COUNT = 23; - protected static final int FIELD_INDEX_SUCCESS_MODIFY_COUNT = 24; - protected static final int FIELD_INDEX_TOTAL_TRUNCATE_COUNT = 25; - protected static final int FIELD_INDEX_SUCCESS_TRUNCATE_COUNT = 26; - protected static final int FIELD_INDEX_TOTAL_SET_META_COUNT = 27; - protected static final int FIELD_INDEX_SUCCESS_SET_META_COUNT = 28; - protected static final int FIELD_INDEX_TOTAL_DELETE_COUNT = 29; - protected static final int FIELD_INDEX_SUCCESS_DELETE_COUNT = 30; - protected static final int FIELD_INDEX_TOTAL_DOWNLOAD_COUNT = 31; - protected static final int FIELD_INDEX_SUCCESS_DOWNLOAD_COUNT = 32; - protected static final int FIELD_INDEX_TOTAL_GET_META_COUNT = 33; - protected static final int FIELD_INDEX_SUCCESS_GET_META_COUNT = 34; - protected static final int FIELD_INDEX_TOTAL_CREATE_LINK_COUNT = 35; - protected static final int FIELD_INDEX_SUCCESS_CREATE_LINK_COUNT= 36; - protected static final int FIELD_INDEX_TOTAL_DELETE_LINK_COUNT = 37; - protected static final int FIELD_INDEX_SUCCESS_DELETE_LINK_COUNT= 38; - protected static final int FIELD_INDEX_TOTAL_UPLOAD_BYTES = 39; - protected static final int FIELD_INDEX_SUCCESS_UPLOAD_BYTES = 40; - protected static final int FIELD_INDEX_TOTAL_APPEND_BYTES = 41; - protected static final int FIELD_INDEX_SUCCESS_APPEND_BYTES = 42; - protected static final int FIELD_INDEX_TOTAL_MODIFY_BYTES = 43; - protected static final int FIELD_INDEX_SUCCESS_MODIFY_BYTES = 44; - protected static final int FIELD_INDEX_TOTAL_DOWNLOAD_BYTES = 45; - protected static final int FIELD_INDEX_SUCCESS_DOWNLOAD_BYTES = 46; - protected static final int FIELD_INDEX_TOTAL_SYNC_IN_BYTES = 47; - protected static final int FIELD_INDEX_SUCCESS_SYNC_IN_BYTES = 48; - protected static final int FIELD_INDEX_TOTAL_SYNC_OUT_BYTES = 49; - protected static final int FIELD_INDEX_SUCCESS_SYNC_OUT_BYTES = 50; - protected static final int FIELD_INDEX_TOTAL_FILE_OPEN_COUNT = 51; - protected static final int FIELD_INDEX_SUCCESS_FILE_OPEN_COUNT = 52; - protected static final int FIELD_INDEX_TOTAL_FILE_READ_COUNT = 53; - protected static final int FIELD_INDEX_SUCCESS_FILE_READ_COUNT = 54; - protected static final int FIELD_INDEX_TOTAL_FILE_WRITE_COUNT = 55; - protected static final int FIELD_INDEX_SUCCESS_FILE_WRITE_COUNT = 56; - protected static final int FIELD_INDEX_LAST_SOURCE_UPDATE = 57; - protected static final int FIELD_INDEX_LAST_SYNC_UPDATE = 58; - protected static final int FIELD_INDEX_LAST_SYNCED_TIMESTAMP = 59; - protected static final int FIELD_INDEX_LAST_HEART_BEAT_TIME = 60; - protected static final int FIELD_INDEX_IF_TRUNK_FILE = 61; - - protected static int fieldsTotalSize; - protected static StructBase.FieldInfo[] fieldsArray = new StructBase.FieldInfo[62]; - - static - { - int offset = 0; - - fieldsArray[FIELD_INDEX_STATUS] = new StructBase.FieldInfo("status", offset, 1); - offset += 1; - - fieldsArray[FIELD_INDEX_ID] = new StructBase.FieldInfo("id", offset, ProtoCommon.FDFS_STORAGE_ID_MAX_SIZE); - offset += ProtoCommon.FDFS_STORAGE_ID_MAX_SIZE; - - fieldsArray[FIELD_INDEX_IP_ADDR] = new StructBase.FieldInfo("ipAddr", offset, ProtoCommon.FDFS_IPADDR_SIZE); - offset += ProtoCommon.FDFS_IPADDR_SIZE; - - fieldsArray[FIELD_INDEX_DOMAIN_NAME] = new StructBase.FieldInfo("domainName", offset, ProtoCommon.FDFS_DOMAIN_NAME_MAX_SIZE); - offset += ProtoCommon.FDFS_DOMAIN_NAME_MAX_SIZE; + protected static final int FIELD_INDEX_TOTAL_UPLOAD_COUNT = 19; + protected static final int FIELD_INDEX_SUCCESS_UPLOAD_COUNT = 20; + protected static final int FIELD_INDEX_TOTAL_APPEND_COUNT = 21; + protected static final int FIELD_INDEX_SUCCESS_APPEND_COUNT = 22; + protected static final int FIELD_INDEX_TOTAL_MODIFY_COUNT = 23; + protected static final int FIELD_INDEX_SUCCESS_MODIFY_COUNT = 24; + protected static final int FIELD_INDEX_TOTAL_TRUNCATE_COUNT = 25; + protected static final int FIELD_INDEX_SUCCESS_TRUNCATE_COUNT = 26; + protected static final int FIELD_INDEX_TOTAL_SET_META_COUNT = 27; + protected static final int FIELD_INDEX_SUCCESS_SET_META_COUNT = 28; + protected static final int FIELD_INDEX_TOTAL_DELETE_COUNT = 29; + protected static final int FIELD_INDEX_SUCCESS_DELETE_COUNT = 30; + protected static final int FIELD_INDEX_TOTAL_DOWNLOAD_COUNT = 31; + protected static final int FIELD_INDEX_SUCCESS_DOWNLOAD_COUNT = 32; + protected static final int FIELD_INDEX_TOTAL_GET_META_COUNT = 33; + protected static final int FIELD_INDEX_SUCCESS_GET_META_COUNT = 34; + protected static final int FIELD_INDEX_TOTAL_CREATE_LINK_COUNT = 35; + protected static final int FIELD_INDEX_SUCCESS_CREATE_LINK_COUNT = 36; + protected static final int FIELD_INDEX_TOTAL_DELETE_LINK_COUNT = 37; + protected static final int FIELD_INDEX_SUCCESS_DELETE_LINK_COUNT = 38; + protected static final int FIELD_INDEX_TOTAL_UPLOAD_BYTES = 39; + protected static final int FIELD_INDEX_SUCCESS_UPLOAD_BYTES = 40; + protected static final int FIELD_INDEX_TOTAL_APPEND_BYTES = 41; + protected static final int FIELD_INDEX_SUCCESS_APPEND_BYTES = 42; + protected static final int FIELD_INDEX_TOTAL_MODIFY_BYTES = 43; + protected static final int FIELD_INDEX_SUCCESS_MODIFY_BYTES = 44; + protected static final int FIELD_INDEX_TOTAL_DOWNLOAD_BYTES = 45; + protected static final int FIELD_INDEX_SUCCESS_DOWNLOAD_BYTES = 46; + protected static final int FIELD_INDEX_TOTAL_SYNC_IN_BYTES = 47; + protected static final int FIELD_INDEX_SUCCESS_SYNC_IN_BYTES = 48; + protected static final int FIELD_INDEX_TOTAL_SYNC_OUT_BYTES = 49; + protected static final int FIELD_INDEX_SUCCESS_SYNC_OUT_BYTES = 50; + protected static final int FIELD_INDEX_TOTAL_FILE_OPEN_COUNT = 51; + protected static final int FIELD_INDEX_SUCCESS_FILE_OPEN_COUNT = 52; + protected static final int FIELD_INDEX_TOTAL_FILE_READ_COUNT = 53; + protected static final int FIELD_INDEX_SUCCESS_FILE_READ_COUNT = 54; + protected static final int FIELD_INDEX_TOTAL_FILE_WRITE_COUNT = 55; + protected static final int FIELD_INDEX_SUCCESS_FILE_WRITE_COUNT = 56; + protected static final int FIELD_INDEX_LAST_SOURCE_UPDATE = 57; + protected static final int FIELD_INDEX_LAST_SYNC_UPDATE = 58; + protected static final int FIELD_INDEX_LAST_SYNCED_TIMESTAMP = 59; + protected static final int FIELD_INDEX_LAST_HEART_BEAT_TIME = 60; + protected static final int FIELD_INDEX_IF_TRUNK_FILE = 61; - fieldsArray[FIELD_INDEX_SRC_IP_ADDR] = new StructBase.FieldInfo("srcIpAddr", offset, ProtoCommon.FDFS_IPADDR_SIZE); - offset += ProtoCommon.FDFS_IPADDR_SIZE; - - fieldsArray[FIELD_INDEX_VERSION] = new StructBase.FieldInfo("version", offset, ProtoCommon.FDFS_VERSION_SIZE); - offset += ProtoCommon.FDFS_VERSION_SIZE; - - fieldsArray[FIELD_INDEX_JOIN_TIME] = new StructBase.FieldInfo("joinTime", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_UP_TIME] = new StructBase.FieldInfo("upTime", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + protected static int fieldsTotalSize; + protected static StructBase.FieldInfo[] fieldsArray = new StructBase.FieldInfo[62]; - fieldsArray[FIELD_INDEX_TOTAL_MB] = new StructBase.FieldInfo("totalMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_FREE_MB] = new StructBase.FieldInfo("freeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + static { + int offset = 0; - fieldsArray[FIELD_INDEX_UPLOAD_PRIORITY] = new StructBase.FieldInfo("uploadPriority", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_STORE_PATH_COUNT] = new StructBase.FieldInfo("storePathCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH] = new StructBase.FieldInfo("subdirCountPerPath", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_STATUS] = new StructBase.FieldInfo("status", offset, 1); + offset += 1; - fieldsArray[FIELD_INDEX_CURRENT_WRITE_PATH] = new StructBase.FieldInfo("currentWritePath", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_ID] = new StructBase.FieldInfo("id", offset, ProtoCommon.FDFS_STORAGE_ID_MAX_SIZE); + offset += ProtoCommon.FDFS_STORAGE_ID_MAX_SIZE; - fieldsArray[FIELD_INDEX_STORAGE_PORT] = new StructBase.FieldInfo("storagePort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_IP_ADDR] = new StructBase.FieldInfo("ipAddr", offset, ProtoCommon.FDFS_IPADDR_SIZE); + offset += ProtoCommon.FDFS_IPADDR_SIZE; - fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT] = new StructBase.FieldInfo("storageHttpPort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_DOMAIN_NAME] = new StructBase.FieldInfo("domainName", offset, ProtoCommon.FDFS_DOMAIN_NAME_MAX_SIZE); + offset += ProtoCommon.FDFS_DOMAIN_NAME_MAX_SIZE; - fieldsArray[FIELD_INDEX_CONNECTION_ALLOC_COUNT] = new StructBase.FieldInfo("connectionAllocCount", offset, 4); - offset += 4; + fieldsArray[FIELD_INDEX_SRC_IP_ADDR] = new StructBase.FieldInfo("srcIpAddr", offset, ProtoCommon.FDFS_IPADDR_SIZE); + offset += ProtoCommon.FDFS_IPADDR_SIZE; - fieldsArray[FIELD_INDEX_CONNECTION_CURRENT_COUNT] = new StructBase.FieldInfo("connectionCurrentCount", offset, 4); - offset += 4; + fieldsArray[FIELD_INDEX_VERSION] = new StructBase.FieldInfo("version", offset, ProtoCommon.FDFS_VERSION_SIZE); + offset += ProtoCommon.FDFS_VERSION_SIZE; - fieldsArray[FIELD_INDEX_CONNECTION_MAX_COUNT] = new StructBase.FieldInfo("connectionMaxCount", offset, 4); - offset += 4; + fieldsArray[FIELD_INDEX_JOIN_TIME] = new StructBase.FieldInfo("joinTime", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - fieldsArray[FIELD_INDEX_TOTAL_UPLOAD_COUNT] = new StructBase.FieldInfo("totalUploadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_UPLOAD_COUNT] = new StructBase.FieldInfo("successUploadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_APPEND_COUNT] = new StructBase.FieldInfo("totalAppendCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_APPEND_COUNT] = new StructBase.FieldInfo("successAppendCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_UP_TIME] = new StructBase.FieldInfo("upTime", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - fieldsArray[FIELD_INDEX_TOTAL_MODIFY_COUNT] = new StructBase.FieldInfo("totalModifyCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_MODIFY_COUNT] = new StructBase.FieldInfo("successModifyCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_TOTAL_MB] = new StructBase.FieldInfo("totalMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - fieldsArray[FIELD_INDEX_TOTAL_TRUNCATE_COUNT] = new StructBase.FieldInfo("totalTruncateCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_TRUNCATE_COUNT] = new StructBase.FieldInfo("successTruncateCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_SET_META_COUNT] = new StructBase.FieldInfo("totalSetMetaCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_SET_META_COUNT] = new StructBase.FieldInfo("successSetMetaCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_DELETE_COUNT] = new StructBase.FieldInfo("totalDeleteCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_DELETE_COUNT] = new StructBase.FieldInfo("successDeleteCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_DOWNLOAD_COUNT] = new StructBase.FieldInfo("totalDownloadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_DOWNLOAD_COUNT] = new StructBase.FieldInfo("successDownloadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_GET_META_COUNT] = new StructBase.FieldInfo("totalGetMetaCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_GET_META_COUNT] = new StructBase.FieldInfo("successGetMetaCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_CREATE_LINK_COUNT] = new StructBase.FieldInfo("totalCreateLinkCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_CREATE_LINK_COUNT] = new StructBase.FieldInfo("successCreateLinkCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_DELETE_LINK_COUNT] = new StructBase.FieldInfo("totalDeleteLinkCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_DELETE_LINK_COUNT] = new StructBase.FieldInfo("successDeleteLinkCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_UPLOAD_BYTES] = new StructBase.FieldInfo("totalUploadBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_UPLOAD_BYTES] = new StructBase.FieldInfo("successUploadBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_APPEND_BYTES] = new StructBase.FieldInfo("totalAppendBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_APPEND_BYTES] = new StructBase.FieldInfo("successAppendBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_MODIFY_BYTES] = new StructBase.FieldInfo("totalModifyBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_MODIFY_BYTES] = new StructBase.FieldInfo("successModifyBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_DOWNLOAD_BYTES] = new StructBase.FieldInfo("totalDownloadloadBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_DOWNLOAD_BYTES] = new StructBase.FieldInfo("successDownloadloadBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_FREE_MB] = new StructBase.FieldInfo("freeMB", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - fieldsArray[FIELD_INDEX_TOTAL_SYNC_IN_BYTES] = new StructBase.FieldInfo("totalSyncInBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_SYNC_IN_BYTES] = new StructBase.FieldInfo("successSyncInBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_UPLOAD_PRIORITY] = new StructBase.FieldInfo("uploadPriority", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - fieldsArray[FIELD_INDEX_TOTAL_SYNC_OUT_BYTES] = new StructBase.FieldInfo("totalSyncOutBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_SYNC_OUT_BYTES] = new StructBase.FieldInfo("successSyncOutBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_FILE_OPEN_COUNT] = new StructBase.FieldInfo("totalFileOpenCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_FILE_OPEN_COUNT] = new StructBase.FieldInfo("successFileOpenCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + fieldsArray[FIELD_INDEX_STORE_PATH_COUNT] = new StructBase.FieldInfo("storePathCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - fieldsArray[FIELD_INDEX_TOTAL_FILE_READ_COUNT] = new StructBase.FieldInfo("totalFileReadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_FILE_READ_COUNT] = new StructBase.FieldInfo("successFileReadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_TOTAL_FILE_WRITE_COUNT] = new StructBase.FieldInfo("totalFileWriteCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_SUCCESS_FILE_WRITE_COUNT] = new StructBase.FieldInfo("successFileWriteCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_LAST_SOURCE_UPDATE] = new StructBase.FieldInfo("lastSourceUpdate", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_LAST_SYNC_UPDATE] = new StructBase.FieldInfo("lastSyncUpdate", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_LAST_SYNCED_TIMESTAMP] = new StructBase.FieldInfo("lastSyncedTimestamp", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_LAST_HEART_BEAT_TIME] = new StructBase.FieldInfo("lastHeartBeatTime", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); - offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - fieldsArray[FIELD_INDEX_IF_TRUNK_FILE] = new StructBase.FieldInfo("ifTrunkServer", offset, 1); - offset += 1; - - fieldsTotalSize = offset; - } - - protected byte status; - protected String id; - protected String ipAddr; - protected String srcIpAddr; - protected String domainName; //http domain name - protected String version; - protected long totalMB; //total disk storage in MB - protected long freeMB; //free disk storage in MB - protected int uploadPriority; //upload priority - protected Date joinTime; //storage join timestamp (create timestamp) - protected Date upTime; //storage service started timestamp - protected int storePathCount; //store base path count of each storage server - protected int subdirCountPerPath; - protected int storagePort; - protected int storageHttpPort; //storage http server port - protected int currentWritePath; //current write path index - protected int connectionAllocCount; - protected int connectionCurrentCount; - protected int connectionMaxCount; - protected long totalUploadCount; - protected long successUploadCount; - protected long totalAppendCount; - protected long successAppendCount; - protected long totalModifyCount; - protected long successModifyCount; - protected long totalTruncateCount; - protected long successTruncateCount; - protected long totalSetMetaCount; - protected long successSetMetaCount; - protected long totalDeleteCount; - protected long successDeleteCount; - protected long totalDownloadCount; - protected long successDownloadCount; - protected long totalGetMetaCount; - protected long successGetMetaCount; - protected long totalCreateLinkCount; - protected long successCreateLinkCount; - protected long totalDeleteLinkCount; - protected long successDeleteLinkCount; - protected long totalUploadBytes; - protected long successUploadBytes; - protected long totalAppendBytes; - protected long successAppendBytes; - protected long totalModifyBytes; - protected long successModifyBytes; - protected long totalDownloadloadBytes; - protected long successDownloadloadBytes; - protected long totalSyncInBytes; - protected long successSyncInBytes; - protected long totalSyncOutBytes; - protected long successSyncOutBytes; - protected long totalFileOpenCount; - protected long successFileOpenCount; - protected long totalFileReadCount; - protected long successFileReadCount; - protected long totalFileWriteCount; - protected long successFileWriteCount; - protected Date lastSourceUpdate; - protected Date lastSyncUpdate; - protected Date lastSyncedTimestamp; - protected Date lastHeartBeatTime; - protected boolean ifTrunkServer; - -/** -* get storage status -* @return storage status -*/ - public byte getStatus() - { - return this.status; - } - -/** -* get storage server id -* @return storage server id -*/ - public String getId() - { - return this.id; + fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH] = new StructBase.FieldInfo("subdirCountPerPath", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_CURRENT_WRITE_PATH] = new StructBase.FieldInfo("currentWritePath", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_STORAGE_PORT] = new StructBase.FieldInfo("storagePort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT] = new StructBase.FieldInfo("storageHttpPort", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_CONNECTION_ALLOC_COUNT] = new StructBase.FieldInfo("connectionAllocCount", offset, 4); + offset += 4; + + fieldsArray[FIELD_INDEX_CONNECTION_CURRENT_COUNT] = new StructBase.FieldInfo("connectionCurrentCount", offset, 4); + offset += 4; + + fieldsArray[FIELD_INDEX_CONNECTION_MAX_COUNT] = new StructBase.FieldInfo("connectionMaxCount", offset, 4); + offset += 4; + + fieldsArray[FIELD_INDEX_TOTAL_UPLOAD_COUNT] = new StructBase.FieldInfo("totalUploadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_UPLOAD_COUNT] = new StructBase.FieldInfo("successUploadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_APPEND_COUNT] = new StructBase.FieldInfo("totalAppendCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_APPEND_COUNT] = new StructBase.FieldInfo("successAppendCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_MODIFY_COUNT] = new StructBase.FieldInfo("totalModifyCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_MODIFY_COUNT] = new StructBase.FieldInfo("successModifyCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_TRUNCATE_COUNT] = new StructBase.FieldInfo("totalTruncateCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_TRUNCATE_COUNT] = new StructBase.FieldInfo("successTruncateCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_SET_META_COUNT] = new StructBase.FieldInfo("totalSetMetaCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_SET_META_COUNT] = new StructBase.FieldInfo("successSetMetaCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_DELETE_COUNT] = new StructBase.FieldInfo("totalDeleteCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_DELETE_COUNT] = new StructBase.FieldInfo("successDeleteCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_DOWNLOAD_COUNT] = new StructBase.FieldInfo("totalDownloadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_DOWNLOAD_COUNT] = new StructBase.FieldInfo("successDownloadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_GET_META_COUNT] = new StructBase.FieldInfo("totalGetMetaCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_GET_META_COUNT] = new StructBase.FieldInfo("successGetMetaCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_CREATE_LINK_COUNT] = new StructBase.FieldInfo("totalCreateLinkCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_CREATE_LINK_COUNT] = new StructBase.FieldInfo("successCreateLinkCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_DELETE_LINK_COUNT] = new StructBase.FieldInfo("totalDeleteLinkCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_DELETE_LINK_COUNT] = new StructBase.FieldInfo("successDeleteLinkCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_UPLOAD_BYTES] = new StructBase.FieldInfo("totalUploadBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_UPLOAD_BYTES] = new StructBase.FieldInfo("successUploadBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_APPEND_BYTES] = new StructBase.FieldInfo("totalAppendBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_APPEND_BYTES] = new StructBase.FieldInfo("successAppendBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_MODIFY_BYTES] = new StructBase.FieldInfo("totalModifyBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_MODIFY_BYTES] = new StructBase.FieldInfo("successModifyBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_DOWNLOAD_BYTES] = new StructBase.FieldInfo("totalDownloadloadBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_DOWNLOAD_BYTES] = new StructBase.FieldInfo("successDownloadloadBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_SYNC_IN_BYTES] = new StructBase.FieldInfo("totalSyncInBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_SYNC_IN_BYTES] = new StructBase.FieldInfo("successSyncInBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_SYNC_OUT_BYTES] = new StructBase.FieldInfo("totalSyncOutBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_SYNC_OUT_BYTES] = new StructBase.FieldInfo("successSyncOutBytes", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_FILE_OPEN_COUNT] = new StructBase.FieldInfo("totalFileOpenCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_FILE_OPEN_COUNT] = new StructBase.FieldInfo("successFileOpenCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_FILE_READ_COUNT] = new StructBase.FieldInfo("totalFileReadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_FILE_READ_COUNT] = new StructBase.FieldInfo("successFileReadCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_TOTAL_FILE_WRITE_COUNT] = new StructBase.FieldInfo("totalFileWriteCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_SUCCESS_FILE_WRITE_COUNT] = new StructBase.FieldInfo("successFileWriteCount", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_LAST_SOURCE_UPDATE] = new StructBase.FieldInfo("lastSourceUpdate", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_LAST_SYNC_UPDATE] = new StructBase.FieldInfo("lastSyncUpdate", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_LAST_SYNCED_TIMESTAMP] = new StructBase.FieldInfo("lastSyncedTimestamp", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_LAST_HEART_BEAT_TIME] = new StructBase.FieldInfo("lastHeartBeatTime", offset, ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + fieldsArray[FIELD_INDEX_IF_TRUNK_FILE] = new StructBase.FieldInfo("ifTrunkServer", offset, 1); + offset += 1; + + fieldsTotalSize = offset; } -/** -* get storage server ip address -* @return storage server ip address -*/ - public String getIpAddr() - { - return this.ipAddr; - } - -/** -* get source storage ip address -* @return source storage ip address -*/ - public String getSrcIpAddr() - { - return this.srcIpAddr; - } - -/** -* get the domain name of the storage server -* @return the domain name of the storage server -*/ - public String getDomainName() - { - return this.domainName; + protected byte status; + protected String id; + protected String ipAddr; + protected String srcIpAddr; + protected String domainName; //http domain name + protected String version; + protected long totalMB; //total disk storage in MB + protected long freeMB; //free disk storage in MB + protected int uploadPriority; //upload priority + protected Date joinTime; //storage join timestamp (create timestamp) + protected Date upTime; //storage service started timestamp + protected int storePathCount; //store base path count of each storage server + protected int subdirCountPerPath; + protected int storagePort; + protected int storageHttpPort; //storage http server port + protected int currentWritePath; //current write path index + protected int connectionAllocCount; + protected int connectionCurrentCount; + protected int connectionMaxCount; + protected long totalUploadCount; + protected long successUploadCount; + protected long totalAppendCount; + protected long successAppendCount; + protected long totalModifyCount; + protected long successModifyCount; + protected long totalTruncateCount; + protected long successTruncateCount; + protected long totalSetMetaCount; + protected long successSetMetaCount; + protected long totalDeleteCount; + protected long successDeleteCount; + protected long totalDownloadCount; + protected long successDownloadCount; + protected long totalGetMetaCount; + protected long successGetMetaCount; + protected long totalCreateLinkCount; + protected long successCreateLinkCount; + protected long totalDeleteLinkCount; + protected long successDeleteLinkCount; + protected long totalUploadBytes; + protected long successUploadBytes; + protected long totalAppendBytes; + protected long successAppendBytes; + protected long totalModifyBytes; + protected long successModifyBytes; + protected long totalDownloadloadBytes; + protected long successDownloadloadBytes; + protected long totalSyncInBytes; + protected long successSyncInBytes; + protected long totalSyncOutBytes; + protected long successSyncOutBytes; + protected long totalFileOpenCount; + protected long successFileOpenCount; + protected long totalFileReadCount; + protected long successFileReadCount; + protected long totalFileWriteCount; + protected long successFileWriteCount; + protected Date lastSourceUpdate; + protected Date lastSyncUpdate; + protected Date lastSyncedTimestamp; + protected Date lastHeartBeatTime; + protected boolean ifTrunkServer; + + /** + * get fields total size + * + * @return fields total size + */ + public static int getFieldsTotalSize() { + return fieldsTotalSize; } -/** -* get storage version -* @return storage version -*/ - public String getVersion() - { - return this.version; - } - -/** -* get total disk space in MB -* @return total disk space in MB -*/ - public long getTotalMB() - { - return this.totalMB; - } - -/** -* get free disk space in MB -* @return free disk space in MB -*/ - public long getFreeMB() - { - return this.freeMB; - } - -/** -* get storage server upload priority -* @return storage server upload priority -*/ - public int getUploadPriority() - { - return this.uploadPriority; - } - -/** -* get storage server join time -* @return storage server join time -*/ - public Date getJoinTime() - { - return this.joinTime; - } - -/** -* get storage server up time -* @return storage server up time -*/ - public Date getUpTime() - { - return this.upTime; + /** + * get storage status + * + * @return storage status + */ + public byte getStatus() { + return this.status; } -/** -* get store base path count of each storage server -* @return store base path count of each storage server -*/ - public int getStorePathCount() - { - return this.storePathCount; - } - -/** -* get sub dir count per store path -* @return sub dir count per store path -*/ - public int getSubdirCountPerPath() - { - return this.subdirCountPerPath; - } - -/** -* get storage server port -* @return storage server port -*/ - public int getStoragePort() - { - return this.storagePort; - } - -/** -* get storage server HTTP port -* @return storage server HTTP port -*/ - public int getStorageHttpPort() - { - return this.storageHttpPort; + /** + * get storage server id + * + * @return storage server id + */ + public String getId() { + return this.id; } -/** -* get current write path index -* @return current write path index -*/ - public int getCurrentWritePath() - { - return this.currentWritePath; + /** + * get storage server ip address + * + * @return storage server ip address + */ + public String getIpAddr() { + return this.ipAddr; } -/** -* get total upload file count -* @return total upload file count -*/ - public long getTotalUploadCount() - { - return this.totalUploadCount; + /** + * get source storage ip address + * + * @return source storage ip address + */ + public String getSrcIpAddr() { + return this.srcIpAddr; } -/** -* get success upload file count -* @return success upload file count -*/ - public long getSuccessUploadCount() - { - return this.successUploadCount; + /** + * get the domain name of the storage server + * + * @return the domain name of the storage server + */ + public String getDomainName() { + return this.domainName; } -/** -* get total append count -* @return total append count -*/ - public long getTotalAppendCount() - { - return this.totalAppendCount; + /** + * get storage version + * + * @return storage version + */ + public String getVersion() { + return this.version; } -/** -* get success append count -* @return success append count -*/ - public long getSuccessAppendCount() - { - return this.successAppendCount; - } - -/** -* get total modify count -* @return total modify count -*/ - public long getTotalModifyCount() - { - return this.totalModifyCount; + /** + * get total disk space in MB + * + * @return total disk space in MB + */ + public long getTotalMB() { + return this.totalMB; } -/** -* get success modify count -* @return success modify count -*/ - public long getSuccessModifyCount() - { - return this.successModifyCount; - } - -/** -* get total truncate count -* @return total truncate count -*/ - public long getTotalTruncateCount() - { - return this.totalTruncateCount; + /** + * get free disk space in MB + * + * @return free disk space in MB + */ + public long getFreeMB() { + return this.freeMB; } -/** -* get success truncate count -* @return success truncate count -*/ - public long getSuccessTruncateCount() - { - return this.successTruncateCount; - } - -/** -* get total set meta data count -* @return total set meta data count -*/ - public long getTotalSetMetaCount() - { - return this.totalSetMetaCount; - } - -/** -* get success set meta data count -* @return success set meta data count -*/ - public long getSuccessSetMetaCount() - { - return this.successSetMetaCount; - } - -/** -* get total delete file count -* @return total delete file count -*/ - public long getTotalDeleteCount() - { - return this.totalDeleteCount; + /** + * get storage server upload priority + * + * @return storage server upload priority + */ + public int getUploadPriority() { + return this.uploadPriority; } -/** -* get success delete file count -* @return success delete file count -*/ - public long getSuccessDeleteCount() - { - return this.successDeleteCount; - } - -/** -* get total download file count -* @return total download file count -*/ - public long getTotalDownloadCount() - { - return this.totalDownloadCount; + /** + * get storage server join time + * + * @return storage server join time + */ + public Date getJoinTime() { + return this.joinTime; } -/** -* get success download file count -* @return success download file count -*/ - public long getSuccessDownloadCount() - { - return this.successDownloadCount; + /** + * get storage server up time + * + * @return storage server up time + */ + public Date getUpTime() { + return this.upTime; } -/** -* get total get metadata count -* @return total get metadata count -*/ - public long getTotalGetMetaCount() - { - return this.totalGetMetaCount; + /** + * get store base path count of each storage server + * + * @return store base path count of each storage server + */ + public int getStorePathCount() { + return this.storePathCount; } -/** -* get success get metadata count -* @return success get metadata count -*/ - public long getSuccessGetMetaCount() - { - return this.successGetMetaCount; + /** + * get sub dir count per store path + * + * @return sub dir count per store path + */ + public int getSubdirCountPerPath() { + return this.subdirCountPerPath; } -/** -* get total create linke count -* @return total create linke count -*/ - public long getTotalCreateLinkCount() - { - return this.totalCreateLinkCount; + /** + * get storage server port + * + * @return storage server port + */ + public int getStoragePort() { + return this.storagePort; } -/** -* get success create linke count -* @return success create linke count -*/ - public long getSuccessCreateLinkCount() - { - return this.successCreateLinkCount; - } - -/** -* get total delete link count -* @return total delete link count -*/ - public long getTotalDeleteLinkCount() - { - return this.totalDeleteLinkCount; - } - -/** -* get success delete link count -* @return success delete link count -*/ - public long getSuccessDeleteLinkCount() - { - return this.successDeleteLinkCount; + /** + * get storage server HTTP port + * + * @return storage server HTTP port + */ + public int getStorageHttpPort() { + return this.storageHttpPort; } -/** -* get total upload file bytes -* @return total upload file bytes -*/ - public long getTotalUploadBytes() - { - return this.totalUploadBytes; - } - -/** -* get success upload file bytes -* @return success upload file bytes -*/ - public long getSuccessUploadBytes() - { - return this.successUploadBytes; + /** + * get current write path index + * + * @return current write path index + */ + public int getCurrentWritePath() { + return this.currentWritePath; } -/** -* get total append bytes -* @return total append bytes -*/ - public long getTotalAppendBytes() - { - return this.totalAppendBytes; - } - -/** -* get success append bytes -* @return success append bytes -*/ - public long getSuccessAppendBytes() - { - return this.successAppendBytes; + /** + * get total upload file count + * + * @return total upload file count + */ + public long getTotalUploadCount() { + return this.totalUploadCount; } -/** -* get total modify bytes -* @return total modify bytes -*/ - public long getTotalModifyBytes() - { - return this.totalModifyBytes; - } - -/** -* get success modify bytes -* @return success modify bytes -*/ - public long getSuccessModifyBytes() - { - return this.successModifyBytes; - } - -/** -* get total download file bytes -* @return total download file bytes -*/ - public long getTotalDownloadloadBytes() - { - return this.totalDownloadloadBytes; - } - -/** -* get success download file bytes -* @return success download file bytes -*/ - public long getSuccessDownloadloadBytes() - { - return this.successDownloadloadBytes; + /** + * get success upload file count + * + * @return success upload file count + */ + public long getSuccessUploadCount() { + return this.successUploadCount; } -/** -* get total sync in bytes -* @return total sync in bytes -*/ - public long getTotalSyncInBytes() - { - return this.totalSyncInBytes; - } - -/** -* get success sync in bytes -* @return success sync in bytes -*/ - public long getSuccessSyncInBytes() - { - return this.successSyncInBytes; + /** + * get total append count + * + * @return total append count + */ + public long getTotalAppendCount() { + return this.totalAppendCount; } -/** -* get total sync out bytes -* @return total sync out bytes -*/ - public long getTotalSyncOutBytes() - { - return this.totalSyncOutBytes; - } - -/** -* get success sync out bytes -* @return success sync out bytes -*/ - public long getSuccessSyncOutBytes() - { - return this.successSyncOutBytes; + /** + * get success append count + * + * @return success append count + */ + public long getSuccessAppendCount() { + return this.successAppendCount; } -/** -* get total file opened count -* @return total file opened bytes -*/ - public long getTotalFileOpenCount() - { - return this.totalFileOpenCount; - } - -/** -* get success file opened count -* @return success file opened count -*/ - public long getSuccessFileOpenCount() - { - return this.successFileOpenCount; + /** + * get total modify count + * + * @return total modify count + */ + public long getTotalModifyCount() { + return this.totalModifyCount; } -/** -* get total file read count -* @return total file read bytes -*/ - public long getTotalFileReadCount() - { - return this.totalFileReadCount; - } - -/** -* get success file read count -* @return success file read count -*/ - public long getSuccessFileReadCount() - { - return this.successFileReadCount; + /** + * get success modify count + * + * @return success modify count + */ + public long getSuccessModifyCount() { + return this.successModifyCount; } -/** -* get total file write count -* @return total file write bytes -*/ - public long getTotalFileWriteCount() - { - return this.totalFileWriteCount; - } - -/** -* get success file write count -* @return success file write count -*/ - public long getSuccessFileWriteCount() - { - return this.successFileWriteCount; - } - -/** -* get last source update timestamp -* @return last source update timestamp -*/ - public Date getLastSourceUpdate() - { - return this.lastSourceUpdate; + /** + * get total truncate count + * + * @return total truncate count + */ + public long getTotalTruncateCount() { + return this.totalTruncateCount; } -/** -* get last synced update timestamp -* @return last synced update timestamp -*/ - public Date getLastSyncUpdate() - { - return this.lastSyncUpdate; - } - -/** -* get last synced timestamp -* @return last synced timestamp -*/ - public Date getLastSyncedTimestamp() - { - return this.lastSyncedTimestamp; + /** + * get success truncate count + * + * @return success truncate count + */ + public long getSuccessTruncateCount() { + return this.successTruncateCount; } -/** -* get last heart beat timestamp -* @return last heart beat timestamp -*/ - public Date getLastHeartBeatTime() - { - return this.lastHeartBeatTime; + /** + * get total set meta data count + * + * @return total set meta data count + */ + public long getTotalSetMetaCount() { + return this.totalSetMetaCount; } -/** -* if the trunk server -* @return true for the trunk server, otherwise false -*/ - public boolean isTrunkServer() - { - return this.ifTrunkServer; - } - -/** -* get connection alloc count -* @return connection alloc count -*/ - public int getConnectionAllocCount() - { - return this.connectionAllocCount; + /** + * get success set meta data count + * + * @return success set meta data count + */ + public long getSuccessSetMetaCount() { + return this.successSetMetaCount; } -/** -* get connection current count -* @return connection current count -*/ - public int getConnectionCurrentCount() - { - return this.connectionCurrentCount; + /** + * get total delete file count + * + * @return total delete file count + */ + public long getTotalDeleteCount() { + return this.totalDeleteCount; } -/** -* get connection max count -* @return connection max count -*/ - public int getConnectionMaxCount() - { - return this.connectionMaxCount; + /** + * get success delete file count + * + * @return success delete file count + */ + public long getSuccessDeleteCount() { + return this.successDeleteCount; } -/** -* set fields -* @param bs byte array -* @param offset start offset -*/ - public void setFields(byte[] bs, int offset) - { - this.status = byteValue(bs, offset, fieldsArray[FIELD_INDEX_STATUS]); - this.id = stringValue(bs, offset, fieldsArray[FIELD_INDEX_ID]); - this.ipAddr = stringValue(bs, offset, fieldsArray[FIELD_INDEX_IP_ADDR]); - this.srcIpAddr = stringValue(bs, offset, fieldsArray[FIELD_INDEX_SRC_IP_ADDR]); - this.domainName = stringValue(bs, offset, fieldsArray[FIELD_INDEX_DOMAIN_NAME]); - this.version = stringValue(bs, offset, fieldsArray[FIELD_INDEX_VERSION]); - this.totalMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MB]); - this.freeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_FREE_MB]); - this.uploadPriority = intValue(bs, offset, fieldsArray[FIELD_INDEX_UPLOAD_PRIORITY]); - this.joinTime = dateValue(bs, offset, fieldsArray[FIELD_INDEX_JOIN_TIME]); - this.upTime = dateValue(bs, offset, fieldsArray[FIELD_INDEX_UP_TIME]); - this.storePathCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORE_PATH_COUNT]); - this.subdirCountPerPath = intValue(bs, offset, fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH]); - this.storagePort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_PORT]); - this.storageHttpPort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT]); - this.currentWritePath = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_WRITE_PATH]); + /** + * get total download file count + * + * @return total download file count + */ + public long getTotalDownloadCount() { + return this.totalDownloadCount; + } - this.connectionAllocCount = int32Value(bs, offset, fieldsArray[FIELD_INDEX_CONNECTION_ALLOC_COUNT]); - this.connectionCurrentCount = int32Value(bs, offset, fieldsArray[FIELD_INDEX_CONNECTION_CURRENT_COUNT]); - this.connectionMaxCount = int32Value(bs, offset, fieldsArray[FIELD_INDEX_CONNECTION_MAX_COUNT]); + /** + * get success download file count + * + * @return success download file count + */ + public long getSuccessDownloadCount() { + return this.successDownloadCount; + } - this.totalUploadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_UPLOAD_COUNT]); - this.successUploadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_UPLOAD_COUNT]); - this.totalAppendCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_APPEND_COUNT]); - this.successAppendCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_APPEND_COUNT]); - this.totalModifyCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MODIFY_COUNT]); - this.successModifyCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_MODIFY_COUNT]); - this.totalTruncateCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_TRUNCATE_COUNT]); - this.successTruncateCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_TRUNCATE_COUNT]); - this.totalSetMetaCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_SET_META_COUNT]); - this.successSetMetaCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_SET_META_COUNT]); - this.totalDeleteCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_DELETE_COUNT]); - this.successDeleteCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_DELETE_COUNT]); - this.totalDownloadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_DOWNLOAD_COUNT]); - this.successDownloadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_DOWNLOAD_COUNT]); - this.totalGetMetaCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_GET_META_COUNT]); - this.successGetMetaCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_GET_META_COUNT]); - this.totalCreateLinkCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_CREATE_LINK_COUNT]); - this.successCreateLinkCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_CREATE_LINK_COUNT]); - this.totalDeleteLinkCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_DELETE_LINK_COUNT]); - this.successDeleteLinkCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_DELETE_LINK_COUNT]); - this.totalUploadBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_UPLOAD_BYTES]); - this.successUploadBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_UPLOAD_BYTES]); - this.totalAppendBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_APPEND_BYTES]); - this.successAppendBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_APPEND_BYTES]); - this.totalModifyBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MODIFY_BYTES]); - this.successModifyBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_MODIFY_BYTES]); - this.totalDownloadloadBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_DOWNLOAD_BYTES]); - this.successDownloadloadBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_DOWNLOAD_BYTES]); - this.totalSyncInBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_SYNC_IN_BYTES]); - this.successSyncInBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_SYNC_IN_BYTES]); - this.totalSyncOutBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_SYNC_OUT_BYTES]); - this.successSyncOutBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_SYNC_OUT_BYTES]); - this.totalFileOpenCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_FILE_OPEN_COUNT]); - this.successFileOpenCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_FILE_OPEN_COUNT]); - this.totalFileReadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_FILE_READ_COUNT]); - this.successFileReadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_FILE_READ_COUNT]); - this.totalFileWriteCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_FILE_WRITE_COUNT]); - this.successFileWriteCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_FILE_WRITE_COUNT]); - this.lastSourceUpdate = dateValue(bs, offset, fieldsArray[FIELD_INDEX_LAST_SOURCE_UPDATE]); - this.lastSyncUpdate = dateValue(bs, offset, fieldsArray[FIELD_INDEX_LAST_SYNC_UPDATE]); - this.lastSyncedTimestamp = dateValue(bs, offset, fieldsArray[FIELD_INDEX_LAST_SYNCED_TIMESTAMP]); - this.lastHeartBeatTime = dateValue(bs, offset, fieldsArray[FIELD_INDEX_LAST_HEART_BEAT_TIME]); - this.ifTrunkServer = booleanValue(bs, offset, fieldsArray[FIELD_INDEX_IF_TRUNK_FILE]); - } + /** + * get total get metadata count + * + * @return total get metadata count + */ + public long getTotalGetMetaCount() { + return this.totalGetMetaCount; + } -/** -* get fields total size -* @return fields total size -*/ - public static int getFieldsTotalSize() - { - return fieldsTotalSize; - } + /** + * get success get metadata count + * + * @return success get metadata count + */ + public long getSuccessGetMetaCount() { + return this.successGetMetaCount; + } + + /** + * get total create linke count + * + * @return total create linke count + */ + public long getTotalCreateLinkCount() { + return this.totalCreateLinkCount; + } + + /** + * get success create linke count + * + * @return success create linke count + */ + public long getSuccessCreateLinkCount() { + return this.successCreateLinkCount; + } + + /** + * get total delete link count + * + * @return total delete link count + */ + public long getTotalDeleteLinkCount() { + return this.totalDeleteLinkCount; + } + + /** + * get success delete link count + * + * @return success delete link count + */ + public long getSuccessDeleteLinkCount() { + return this.successDeleteLinkCount; + } + + /** + * get total upload file bytes + * + * @return total upload file bytes + */ + public long getTotalUploadBytes() { + return this.totalUploadBytes; + } + + /** + * get success upload file bytes + * + * @return success upload file bytes + */ + public long getSuccessUploadBytes() { + return this.successUploadBytes; + } + + /** + * get total append bytes + * + * @return total append bytes + */ + public long getTotalAppendBytes() { + return this.totalAppendBytes; + } + + /** + * get success append bytes + * + * @return success append bytes + */ + public long getSuccessAppendBytes() { + return this.successAppendBytes; + } + + /** + * get total modify bytes + * + * @return total modify bytes + */ + public long getTotalModifyBytes() { + return this.totalModifyBytes; + } + + /** + * get success modify bytes + * + * @return success modify bytes + */ + public long getSuccessModifyBytes() { + return this.successModifyBytes; + } + + /** + * get total download file bytes + * + * @return total download file bytes + */ + public long getTotalDownloadloadBytes() { + return this.totalDownloadloadBytes; + } + + /** + * get success download file bytes + * + * @return success download file bytes + */ + public long getSuccessDownloadloadBytes() { + return this.successDownloadloadBytes; + } + + /** + * get total sync in bytes + * + * @return total sync in bytes + */ + public long getTotalSyncInBytes() { + return this.totalSyncInBytes; + } + + /** + * get success sync in bytes + * + * @return success sync in bytes + */ + public long getSuccessSyncInBytes() { + return this.successSyncInBytes; + } + + /** + * get total sync out bytes + * + * @return total sync out bytes + */ + public long getTotalSyncOutBytes() { + return this.totalSyncOutBytes; + } + + /** + * get success sync out bytes + * + * @return success sync out bytes + */ + public long getSuccessSyncOutBytes() { + return this.successSyncOutBytes; + } + + /** + * get total file opened count + * + * @return total file opened bytes + */ + public long getTotalFileOpenCount() { + return this.totalFileOpenCount; + } + + /** + * get success file opened count + * + * @return success file opened count + */ + public long getSuccessFileOpenCount() { + return this.successFileOpenCount; + } + + /** + * get total file read count + * + * @return total file read bytes + */ + public long getTotalFileReadCount() { + return this.totalFileReadCount; + } + + /** + * get success file read count + * + * @return success file read count + */ + public long getSuccessFileReadCount() { + return this.successFileReadCount; + } + + /** + * get total file write count + * + * @return total file write bytes + */ + public long getTotalFileWriteCount() { + return this.totalFileWriteCount; + } + + /** + * get success file write count + * + * @return success file write count + */ + public long getSuccessFileWriteCount() { + return this.successFileWriteCount; + } + + /** + * get last source update timestamp + * + * @return last source update timestamp + */ + public Date getLastSourceUpdate() { + return this.lastSourceUpdate; + } + + /** + * get last synced update timestamp + * + * @return last synced update timestamp + */ + public Date getLastSyncUpdate() { + return this.lastSyncUpdate; + } + + /** + * get last synced timestamp + * + * @return last synced timestamp + */ + public Date getLastSyncedTimestamp() { + return this.lastSyncedTimestamp; + } + + /** + * get last heart beat timestamp + * + * @return last heart beat timestamp + */ + public Date getLastHeartBeatTime() { + return this.lastHeartBeatTime; + } + + /** + * if the trunk server + * + * @return true for the trunk server, otherwise false + */ + public boolean isTrunkServer() { + return this.ifTrunkServer; + } + + /** + * get connection alloc count + * + * @return connection alloc count + */ + public int getConnectionAllocCount() { + return this.connectionAllocCount; + } + + /** + * get connection current count + * + * @return connection current count + */ + public int getConnectionCurrentCount() { + return this.connectionCurrentCount; + } + + /** + * get connection max count + * + * @return connection max count + */ + public int getConnectionMaxCount() { + return this.connectionMaxCount; + } + + /** + * set fields + * + * @param bs byte array + * @param offset start offset + */ + public void setFields(byte[] bs, int offset) { + this.status = byteValue(bs, offset, fieldsArray[FIELD_INDEX_STATUS]); + this.id = stringValue(bs, offset, fieldsArray[FIELD_INDEX_ID]); + this.ipAddr = stringValue(bs, offset, fieldsArray[FIELD_INDEX_IP_ADDR]); + this.srcIpAddr = stringValue(bs, offset, fieldsArray[FIELD_INDEX_SRC_IP_ADDR]); + this.domainName = stringValue(bs, offset, fieldsArray[FIELD_INDEX_DOMAIN_NAME]); + this.version = stringValue(bs, offset, fieldsArray[FIELD_INDEX_VERSION]); + this.totalMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MB]); + this.freeMB = longValue(bs, offset, fieldsArray[FIELD_INDEX_FREE_MB]); + this.uploadPriority = intValue(bs, offset, fieldsArray[FIELD_INDEX_UPLOAD_PRIORITY]); + this.joinTime = dateValue(bs, offset, fieldsArray[FIELD_INDEX_JOIN_TIME]); + this.upTime = dateValue(bs, offset, fieldsArray[FIELD_INDEX_UP_TIME]); + this.storePathCount = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORE_PATH_COUNT]); + this.subdirCountPerPath = intValue(bs, offset, fieldsArray[FIELD_INDEX_SUBDIR_COUNT_PER_PATH]); + this.storagePort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_PORT]); + this.storageHttpPort = intValue(bs, offset, fieldsArray[FIELD_INDEX_STORAGE_HTTP_PORT]); + this.currentWritePath = intValue(bs, offset, fieldsArray[FIELD_INDEX_CURRENT_WRITE_PATH]); + + this.connectionAllocCount = int32Value(bs, offset, fieldsArray[FIELD_INDEX_CONNECTION_ALLOC_COUNT]); + this.connectionCurrentCount = int32Value(bs, offset, fieldsArray[FIELD_INDEX_CONNECTION_CURRENT_COUNT]); + this.connectionMaxCount = int32Value(bs, offset, fieldsArray[FIELD_INDEX_CONNECTION_MAX_COUNT]); + + this.totalUploadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_UPLOAD_COUNT]); + this.successUploadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_UPLOAD_COUNT]); + this.totalAppendCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_APPEND_COUNT]); + this.successAppendCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_APPEND_COUNT]); + this.totalModifyCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MODIFY_COUNT]); + this.successModifyCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_MODIFY_COUNT]); + this.totalTruncateCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_TRUNCATE_COUNT]); + this.successTruncateCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_TRUNCATE_COUNT]); + this.totalSetMetaCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_SET_META_COUNT]); + this.successSetMetaCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_SET_META_COUNT]); + this.totalDeleteCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_DELETE_COUNT]); + this.successDeleteCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_DELETE_COUNT]); + this.totalDownloadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_DOWNLOAD_COUNT]); + this.successDownloadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_DOWNLOAD_COUNT]); + this.totalGetMetaCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_GET_META_COUNT]); + this.successGetMetaCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_GET_META_COUNT]); + this.totalCreateLinkCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_CREATE_LINK_COUNT]); + this.successCreateLinkCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_CREATE_LINK_COUNT]); + this.totalDeleteLinkCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_DELETE_LINK_COUNT]); + this.successDeleteLinkCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_DELETE_LINK_COUNT]); + this.totalUploadBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_UPLOAD_BYTES]); + this.successUploadBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_UPLOAD_BYTES]); + this.totalAppendBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_APPEND_BYTES]); + this.successAppendBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_APPEND_BYTES]); + this.totalModifyBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_MODIFY_BYTES]); + this.successModifyBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_MODIFY_BYTES]); + this.totalDownloadloadBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_DOWNLOAD_BYTES]); + this.successDownloadloadBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_DOWNLOAD_BYTES]); + this.totalSyncInBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_SYNC_IN_BYTES]); + this.successSyncInBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_SYNC_IN_BYTES]); + this.totalSyncOutBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_SYNC_OUT_BYTES]); + this.successSyncOutBytes = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_SYNC_OUT_BYTES]); + this.totalFileOpenCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_FILE_OPEN_COUNT]); + this.successFileOpenCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_FILE_OPEN_COUNT]); + this.totalFileReadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_FILE_READ_COUNT]); + this.successFileReadCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_FILE_READ_COUNT]); + this.totalFileWriteCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_TOTAL_FILE_WRITE_COUNT]); + this.successFileWriteCount = longValue(bs, offset, fieldsArray[FIELD_INDEX_SUCCESS_FILE_WRITE_COUNT]); + this.lastSourceUpdate = dateValue(bs, offset, fieldsArray[FIELD_INDEX_LAST_SOURCE_UPDATE]); + this.lastSyncUpdate = dateValue(bs, offset, fieldsArray[FIELD_INDEX_LAST_SYNC_UPDATE]); + this.lastSyncedTimestamp = dateValue(bs, offset, fieldsArray[FIELD_INDEX_LAST_SYNCED_TIMESTAMP]); + this.lastHeartBeatTime = dateValue(bs, offset, fieldsArray[FIELD_INDEX_LAST_HEART_BEAT_TIME]); + this.ifTrunkServer = booleanValue(bs, offset, fieldsArray[FIELD_INDEX_IF_TRUNK_FILE]); + } } diff --git a/src/main/java/org/csource/fastdfs/TrackerClient.java b/src/main/java/org/csource/fastdfs/TrackerClient.java index 0e32113..c7fb020 100644 --- a/src/main/java/org/csource/fastdfs/TrackerClient.java +++ b/src/main/java/org/csource/fastdfs/TrackerClient.java @@ -1,990 +1,811 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; import java.io.IOException; import java.io.OutputStream; -import java.util.Arrays; import java.net.Socket; +import java.util.Arrays; /** -* Tracker client -* @author Happy Fish / YuQing -* @version Version 1.19 -*/ -public class TrackerClient -{ - protected TrackerGroup tracker_group; + * Tracker client + * + * @author Happy Fish / YuQing + * @version Version 1.19 + */ +public class TrackerClient { + protected TrackerGroup tracker_group; protected byte errno; - -/** -* constructor with global tracker group -*/ - public TrackerClient() - { - this.tracker_group = ClientGlobal.g_tracker_group; - } - -/** -* constructor with specified tracker group -* @param tracker_group the tracker group object -*/ - public TrackerClient(TrackerGroup tracker_group) - { - this.tracker_group = tracker_group; - } - -/** -* get the error code of last call -* @return the error code of last call -*/ - public byte getErrorCode() - { - return this.errno; - } - - /** - * get a connection to tracker server - * @return tracker server Socket object, return null if fail - */ - public TrackerServer getConnection() throws IOException - { - return this.tracker_group.getConnection(); - } - - /** - * query storage server to upload file - * @param trackerServer the tracker server - * @return storage server Socket object, return null if fail - */ - public StorageServer getStoreStorage(TrackerServer trackerServer) throws IOException - { - final String groupName = null; - return this.getStoreStorage(trackerServer, groupName); - } - - /** - * query storage server to upload file - * @param trackerServer the tracker server - * @param groupName the group name to upload file to, can be empty - * @return storage server object, return null if fail - */ - public StorageServer getStoreStorage(TrackerServer trackerServer, String groupName) throws IOException - { - byte[] header; - String ip_addr; - int port; - byte cmd; - int out_len; - boolean bNewConnection; - byte store_path; - Socket trackerSocket; - - if (trackerServer == null) - { - trackerServer = getConnection(); - if (trackerServer == null) - { - return null; - } - bNewConnection = true; - } - else - { - bNewConnection = false; - } - trackerSocket = trackerServer.getSocket(); - OutputStream out = trackerSocket.getOutputStream(); - - try - { - if (groupName == null || groupName.length() == 0) - { - cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE; - out_len = 0; - } - else - { - cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE; - out_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; - } - header = ProtoCommon.packHeader(cmd, out_len, (byte)0); - out.write(header); + /** + * constructor with global tracker group + */ + public TrackerClient() { + this.tracker_group = ClientGlobal.g_tracker_group; + } - if (groupName != null && groupName.length() > 0) - { - byte[] bGroupName; - byte[] bs; - int group_len; - - bs = groupName.getBytes(ClientGlobal.g_charset); - bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - - if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) - { - group_len = bs.length; - } - else - { - group_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; - } - Arrays.fill(bGroupName, (byte)0); - System.arraycopy(bs, 0, bGroupName, 0, group_len); - out.write(bGroupName); - } - - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), - ProtoCommon.TRACKER_PROTO_CMD_RESP, - ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN); - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return null; - } - - ip_addr = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, ProtoCommon.FDFS_IPADDR_SIZE-1).trim(); - - port = (int)ProtoCommon.buff2long(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN - + ProtoCommon.FDFS_IPADDR_SIZE-1); - store_path = pkgInfo.body[ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN - 1]; - - return new StorageServer(ip_addr, port, store_path); - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - trackerServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - } - - throw ex; - } - finally - { - if (bNewConnection) - { - try - { - trackerServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - } - } - } + /** + * constructor with specified tracker group + * + * @param tracker_group the tracker group object + */ + public TrackerClient(TrackerGroup tracker_group) { + this.tracker_group = tracker_group; + } - /** - * query storage servers to upload file - * @param trackerServer the tracker server - * @param groupName the group name to upload file to, can be empty - * @return storage servers, return null if fail - */ - public StorageServer[] getStoreStorages(TrackerServer trackerServer, String groupName) throws IOException - { - byte[] header; - String ip_addr; - int port; - byte cmd; - int out_len; - boolean bNewConnection; - Socket trackerSocket; - - if (trackerServer == null) - { - trackerServer = getConnection(); - if (trackerServer == null) - { - return null; - } - bNewConnection = true; - } - else - { - bNewConnection = false; - } + /** + * get the error code of last call + * + * @return the error code of last call + */ + public byte getErrorCode() { + return this.errno; + } - trackerSocket = trackerServer.getSocket(); - OutputStream out = trackerSocket.getOutputStream(); - - try - { - if (groupName == null || groupName.length() == 0) - { - cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL; - out_len = 0; - } - else - { - cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL; - out_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; - } - header = ProtoCommon.packHeader(cmd, out_len, (byte)0); - out.write(header); + /** + * get a connection to tracker server + * + * @return tracker server Socket object, return null if fail + */ + public TrackerServer getConnection() throws IOException { + return this.tracker_group.getConnection(); + } - if (groupName != null && groupName.length() > 0) - { - byte[] bGroupName; - byte[] bs; - int group_len; - - bs = groupName.getBytes(ClientGlobal.g_charset); - bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - - if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) - { - group_len = bs.length; - } - else - { - group_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; - } - Arrays.fill(bGroupName, (byte)0); - System.arraycopy(bs, 0, bGroupName, 0, group_len); - out.write(bGroupName); - } - - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), - ProtoCommon.TRACKER_PROTO_CMD_RESP, -1); - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return null; - } - - if (pkgInfo.body.length < ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN) - { - this.errno = ProtoCommon.ERR_NO_EINVAL; - return null; - } - - int ipPortLen = pkgInfo.body.length - (ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1); - final int recordLength = ProtoCommon.FDFS_IPADDR_SIZE - 1 + ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; - - if (ipPortLen % recordLength != 0) - { - this.errno = ProtoCommon.ERR_NO_EINVAL; - return null; - } - - int serverCount = ipPortLen / recordLength; - if (serverCount > 16) - { - this.errno = ProtoCommon.ERR_NO_ENOSPC; - return null; - } - - StorageServer[] results = new StorageServer[serverCount]; - byte store_path = pkgInfo.body[pkgInfo.body.length - 1]; - int offset = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; - - for (int i=0; i decoder = new ProtoStructDecoder(); - return decoder.decode(pkgInfo.body, StructGroupStat.class, StructGroupStat.getFieldsTotalSize()); - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - trackerServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - } - - throw ex; - } - catch(Exception ex) - { - ex.printStackTrace(); - this.errno = ProtoCommon.ERR_NO_EINVAL; - return null; - } - finally - { - if (bNewConnection) - { - try - { - trackerServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - } - } - } + trackerSocket = trackerServer.getSocket(); + OutputStream out = trackerSocket.getOutputStream(); - /** - * query storage server stat info of the group - * @param trackerServer the tracker server - * @param groupName the group name of storage server - * @return storage server stat array, return null if fail - */ - public StructStorageStat[] listStorages(TrackerServer trackerServer, String groupName) throws IOException - { - final String storageIpAddr = null; - return this.listStorages(trackerServer, groupName, storageIpAddr); - } - - /** - * query storage server stat info of the group - * @param trackerServer the tracker server - * @param groupName the group name of storage server - * @param storageIpAddr the storage server ip address, can be null or empty - * @return storage server stat array, return null if fail - */ - public StructStorageStat[] listStorages(TrackerServer trackerServer, - String groupName, String storageIpAddr) throws IOException - { - byte[] header; - byte[] bGroupName; - byte[] bs; - int len; - boolean bNewConnection; - Socket trackerSocket; - - if (trackerServer == null) - { - trackerServer = getConnection(); - if (trackerServer == null) - { - return null; - } - bNewConnection = true; - } - else - { - bNewConnection = false; - } - trackerSocket = trackerServer.getSocket(); - OutputStream out = trackerSocket.getOutputStream(); - - try - { - bs = groupName.getBytes(ClientGlobal.g_charset); - bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - - if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) - { - len = bs.length; - } - else - { - len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; - } - Arrays.fill(bGroupName, (byte)0); - System.arraycopy(bs, 0, bGroupName, 0, len); - - int ipAddrLen; - byte[] bIpAddr; - if (storageIpAddr != null && storageIpAddr.length() > 0) - { - bIpAddr = storageIpAddr.getBytes(ClientGlobal.g_charset); - if (bIpAddr.length < ProtoCommon.FDFS_IPADDR_SIZE) - { - ipAddrLen = bIpAddr.length; - } - else - { - ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1; - } - } - else - { - bIpAddr = null; - ipAddrLen = 0; - } - - header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte)0); - byte[] wholePkg = new byte[header.length + bGroupName.length + ipAddrLen]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - System.arraycopy(bGroupName, 0, wholePkg, header.length, bGroupName.length); - if (ipAddrLen > 0) - { - System.arraycopy(bIpAddr, 0, wholePkg, header.length + bGroupName.length, ipAddrLen); - } - out.write(wholePkg); - - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), - ProtoCommon.TRACKER_PROTO_CMD_RESP, -1); - this.errno = pkgInfo.errno; - if (pkgInfo.errno != 0) - { - return null; - } - - ProtoStructDecoder decoder = new ProtoStructDecoder(); - return decoder.decode(pkgInfo.body, StructStorageStat.class, StructStorageStat.getFieldsTotalSize()); - } - catch(IOException ex) - { - if (!bNewConnection) - { - try - { - trackerServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - } - - throw ex; - } - catch(Exception ex) - { - ex.printStackTrace(); - this.errno = ProtoCommon.ERR_NO_EINVAL; - return null; - } - finally - { - if (bNewConnection) - { - try - { - trackerServer.close(); - } - catch(IOException ex1) - { - ex1.printStackTrace(); - } - } - } - } + try { + if (groupName == null || groupName.length() == 0) { + cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE; + out_len = 0; + } else { + cmd = ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE; + out_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; + } + header = ProtoCommon.packHeader(cmd, out_len, (byte) 0); + out.write(header); - /** - * delete a storage server from the tracker server - * @param trackerServer the connected tracker server - * @param groupName the group name of storage server - * @param storageIpAddr the storage server ip address - * @return true for success, false for fail - */ - private boolean deleteStorage(TrackerServer trackerServer, - String groupName, String storageIpAddr) throws IOException - { - byte[] header; - byte[] bGroupName; - byte[] bs; - int len; - Socket trackerSocket; - - trackerSocket = trackerServer.getSocket(); - OutputStream out = trackerSocket.getOutputStream(); - - bs = groupName.getBytes(ClientGlobal.g_charset); - bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - - if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) - { - len = bs.length; - } - else - { - len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; - } - Arrays.fill(bGroupName, (byte)0); - System.arraycopy(bs, 0, bGroupName, 0, len); - - int ipAddrLen; - byte[] bIpAddr = storageIpAddr.getBytes(ClientGlobal.g_charset); - if (bIpAddr.length < ProtoCommon.FDFS_IPADDR_SIZE) - { - ipAddrLen = bIpAddr.length; - } - else - { - ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1; - } + if (groupName != null && groupName.length() > 0) { + byte[] bGroupName; + byte[] bs; + int group_len; - header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte)0); - byte[] wholePkg = new byte[header.length + bGroupName.length + ipAddrLen]; - System.arraycopy(header, 0, wholePkg, 0, header.length); - System.arraycopy(bGroupName, 0, wholePkg, header.length, bGroupName.length); - System.arraycopy(bIpAddr, 0, wholePkg, header.length + bGroupName.length, ipAddrLen); - out.write(wholePkg); - - ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), - ProtoCommon.TRACKER_PROTO_CMD_RESP, 0); - this.errno = pkgInfo.errno; - return pkgInfo.errno == 0; - } + bs = groupName.getBytes(ClientGlobal.g_charset); + bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; - /** - * delete a storage server from the global FastDFS cluster - * @param groupName the group name of storage server - * @param storageIpAddr the storage server ip address - * @return true for success, false for fail - */ - public boolean deleteStorage(String groupName, String storageIpAddr) throws IOException - { - return this.deleteStorage(ClientGlobal.g_tracker_group, groupName, storageIpAddr); - } - - /** - * delete a storage server from the FastDFS cluster - * @param trackerGroup the tracker server group - * @param groupName the group name of storage server - * @param storageIpAddr the storage server ip address - * @return true for success, false for fail - */ - public boolean deleteStorage(TrackerGroup trackerGroup, - String groupName, String storageIpAddr) throws IOException - { - int serverIndex; - int notFoundCount; - TrackerServer trackerServer; - - notFoundCount = 0; - for (serverIndex=0; serverIndex 0) { + byte[] bGroupName; + byte[] bs; + int group_len; + + bs = groupName.getBytes(ClientGlobal.g_charset); + bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; + + if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) { + group_len = bs.length; + } else { + group_len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; + } + Arrays.fill(bGroupName, (byte) 0); + System.arraycopy(bs, 0, bGroupName, 0, group_len); + out.write(bGroupName); + } + + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), + ProtoCommon.TRACKER_PROTO_CMD_RESP, -1); + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return null; + } + + if (pkgInfo.body.length < ProtoCommon.TRACKER_QUERY_STORAGE_STORE_BODY_LEN) { + this.errno = ProtoCommon.ERR_NO_EINVAL; + return null; + } + + int ipPortLen = pkgInfo.body.length - (ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + 1); + final int recordLength = ProtoCommon.FDFS_IPADDR_SIZE - 1 + ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + if (ipPortLen % recordLength != 0) { + this.errno = ProtoCommon.ERR_NO_EINVAL; + return null; + } + + int serverCount = ipPortLen / recordLength; + if (serverCount > 16) { + this.errno = ProtoCommon.ERR_NO_ENOSPC; + return null; + } + + StorageServer[] results = new StorageServer[serverCount]; + byte store_path = pkgInfo.body[pkgInfo.body.length - 1]; + int offset = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; + + for (int i = 0; i < serverCount; i++) { + ip_addr = new String(pkgInfo.body, offset, ProtoCommon.FDFS_IPADDR_SIZE - 1).trim(); + offset += ProtoCommon.FDFS_IPADDR_SIZE - 1; + + port = (int) ProtoCommon.buff2long(pkgInfo.body, offset); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + results[i] = new StorageServer(ip_addr, port, store_path); + } + + return results; + } catch (IOException ex) { + if (!bNewConnection) { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + } + } + + /** + * query storage server to download file + * + * @param trackerServer the tracker server + * @param groupName the group name of storage server + * @param filename filename on storage server + * @return storage server Socket object, return null if fail + */ + public StorageServer getFetchStorage(TrackerServer trackerServer, + String groupName, String filename) throws IOException { + ServerInfo[] servers = this.getStorages(trackerServer, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE, + groupName, filename); + if (servers == null) { + return null; + } else { + return new StorageServer(servers[0].getIpAddr(), servers[0].getPort(), 0); + } + } + + /** + * query storage server to update file (delete file or set meta data) + * + * @param trackerServer the tracker server + * @param groupName the group name of storage server + * @param filename filename on storage server + * @return storage server Socket object, return null if fail + */ + public StorageServer getUpdateStorage(TrackerServer trackerServer, + String groupName, String filename) throws IOException { + ServerInfo[] servers = this.getStorages(trackerServer, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE, + groupName, filename); + if (servers == null) { + return null; + } else { + return new StorageServer(servers[0].getIpAddr(), servers[0].getPort(), 0); + } + } + + /** + * get storage servers to download file + * + * @param trackerServer the tracker server + * @param groupName the group name of storage server + * @param filename filename on storage server + * @return storage servers, return null if fail + */ + public ServerInfo[] getFetchStorages(TrackerServer trackerServer, + String groupName, String filename) throws IOException { + return this.getStorages(trackerServer, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ALL, + groupName, filename); + } + + /** + * query storage server to download file + * + * @param trackerServer the tracker server + * @param cmd command code, ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE or + * ProtoCommon.TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE + * @param groupName the group name of storage server + * @param filename filename on storage server + * @return storage server Socket object, return null if fail + */ + protected ServerInfo[] getStorages(TrackerServer trackerServer, + byte cmd, String groupName, String filename) throws IOException { + byte[] header; + byte[] bFileName; + byte[] bGroupName; + byte[] bs; + int len; + String ip_addr; + int port; + boolean bNewConnection; + Socket trackerSocket; + + if (trackerServer == null) { + trackerServer = getConnection(); + if (trackerServer == null) { + return null; + } + bNewConnection = true; + } else { + bNewConnection = false; + } + trackerSocket = trackerServer.getSocket(); + OutputStream out = trackerSocket.getOutputStream(); + + try { + bs = groupName.getBytes(ClientGlobal.g_charset); + bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; + bFileName = filename.getBytes(ClientGlobal.g_charset); + + if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) { + len = bs.length; + } else { + len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; + } + Arrays.fill(bGroupName, (byte) 0); + System.arraycopy(bs, 0, bGroupName, 0, len); + + header = ProtoCommon.packHeader(cmd, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + bFileName.length, (byte) 0); + byte[] wholePkg = new byte[header.length + bGroupName.length + bFileName.length]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + System.arraycopy(bGroupName, 0, wholePkg, header.length, bGroupName.length); + System.arraycopy(bFileName, 0, wholePkg, header.length + bGroupName.length, bFileName.length); + out.write(wholePkg); + + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), + ProtoCommon.TRACKER_PROTO_CMD_RESP, -1); + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return null; + } + + if (pkgInfo.body.length < ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) { + throw new IOException("Invalid body length: " + pkgInfo.body.length); + } + + if ((pkgInfo.body.length - ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) % (ProtoCommon.FDFS_IPADDR_SIZE - 1) != 0) { + throw new IOException("Invalid body length: " + pkgInfo.body.length); + } + + int server_count = 1 + (pkgInfo.body.length - ProtoCommon.TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) / (ProtoCommon.FDFS_IPADDR_SIZE - 1); + + ip_addr = new String(pkgInfo.body, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN, ProtoCommon.FDFS_IPADDR_SIZE - 1).trim(); + int offset = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ProtoCommon.FDFS_IPADDR_SIZE - 1; + + port = (int) ProtoCommon.buff2long(pkgInfo.body, offset); + offset += ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE; + + ServerInfo[] servers = new ServerInfo[server_count]; + servers[0] = new ServerInfo(ip_addr, port); + for (int i = 1; i < server_count; i++) { + servers[i] = new ServerInfo(new String(pkgInfo.body, offset, ProtoCommon.FDFS_IPADDR_SIZE - 1).trim(), port); + offset += ProtoCommon.FDFS_IPADDR_SIZE - 1; + } + + return servers; + } catch (IOException ex) { + if (!bNewConnection) { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + + throw ex; + } finally { + if (bNewConnection) { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + } + } + + /** + * query storage server to download file + * + * @param trackerServer the tracker server + * @param file_id the file id(including group name and filename) + * @return storage server Socket object, return null if fail + */ + public StorageServer getFetchStorage1(TrackerServer trackerServer, String file_id) throws IOException { + String[] parts = new String[2]; + this.errno = StorageClient1.split_file_id(file_id, parts); + if (this.errno != 0) { + return null; + } + + return this.getFetchStorage(trackerServer, parts[0], parts[1]); + } + + /** + * get storage servers to download file + * + * @param trackerServer the tracker server + * @param file_id the file id(including group name and filename) + * @return storage servers, return null if fail + */ + public ServerInfo[] getFetchStorages1(TrackerServer trackerServer, String file_id) throws IOException { + String[] parts = new String[2]; + this.errno = StorageClient1.split_file_id(file_id, parts); + if (this.errno != 0) { + return null; + } + + return this.getFetchStorages(trackerServer, parts[0], parts[1]); + } + + /** + * list groups + * + * @param trackerServer the tracker server + * @return group stat array, return null if fail + */ + public StructGroupStat[] listGroups(TrackerServer trackerServer) throws IOException { + byte[] header; + String ip_addr; + int port; + byte cmd; + int out_len; + boolean bNewConnection; + byte store_path; + Socket trackerSocket; + + if (trackerServer == null) { + trackerServer = getConnection(); + if (trackerServer == null) { + return null; + } + bNewConnection = true; + } else { + bNewConnection = false; + } + + trackerSocket = trackerServer.getSocket(); + OutputStream out = trackerSocket.getOutputStream(); + + try { + header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_GROUP, 0, (byte) 0); + out.write(header); + + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), + ProtoCommon.TRACKER_PROTO_CMD_RESP, -1); + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return null; + } + + ProtoStructDecoder decoder = new ProtoStructDecoder(); + return decoder.decode(pkgInfo.body, StructGroupStat.class, StructGroupStat.getFieldsTotalSize()); + } catch (IOException ex) { + if (!bNewConnection) { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + + throw ex; + } catch (Exception ex) { + ex.printStackTrace(); + this.errno = ProtoCommon.ERR_NO_EINVAL; + return null; + } finally { + if (bNewConnection) { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + } + } + + /** + * query storage server stat info of the group + * + * @param trackerServer the tracker server + * @param groupName the group name of storage server + * @return storage server stat array, return null if fail + */ + public StructStorageStat[] listStorages(TrackerServer trackerServer, String groupName) throws IOException { + final String storageIpAddr = null; + return this.listStorages(trackerServer, groupName, storageIpAddr); + } + + /** + * query storage server stat info of the group + * + * @param trackerServer the tracker server + * @param groupName the group name of storage server + * @param storageIpAddr the storage server ip address, can be null or empty + * @return storage server stat array, return null if fail + */ + public StructStorageStat[] listStorages(TrackerServer trackerServer, + String groupName, String storageIpAddr) throws IOException { + byte[] header; + byte[] bGroupName; + byte[] bs; + int len; + boolean bNewConnection; + Socket trackerSocket; + + if (trackerServer == null) { + trackerServer = getConnection(); + if (trackerServer == null) { + return null; + } + bNewConnection = true; + } else { + bNewConnection = false; + } + trackerSocket = trackerServer.getSocket(); + OutputStream out = trackerSocket.getOutputStream(); + + try { + bs = groupName.getBytes(ClientGlobal.g_charset); + bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; + + if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) { + len = bs.length; + } else { + len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; + } + Arrays.fill(bGroupName, (byte) 0); + System.arraycopy(bs, 0, bGroupName, 0, len); + + int ipAddrLen; + byte[] bIpAddr; + if (storageIpAddr != null && storageIpAddr.length() > 0) { + bIpAddr = storageIpAddr.getBytes(ClientGlobal.g_charset); + if (bIpAddr.length < ProtoCommon.FDFS_IPADDR_SIZE) { + ipAddrLen = bIpAddr.length; + } else { + ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1; + } + } else { + bIpAddr = null; + ipAddrLen = 0; + } + + header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_LIST_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte) 0); + byte[] wholePkg = new byte[header.length + bGroupName.length + ipAddrLen]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + System.arraycopy(bGroupName, 0, wholePkg, header.length, bGroupName.length); + if (ipAddrLen > 0) { + System.arraycopy(bIpAddr, 0, wholePkg, header.length + bGroupName.length, ipAddrLen); + } + out.write(wholePkg); + + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), + ProtoCommon.TRACKER_PROTO_CMD_RESP, -1); + this.errno = pkgInfo.errno; + if (pkgInfo.errno != 0) { + return null; + } + + ProtoStructDecoder decoder = new ProtoStructDecoder(); + return decoder.decode(pkgInfo.body, StructStorageStat.class, StructStorageStat.getFieldsTotalSize()); + } catch (IOException ex) { + if (!bNewConnection) { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + + throw ex; + } catch (Exception ex) { + ex.printStackTrace(); + this.errno = ProtoCommon.ERR_NO_EINVAL; + return null; + } finally { + if (bNewConnection) { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + } + } + + /** + * delete a storage server from the tracker server + * + * @param trackerServer the connected tracker server + * @param groupName the group name of storage server + * @param storageIpAddr the storage server ip address + * @return true for success, false for fail + */ + private boolean deleteStorage(TrackerServer trackerServer, + String groupName, String storageIpAddr) throws IOException { + byte[] header; + byte[] bGroupName; + byte[] bs; + int len; + Socket trackerSocket; + + trackerSocket = trackerServer.getSocket(); + OutputStream out = trackerSocket.getOutputStream(); + + bs = groupName.getBytes(ClientGlobal.g_charset); + bGroupName = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN]; + + if (bs.length <= ProtoCommon.FDFS_GROUP_NAME_MAX_LEN) { + len = bs.length; + } else { + len = ProtoCommon.FDFS_GROUP_NAME_MAX_LEN; + } + Arrays.fill(bGroupName, (byte) 0); + System.arraycopy(bs, 0, bGroupName, 0, len); + + int ipAddrLen; + byte[] bIpAddr = storageIpAddr.getBytes(ClientGlobal.g_charset); + if (bIpAddr.length < ProtoCommon.FDFS_IPADDR_SIZE) { + ipAddrLen = bIpAddr.length; + } else { + ipAddrLen = ProtoCommon.FDFS_IPADDR_SIZE - 1; + } + + header = ProtoCommon.packHeader(ProtoCommon.TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE, ProtoCommon.FDFS_GROUP_NAME_MAX_LEN + ipAddrLen, (byte) 0); + byte[] wholePkg = new byte[header.length + bGroupName.length + ipAddrLen]; + System.arraycopy(header, 0, wholePkg, 0, header.length); + System.arraycopy(bGroupName, 0, wholePkg, header.length, bGroupName.length); + System.arraycopy(bIpAddr, 0, wholePkg, header.length + bGroupName.length, ipAddrLen); + out.write(wholePkg); + + ProtoCommon.RecvPackageInfo pkgInfo = ProtoCommon.recvPackage(trackerSocket.getInputStream(), + ProtoCommon.TRACKER_PROTO_CMD_RESP, 0); + this.errno = pkgInfo.errno; + return pkgInfo.errno == 0; + } + + /** + * delete a storage server from the global FastDFS cluster + * + * @param groupName the group name of storage server + * @param storageIpAddr the storage server ip address + * @return true for success, false for fail + */ + public boolean deleteStorage(String groupName, String storageIpAddr) throws IOException { + return this.deleteStorage(ClientGlobal.g_tracker_group, groupName, storageIpAddr); + } + + /** + * delete a storage server from the FastDFS cluster + * + * @param trackerGroup the tracker server group + * @param groupName the group name of storage server + * @param storageIpAddr the storage server ip address + * @return true for success, false for fail + */ + public boolean deleteStorage(TrackerGroup trackerGroup, + String groupName, String storageIpAddr) throws IOException { + int serverIndex; + int notFoundCount; + TrackerServer trackerServer; + + notFoundCount = 0; + for (serverIndex = 0; serverIndex < trackerGroup.tracker_servers.length; serverIndex++) { + try { + trackerServer = trackerGroup.getConnection(serverIndex); + } catch (IOException ex) { + ex.printStackTrace(System.err); + this.errno = ProtoCommon.ECONNREFUSED; + return false; + } + + try { + StructStorageStat[] storageStats = listStorages(trackerServer, groupName, storageIpAddr); + if (storageStats == null) { + if (this.errno == ProtoCommon.ERR_NO_ENOENT) { + notFoundCount++; + } else { + return false; + } + } else if (storageStats.length == 0) { + notFoundCount++; + } else if (storageStats[0].getStatus() == ProtoCommon.FDFS_STORAGE_STATUS_ONLINE || + storageStats[0].getStatus() == ProtoCommon.FDFS_STORAGE_STATUS_ACTIVE) { + this.errno = ProtoCommon.ERR_NO_EBUSY; + return false; + } + } finally { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + } + + if (notFoundCount == trackerGroup.tracker_servers.length) { + this.errno = ProtoCommon.ERR_NO_ENOENT; + return false; + } + + notFoundCount = 0; + for (serverIndex = 0; serverIndex < trackerGroup.tracker_servers.length; serverIndex++) { + try { + trackerServer = trackerGroup.getConnection(serverIndex); + } catch (IOException ex) { + System.err.println("connect to server " + trackerGroup.tracker_servers[serverIndex].getAddress().getHostAddress() + ":" + trackerGroup.tracker_servers[serverIndex].getPort() + " fail"); + ex.printStackTrace(System.err); + this.errno = ProtoCommon.ECONNREFUSED; + return false; + } + + try { + if (!this.deleteStorage(trackerServer, groupName, storageIpAddr)) { + if (this.errno != 0) { + if (this.errno == ProtoCommon.ERR_NO_ENOENT) { + notFoundCount++; + } else if (this.errno != ProtoCommon.ERR_NO_EALREADY) { + return false; + } + } + } + } finally { + try { + trackerServer.close(); + } catch (IOException ex1) { + ex1.printStackTrace(); + } + } + } + + if (notFoundCount == trackerGroup.tracker_servers.length) { + this.errno = ProtoCommon.ERR_NO_ENOENT; + return false; + } + + if (this.errno == ProtoCommon.ERR_NO_ENOENT) { + this.errno = 0; + } + + return this.errno == 0; + } } diff --git a/src/main/java/org/csource/fastdfs/TrackerGroup.java b/src/main/java/org/csource/fastdfs/TrackerGroup.java index ba0d514..959c3fd 100644 --- a/src/main/java/org/csource/fastdfs/TrackerGroup.java +++ b/src/main/java/org/csource/fastdfs/TrackerGroup.java @@ -1,121 +1,106 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.common.*; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; /** -* Tracker server group -* @author Happy Fish / YuQing -* @version Version 1.17 -*/ -public class TrackerGroup -{ - protected Integer lock; - public int tracker_server_index; - public InetSocketAddress[] tracker_servers; - -/** -* Constructor -* @param tracker_servers tracker servers -*/ - public TrackerGroup(InetSocketAddress[] tracker_servers) - { - this.tracker_servers = tracker_servers; - this.lock = new Integer(0); - this.tracker_server_index = 0; - } + * Tracker server group + * + * @author Happy Fish / YuQing + * @version Version 1.17 + */ +public class TrackerGroup { + public int tracker_server_index; + public InetSocketAddress[] tracker_servers; + protected Integer lock; -/** -* return connected tracker server -* @return connected tracker server, null for fail -*/ - public TrackerServer getConnection(int serverIndex) throws IOException - { - Socket sock = new Socket(); - sock.setReuseAddress(true); - sock.setSoTimeout(ClientGlobal.g_network_timeout); - sock.connect(this.tracker_servers[serverIndex], ClientGlobal.g_connect_timeout); - return new TrackerServer(sock, this.tracker_servers[serverIndex]); - } - -/** -* return connected tracker server -* @return connected tracker server, null for fail -*/ - public TrackerServer getConnection() throws IOException - { - int current_index; - - synchronized(this.lock) - { - this.tracker_server_index++; - if (this.tracker_server_index >= this.tracker_servers.length) - { - this.tracker_server_index = 0; - } - - current_index = this.tracker_server_index; - } - - try - { - return this.getConnection(current_index); - } - catch(IOException ex) - { - System.err.println("connect to server " + this.tracker_servers[current_index].getAddress().getHostAddress() + ":" + this.tracker_servers[current_index].getPort() + " fail"); - ex.printStackTrace(System.err); - } - - for (int i=0; i= this.tracker_servers.length) { + this.tracker_server_index = 0; + } + + current_index = this.tracker_server_index; + } + + try { + return this.getConnection(current_index); + } catch (IOException ex) { + System.err.println("connect to server " + this.tracker_servers[current_index].getAddress().getHostAddress() + ":" + this.tracker_servers[current_index].getPort() + " fail"); + ex.printStackTrace(System.err); + } + + for (int i = 0; i < this.tracker_servers.length; i++) { + if (i == current_index) { + continue; + } + + try { + TrackerServer trackerServer = this.getConnection(i); + + synchronized (this.lock) { + if (this.tracker_server_index == current_index) { + this.tracker_server_index = i; + } + } + + return trackerServer; + } catch (IOException ex) { + System.err.println("connect to server " + this.tracker_servers[i].getAddress().getHostAddress() + ":" + this.tracker_servers[i].getPort() + " fail"); + ex.printStackTrace(System.err); + } + } + + return null; + } + + public Object clone() { + InetSocketAddress[] trackerServers = new InetSocketAddress[this.tracker_servers.length]; + for (int i = 0; i < trackerServers.length; i++) { + trackerServers[i] = new InetSocketAddress(this.tracker_servers[i].getAddress().getHostAddress(), this.tracker_servers[i].getPort()); + } + + return new TrackerGroup(trackerServers); + } } diff --git a/src/main/java/org/csource/fastdfs/TrackerServer.java b/src/main/java/org/csource/fastdfs/TrackerServer.java index e8a2d42..dffdb33 100644 --- a/src/main/java/org/csource/fastdfs/TrackerServer.java +++ b/src/main/java/org/csource/fastdfs/TrackerServer.java @@ -1,89 +1,81 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.common.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; /** -* Tracker Server Info -* @author Happy Fish / YuQing -* @version Version 1.11 -*/ -public class TrackerServer -{ - protected Socket sock; - protected InetSocketAddress inetSockAddr; - -/** -* Constructor -* @param sock Socket of server -* @param inetSockAddr the server info -*/ - public TrackerServer(Socket sock, InetSocketAddress inetSockAddr) - { - this.sock = sock; - this.inetSockAddr = inetSockAddr; - } - -/** -* get the connected socket -* @return the socket -*/ - public Socket getSocket() throws IOException - { - if (this.sock == null) - { - this.sock = ClientGlobal.getSocket(this.inetSockAddr); - } - - return this.sock; - } - -/** -* get the server info -* @return the server info -*/ - public InetSocketAddress getInetSocketAddress() - { - return this.inetSockAddr; - } - - public OutputStream getOutputStream() throws IOException - { - return this.sock.getOutputStream(); - } - - public InputStream getInputStream() throws IOException - { - return this.sock.getInputStream(); - } + * Tracker Server Info + * + * @author Happy Fish / YuQing + * @version Version 1.11 + */ +public class TrackerServer { + protected Socket sock; + protected InetSocketAddress inetSockAddr; - public void close() throws IOException - { - if (this.sock != null) - { - try - { - ProtoCommon.closeSocket(this.sock); - } - finally - { - this.sock = null; - } - } - } - - protected void finalize() throws Throwable - { - this.close(); - } + /** + * Constructor + * + * @param sock Socket of server + * @param inetSockAddr the server info + */ + public TrackerServer(Socket sock, InetSocketAddress inetSockAddr) { + this.sock = sock; + this.inetSockAddr = inetSockAddr; + } + + /** + * get the connected socket + * + * @return the socket + */ + public Socket getSocket() throws IOException { + if (this.sock == null) { + this.sock = ClientGlobal.getSocket(this.inetSockAddr); + } + + return this.sock; + } + + /** + * get the server info + * + * @return the server info + */ + public InetSocketAddress getInetSocketAddress() { + return this.inetSockAddr; + } + + public OutputStream getOutputStream() throws IOException { + return this.sock.getOutputStream(); + } + + public InputStream getInputStream() throws IOException { + return this.sock.getInputStream(); + } + + public void close() throws IOException { + if (this.sock != null) { + try { + ProtoCommon.closeSocket(this.sock); + } finally { + this.sock = null; + } + } + } + + protected void finalize() throws Throwable { + this.close(); + } } diff --git a/src/main/java/org/csource/fastdfs/UploadCallback.java b/src/main/java/org/csource/fastdfs/UploadCallback.java index 74bbc5a..46603e9 100644 --- a/src/main/java/org/csource/fastdfs/UploadCallback.java +++ b/src/main/java/org/csource/fastdfs/UploadCallback.java @@ -1,29 +1,28 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.common.*; +import java.io.IOException; +import java.io.OutputStream; /** -* upload file callback interface -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ -public interface UploadCallback -{ - /** - * 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; + * upload file callback interface + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ +public interface UploadCallback { + /** + * 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; } diff --git a/src/main/java/org/csource/fastdfs/UploadStream.java b/src/main/java/org/csource/fastdfs/UploadStream.java index 5c207e7..6fb79da 100644 --- a/src/main/java/org/csource/fastdfs/UploadStream.java +++ b/src/main/java/org/csource/fastdfs/UploadStream.java @@ -1,61 +1,55 @@ package org.csource.fastdfs; import java.io.IOException; -import java.io.OutputStream; import java.io.InputStream; -import org.csource.fastdfs.UploadCallback; +import java.io.OutputStream; /** -* Upload file by stream -* @author zhouzezhong & Happy Fish / YuQing -* @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 - - /** - * 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; - } + * Upload file by stream + * + * @author zhouzezhong & Happy Fish / YuQing + * @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 - /** - * 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; - } + /** + * 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; + } + + return 0; + } } diff --git a/src/main/java/org/csource/fastdfs/test/DownloadFileWriter.java b/src/main/java/org/csource/fastdfs/test/DownloadFileWriter.java index 516ec2d..d2a4f83 100644 --- a/src/main/java/org/csource/fastdfs/test/DownloadFileWriter.java +++ b/src/main/java/org/csource/fastdfs/test/DownloadFileWriter.java @@ -1,69 +1,60 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs.test; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.fastdfs.*; +import org.csource.fastdfs.DownloadCallback; + +import java.io.FileOutputStream; +import java.io.IOException; /** -* DowloadCallback test -* @author Happy Fish / YuQing -* @version Version 1.3 -*/ -public class DownloadFileWriter implements DownloadCallback -{ - private String filename; - private FileOutputStream out = null; - private long current_bytes = 0; - - public DownloadFileWriter(String filename) - { - this.filename = filename; - } - - public int recv(long file_size, byte[] data, int bytes) - { - try - { - if (this.out == null) - { - this.out = new FileOutputStream(this.filename); - } - - this.out.write(data, 0, bytes); - this.current_bytes += bytes; - - if (this.current_bytes == file_size) - { - this.out.close(); - this.out = null; - this.current_bytes = 0; - } - } - catch(IOException ex) - { - ex.printStackTrace(); - return -1; - } - - return 0; - } - - protected void finalize() throws Throwable - { - if (this.out != null) - { - this.out.close(); - this.out = null; - } - } + * DowloadCallback test + * + * @author Happy Fish / YuQing + * @version Version 1.3 + */ +public class DownloadFileWriter implements DownloadCallback { + private String filename; + private FileOutputStream out = null; + private long current_bytes = 0; + + public DownloadFileWriter(String filename) { + this.filename = filename; + } + + public int recv(long file_size, byte[] data, int bytes) { + try { + if (this.out == null) { + this.out = new FileOutputStream(this.filename); + } + + this.out.write(data, 0, bytes); + this.current_bytes += bytes; + + if (this.current_bytes == file_size) { + this.out.close(); + this.out = null; + this.current_bytes = 0; + } + } catch (IOException ex) { + ex.printStackTrace(); + return -1; + } + + return 0; + } + + protected void finalize() throws Throwable { + if (this.out != null) { + this.out.close(); + this.out = null; + } + } } diff --git a/src/main/java/org/csource/fastdfs/test/Monitor.java b/src/main/java/org/csource/fastdfs/test/Monitor.java index b3cb257..6e437ec 100644 --- a/src/main/java/org/csource/fastdfs/test/Monitor.java +++ b/src/main/java/org/csource/fastdfs/test/Monitor.java @@ -1,225 +1,203 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs.test; -import java.text.SimpleDateFormat; -import org.csource.common.*; import org.csource.fastdfs.*; +import java.text.SimpleDateFormat; + /** -* load test class -* @author Happy Fish / YuQing -* @version Version 1.20 -*/ -public class Monitor -{ - private Monitor() - { - } - - /** - * entry point - * @param args comand arguments - *

- */ - public static void main(String args[]) - { - if (args.length < 1) - { - System.out.println("Error: Must have 1 parameter: config filename"); - return; - } - - System.out.println("java.version=" + System.getProperty("java.version")); - - try - { - ClientGlobal.init(args[0]); - System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); - System.out.println("charset=" + ClientGlobal.g_charset); - - TrackerClient tracker = new TrackerClient(); - + * load test class + * + * @author Happy Fish / YuQing + * @version Version 1.20 + */ +public class Monitor { + private Monitor() { + } + + /** + * entry point + * + * @param args comand arguments + * + */ + public static void main(String args[]) { + if (args.length < 1) { + System.out.println("Error: Must have 1 parameter: config filename"); + return; + } + + System.out.println("java.version=" + System.getProperty("java.version")); + + try { + ClientGlobal.init(args[0]); + System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + System.out.println("charset=" + ClientGlobal.g_charset); + + TrackerClient tracker = new TrackerClient(); + /* - System.out.println("delete storage return: " + tracker.deleteStorage("group1", "192.168.0.192")); + System.out.println("delete storage return: " + tracker.deleteStorage("group1", "192.168.0.192")); System.out.println("delete storage errno: " + tracker.getErrorCode()); */ - - TrackerServer trackerServer = tracker.getConnection(); - if (trackerServer == null) - { - return; - } - - int count; - StructGroupStat[] groupStats = tracker.listGroups(trackerServer); - if (groupStats == null) - { - System.out.println(""); - System.out.println("ERROR! list groups error, error no: " + tracker.getErrorCode()); - System.out.println(""); - return; - } - - System.out.println("group count: " + groupStats.length); - - count = 0; - for (StructGroupStat groupStat : groupStats) - { - count++; - System.out.println("Group " + count + ":"); - System.out.println("group name = " + groupStat.getGroupName()); - System.out.println("disk total space = " + groupStat.getTotalMB() + "MB"); - System.out.println("disk free space = " + groupStat.getFreeMB() + " MB"); - System.out.println("trunk free space = " + groupStat.getTrunkFreeMB() + " MB"); - System.out.println("storage server count = " + groupStat.getStorageCount()); - System.out.println("active server count = " + groupStat.getActiveCount()); - System.out.println("storage server port = " + groupStat.getStoragePort()); - System.out.println("storage HTTP port = " + groupStat.getStorageHttpPort()); - System.out.println("store path count = " + groupStat.getStorePathCount()); - System.out.println("subdir count per path = " + groupStat.getSubdirCountPerPath()); - System.out.println("current write server index = " + groupStat.getCurrentWriteServer()); - System.out.println("current trunk file id = " + groupStat.getCurrentTrunkFileId()); - - StructStorageStat[] storageStats = tracker.listStorages(trackerServer, groupStat.getGroupName()); - if (storageStats == null) - { - System.out.println(""); - System.out.println("ERROR! list storage error, error no: " + tracker.getErrorCode()); - System.out.println(""); - break; - } - - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - int stroageCount = 0; - for (StructStorageStat storageStat : storageStats) - { - stroageCount++; - System.out.println("\tStorage " + stroageCount + ":"); - System.out.println("\t\tstorage id = " + storageStat.getId()); - System.out.println("\t\tip_addr = " + storageStat.getIpAddr() + " " + ProtoCommon.getStorageStatusCaption(storageStat.getStatus())); - System.out.println("\t\thttp domain = " + storageStat.getDomainName()); - System.out.println("\t\tversion = " + storageStat.getVersion()); - System.out.println("\t\tjoin time = " + df.format(storageStat.getJoinTime())); - System.out.println("\t\tup time = " + (storageStat.getUpTime().getTime() == 0 ? "" : df.format(storageStat.getUpTime()))); - System.out.println("\t\ttotal storage = " + storageStat.getTotalMB() + "MB"); - System.out.println("\t\tfree storage = " + storageStat.getFreeMB() + "MB"); - System.out.println("\t\tupload priority = " + storageStat.getUploadPriority()); - System.out.println("\t\tstore_path_count = " + storageStat.getStorePathCount()); - System.out.println("\t\tsubdir_count_per_path = " + storageStat.getSubdirCountPerPath()); - System.out.println("\t\tstorage_port = " + storageStat.getStoragePort()); - System.out.println("\t\tstorage_http_port = " + storageStat.getStorageHttpPort()); - System.out.println("\t\tcurrent_write_path = " + storageStat.getCurrentWritePath()); - System.out.println("\t\tsource ip_addr = " + storageStat.getSrcIpAddr()); - System.out.println("\t\tif_trunk_server = " + storageStat.isTrunkServer()); - System.out.println("\t\tconntion.alloc_count = " + storageStat.getConnectionAllocCount()); - System.out.println("\t\tconntion.current_count = " + storageStat.getConnectionCurrentCount()); - System.out.println("\t\tconntion.max_count = " + storageStat.getConnectionMaxCount()); - System.out.println("\t\ttotal_upload_count = " + storageStat.getTotalUploadCount()); - System.out.println("\t\tsuccess_upload_count = " + storageStat.getSuccessUploadCount()); - System.out.println("\t\ttotal_append_count = " + storageStat.getTotalAppendCount()); - System.out.println("\t\tsuccess_append_count = " + storageStat.getSuccessAppendCount()); - System.out.println("\t\ttotal_modify_count = " + storageStat.getTotalModifyCount()); - System.out.println("\t\tsuccess_modify_count = " + storageStat.getSuccessModifyCount()); - System.out.println("\t\ttotal_truncate_count = " + storageStat.getTotalTruncateCount()); - System.out.println("\t\tsuccess_truncate_count = " + storageStat.getSuccessTruncateCount()); - System.out.println("\t\ttotal_set_meta_count = " + storageStat.getTotalSetMetaCount()); - System.out.println("\t\tsuccess_set_meta_count = " + storageStat.getSuccessSetMetaCount()); - System.out.println("\t\ttotal_delete_count = " + storageStat.getTotalDeleteCount()); - System.out.println("\t\tsuccess_delete_count = " + storageStat.getSuccessDeleteCount()); - System.out.println("\t\ttotal_download_count = " + storageStat.getTotalDownloadCount()); - System.out.println("\t\tsuccess_download_count = " + storageStat.getSuccessDownloadCount()); - System.out.println("\t\ttotal_get_meta_count = " + storageStat.getTotalGetMetaCount()); - System.out.println("\t\tsuccess_get_meta_count = " + storageStat.getSuccessGetMetaCount()); - System.out.println("\t\ttotal_create_link_count = " + storageStat.getTotalCreateLinkCount()); - System.out.println("\t\tsuccess_create_link_count = " + storageStat.getSuccessCreateLinkCount()); - System.out.println("\t\ttotal_delete_link_count = " + storageStat.getTotalDeleteLinkCount()); - System.out.println("\t\tsuccess_delete_link_count = " + storageStat.getSuccessDeleteLinkCount()); - System.out.println("\t\ttotal_upload_bytes = " + storageStat.getTotalUploadBytes()); - System.out.println("\t\tsuccess_upload_bytes = " + storageStat.getSuccessUploadBytes()); - System.out.println("\t\ttotal_append_bytes = " + storageStat.getTotalAppendBytes()); - System.out.println("\t\tsuccess_append_bytes = " + storageStat.getSuccessAppendBytes()); - System.out.println("\t\ttotal_modify_bytes = " + storageStat.getTotalModifyBytes()); - System.out.println("\t\tsuccess_modify_bytes = " + storageStat.getSuccessModifyBytes()); - System.out.println("\t\ttotal_download_bytes = " + storageStat.getTotalDownloadloadBytes()); - System.out.println("\t\tsuccess_download_bytes = " + storageStat.getSuccessDownloadloadBytes()); - System.out.println("\t\ttotal_sync_in_bytes = " + storageStat.getTotalSyncInBytes()); - System.out.println("\t\tsuccess_sync_in_bytes = " + storageStat.getSuccessSyncInBytes()); - System.out.println("\t\ttotal_sync_out_bytes = " + storageStat.getTotalSyncOutBytes()); - System.out.println("\t\tsuccess_sync_out_bytes = " + storageStat.getSuccessSyncOutBytes()); - System.out.println("\t\ttotal_file_open_count = " + storageStat.getTotalFileOpenCount()); - System.out.println("\t\tsuccess_file_open_count = " + storageStat.getSuccessFileOpenCount()); - System.out.println("\t\ttotal_file_read_count = " + storageStat.getTotalFileReadCount()); - System.out.println("\t\tsuccess_file_read_count = " + storageStat.getSuccessFileReadCount()); - System.out.println("\t\ttotal_file_write_count = " + storageStat.getTotalFileWriteCount()); - System.out.println("\t\tsuccess_file_write_count = " + storageStat.getSuccessFileWriteCount()); - System.out.println("\t\tlast_heart_beat_time = " + df.format(storageStat.getLastHeartBeatTime())); - System.out.println("\t\tlast_source_update = " + df.format(storageStat.getLastSourceUpdate())); - System.out.println("\t\tlast_sync_update = " + df.format(storageStat.getLastSyncUpdate())); - System.out.println("\t\tlast_synced_timestamp = " + df.format(storageStat.getLastSyncedTimestamp()) + getSyncedDelayString(storageStats, storageStat)); - } - } - - trackerServer.close(); - } - catch(Exception ex) - { - ex.printStackTrace(); - } - } - - protected static String getSyncedDelayString(StructStorageStat[] storageStats, StructStorageStat currentStorageStat) - { - long maxLastSourceUpdate = 0; - for (StructStorageStat storageStat : storageStats) - { - if (storageStat != currentStorageStat && storageStat.getLastSourceUpdate().getTime() > maxLastSourceUpdate) - { - maxLastSourceUpdate = storageStat.getLastSourceUpdate().getTime(); - } - } - - if (maxLastSourceUpdate == 0) - { - return ""; - } - if (currentStorageStat.getLastSyncedTimestamp().getTime() == 0) - { - return " (never synced)"; - } - - int delaySeconds = (int)((maxLastSourceUpdate - currentStorageStat.getLastSyncedTimestamp().getTime()) / 1000); - int day = delaySeconds / (24 * 3600); - int remainSeconds = delaySeconds % (24 * 3600); - int hour = remainSeconds / 3600; - remainSeconds %= 3600; - int minute = remainSeconds / 60; - int second = remainSeconds % 60; - String delayTimeStr; - if (day != 0) - { - delayTimeStr = String.format("%1$d days %2$02dh:%3$02dm:%4$02ds", day, hour, minute, second); - } - else if (hour != 0) - { - delayTimeStr = String.format("%1$02dh:%2$02dm:%3$02ds", hour, minute, second); - } - else if (minute != 0) - { - delayTimeStr = String.format("%1$02dm:%2$02ds", minute, second); - } - else - { - delayTimeStr = String.format("%1$ds", second); - } - - return " (" + delayTimeStr + " delay)"; - } + TrackerServer trackerServer = tracker.getConnection(); + if (trackerServer == null) { + return; + } + + int count; + StructGroupStat[] groupStats = tracker.listGroups(trackerServer); + if (groupStats == null) { + System.out.println(""); + System.out.println("ERROR! list groups error, error no: " + tracker.getErrorCode()); + System.out.println(""); + return; + } + + System.out.println("group count: " + groupStats.length); + + count = 0; + for (StructGroupStat groupStat : groupStats) { + count++; + System.out.println("Group " + count + ":"); + System.out.println("group name = " + groupStat.getGroupName()); + System.out.println("disk total space = " + groupStat.getTotalMB() + "MB"); + System.out.println("disk free space = " + groupStat.getFreeMB() + " MB"); + System.out.println("trunk free space = " + groupStat.getTrunkFreeMB() + " MB"); + System.out.println("storage server count = " + groupStat.getStorageCount()); + System.out.println("active server count = " + groupStat.getActiveCount()); + System.out.println("storage server port = " + groupStat.getStoragePort()); + System.out.println("storage HTTP port = " + groupStat.getStorageHttpPort()); + System.out.println("store path count = " + groupStat.getStorePathCount()); + System.out.println("subdir count per path = " + groupStat.getSubdirCountPerPath()); + System.out.println("current write server index = " + groupStat.getCurrentWriteServer()); + System.out.println("current trunk file id = " + groupStat.getCurrentTrunkFileId()); + + StructStorageStat[] storageStats = tracker.listStorages(trackerServer, groupStat.getGroupName()); + if (storageStats == null) { + System.out.println(""); + System.out.println("ERROR! list storage error, error no: " + tracker.getErrorCode()); + System.out.println(""); + break; + } + + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + int stroageCount = 0; + for (StructStorageStat storageStat : storageStats) { + stroageCount++; + System.out.println("\tStorage " + stroageCount + ":"); + System.out.println("\t\tstorage id = " + storageStat.getId()); + System.out.println("\t\tip_addr = " + storageStat.getIpAddr() + " " + ProtoCommon.getStorageStatusCaption(storageStat.getStatus())); + System.out.println("\t\thttp domain = " + storageStat.getDomainName()); + System.out.println("\t\tversion = " + storageStat.getVersion()); + System.out.println("\t\tjoin time = " + df.format(storageStat.getJoinTime())); + System.out.println("\t\tup time = " + (storageStat.getUpTime().getTime() == 0 ? "" : df.format(storageStat.getUpTime()))); + System.out.println("\t\ttotal storage = " + storageStat.getTotalMB() + "MB"); + System.out.println("\t\tfree storage = " + storageStat.getFreeMB() + "MB"); + System.out.println("\t\tupload priority = " + storageStat.getUploadPriority()); + System.out.println("\t\tstore_path_count = " + storageStat.getStorePathCount()); + System.out.println("\t\tsubdir_count_per_path = " + storageStat.getSubdirCountPerPath()); + System.out.println("\t\tstorage_port = " + storageStat.getStoragePort()); + System.out.println("\t\tstorage_http_port = " + storageStat.getStorageHttpPort()); + System.out.println("\t\tcurrent_write_path = " + storageStat.getCurrentWritePath()); + System.out.println("\t\tsource ip_addr = " + storageStat.getSrcIpAddr()); + System.out.println("\t\tif_trunk_server = " + storageStat.isTrunkServer()); + System.out.println("\t\tconntion.alloc_count = " + storageStat.getConnectionAllocCount()); + System.out.println("\t\tconntion.current_count = " + storageStat.getConnectionCurrentCount()); + System.out.println("\t\tconntion.max_count = " + storageStat.getConnectionMaxCount()); + System.out.println("\t\ttotal_upload_count = " + storageStat.getTotalUploadCount()); + System.out.println("\t\tsuccess_upload_count = " + storageStat.getSuccessUploadCount()); + System.out.println("\t\ttotal_append_count = " + storageStat.getTotalAppendCount()); + System.out.println("\t\tsuccess_append_count = " + storageStat.getSuccessAppendCount()); + System.out.println("\t\ttotal_modify_count = " + storageStat.getTotalModifyCount()); + System.out.println("\t\tsuccess_modify_count = " + storageStat.getSuccessModifyCount()); + System.out.println("\t\ttotal_truncate_count = " + storageStat.getTotalTruncateCount()); + System.out.println("\t\tsuccess_truncate_count = " + storageStat.getSuccessTruncateCount()); + System.out.println("\t\ttotal_set_meta_count = " + storageStat.getTotalSetMetaCount()); + System.out.println("\t\tsuccess_set_meta_count = " + storageStat.getSuccessSetMetaCount()); + System.out.println("\t\ttotal_delete_count = " + storageStat.getTotalDeleteCount()); + System.out.println("\t\tsuccess_delete_count = " + storageStat.getSuccessDeleteCount()); + System.out.println("\t\ttotal_download_count = " + storageStat.getTotalDownloadCount()); + System.out.println("\t\tsuccess_download_count = " + storageStat.getSuccessDownloadCount()); + System.out.println("\t\ttotal_get_meta_count = " + storageStat.getTotalGetMetaCount()); + System.out.println("\t\tsuccess_get_meta_count = " + storageStat.getSuccessGetMetaCount()); + System.out.println("\t\ttotal_create_link_count = " + storageStat.getTotalCreateLinkCount()); + System.out.println("\t\tsuccess_create_link_count = " + storageStat.getSuccessCreateLinkCount()); + System.out.println("\t\ttotal_delete_link_count = " + storageStat.getTotalDeleteLinkCount()); + System.out.println("\t\tsuccess_delete_link_count = " + storageStat.getSuccessDeleteLinkCount()); + System.out.println("\t\ttotal_upload_bytes = " + storageStat.getTotalUploadBytes()); + System.out.println("\t\tsuccess_upload_bytes = " + storageStat.getSuccessUploadBytes()); + System.out.println("\t\ttotal_append_bytes = " + storageStat.getTotalAppendBytes()); + System.out.println("\t\tsuccess_append_bytes = " + storageStat.getSuccessAppendBytes()); + System.out.println("\t\ttotal_modify_bytes = " + storageStat.getTotalModifyBytes()); + System.out.println("\t\tsuccess_modify_bytes = " + storageStat.getSuccessModifyBytes()); + System.out.println("\t\ttotal_download_bytes = " + storageStat.getTotalDownloadloadBytes()); + System.out.println("\t\tsuccess_download_bytes = " + storageStat.getSuccessDownloadloadBytes()); + System.out.println("\t\ttotal_sync_in_bytes = " + storageStat.getTotalSyncInBytes()); + System.out.println("\t\tsuccess_sync_in_bytes = " + storageStat.getSuccessSyncInBytes()); + System.out.println("\t\ttotal_sync_out_bytes = " + storageStat.getTotalSyncOutBytes()); + System.out.println("\t\tsuccess_sync_out_bytes = " + storageStat.getSuccessSyncOutBytes()); + System.out.println("\t\ttotal_file_open_count = " + storageStat.getTotalFileOpenCount()); + System.out.println("\t\tsuccess_file_open_count = " + storageStat.getSuccessFileOpenCount()); + System.out.println("\t\ttotal_file_read_count = " + storageStat.getTotalFileReadCount()); + System.out.println("\t\tsuccess_file_read_count = " + storageStat.getSuccessFileReadCount()); + System.out.println("\t\ttotal_file_write_count = " + storageStat.getTotalFileWriteCount()); + System.out.println("\t\tsuccess_file_write_count = " + storageStat.getSuccessFileWriteCount()); + System.out.println("\t\tlast_heart_beat_time = " + df.format(storageStat.getLastHeartBeatTime())); + System.out.println("\t\tlast_source_update = " + df.format(storageStat.getLastSourceUpdate())); + System.out.println("\t\tlast_sync_update = " + df.format(storageStat.getLastSyncUpdate())); + System.out.println("\t\tlast_synced_timestamp = " + df.format(storageStat.getLastSyncedTimestamp()) + getSyncedDelayString(storageStats, storageStat)); + } + } + + trackerServer.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + protected static String getSyncedDelayString(StructStorageStat[] storageStats, StructStorageStat currentStorageStat) { + long maxLastSourceUpdate = 0; + for (StructStorageStat storageStat : storageStats) { + if (storageStat != currentStorageStat && storageStat.getLastSourceUpdate().getTime() > maxLastSourceUpdate) { + maxLastSourceUpdate = storageStat.getLastSourceUpdate().getTime(); + } + } + + if (maxLastSourceUpdate == 0) { + return ""; + } + + if (currentStorageStat.getLastSyncedTimestamp().getTime() == 0) { + return " (never synced)"; + } + + int delaySeconds = (int) ((maxLastSourceUpdate - currentStorageStat.getLastSyncedTimestamp().getTime()) / 1000); + int day = delaySeconds / (24 * 3600); + int remainSeconds = delaySeconds % (24 * 3600); + int hour = remainSeconds / 3600; + remainSeconds %= 3600; + int minute = remainSeconds / 60; + int second = remainSeconds % 60; + String delayTimeStr; + if (day != 0) { + delayTimeStr = String.format("%1$d days %2$02dh:%3$02dm:%4$02ds", day, hour, minute, second); + } else if (hour != 0) { + delayTimeStr = String.format("%1$02dh:%2$02dm:%3$02ds", hour, minute, second); + } else if (minute != 0) { + delayTimeStr = String.format("%1$02dm:%2$02ds", minute, second); + } else { + delayTimeStr = String.format("%1$ds", second); + } + + return " (" + delayTimeStr + " delay)"; + } } diff --git a/src/main/java/org/csource/fastdfs/test/Test.java b/src/main/java/org/csource/fastdfs/test/Test.java index 13e403e..d97f974 100644 --- a/src/main/java/org/csource/fastdfs/test/Test.java +++ b/src/main/java/org/csource/fastdfs/test/Test.java @@ -1,77 +1,69 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs.test; -import java.io.*; -import java.net.*; -import java.util.*; -import org.csource.common.*; +import org.csource.common.NameValuePair; import org.csource.fastdfs.*; /** -* client test -* @author Happy Fish / YuQing -* @version Version 1.18 -*/ -public class Test -{ - private Test() - { - } - - /** - * entry point - * @param args comand arguments - *

- * - */ - public static void main(String args[]) - { - if (args.length < 2) - { - System.out.println("Error: Must have 2 parameters, one is config filename, " - + "the other is the local filename to upload"); - return; - } - - System.out.println("java.version=" + System.getProperty("java.version")); - - String conf_filename = args[0]; - String local_filename = args[1]; - - try - { - ClientGlobal.init(conf_filename); - System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); - System.out.println("charset=" + ClientGlobal.g_charset); - - TrackerClient tracker = new TrackerClient(); - TrackerServer trackerServer = tracker.getConnection(); - StorageServer storageServer = null; - StorageClient1 client = new StorageClient1(trackerServer, storageServer); + * client test + * + * @author Happy Fish / YuQing + * @version Version 1.18 + */ +public class Test { + private Test() { + } - NameValuePair[] metaList = new NameValuePair[1]; - metaList[0] = new NameValuePair("fileName", local_filename); - String fileId = client.upload_file1(local_filename, null, metaList); - System.out.println("upload success. file id is: " + fileId); + /** + * entry point + * + * @param args comand arguments + * + * + */ + public static void main(String args[]) { + if (args.length < 2) { + System.out.println("Error: Must have 2 parameters, one is config filename, " + + "the other is the local filename to upload"); + return; + } - int i = 0; - while (i++ < 10) { - byte[] result = client.download_file1(fileId); - System.out.println(i + ", download result is: " + result.length); - } - - trackerServer.close(); - } - catch(Exception ex) - { - ex.printStackTrace(); - } + System.out.println("java.version=" + System.getProperty("java.version")); + + String conf_filename = args[0]; + String local_filename = args[1]; + + try { + ClientGlobal.init(conf_filename); + System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + System.out.println("charset=" + ClientGlobal.g_charset); + + TrackerClient tracker = new TrackerClient(); + TrackerServer trackerServer = tracker.getConnection(); + StorageServer storageServer = null; + StorageClient1 client = new StorageClient1(trackerServer, storageServer); + + NameValuePair[] metaList = new NameValuePair[1]; + metaList[0] = new NameValuePair("fileName", local_filename); + String fileId = client.upload_file1(local_filename, null, metaList); + System.out.println("upload success. file id is: " + fileId); + + int i = 0; + while (i++ < 10) { + byte[] result = client.download_file1(fileId); + System.out.println(i + ", download result is: " + result.length); + } + + trackerServer.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } } } diff --git a/src/main/java/org/csource/fastdfs/test/Test1.java b/src/main/java/org/csource/fastdfs/test/Test1.java index 53a8673..f236fd9 100644 --- a/src/main/java/org/csource/fastdfs/test/Test1.java +++ b/src/main/java/org/csource/fastdfs/test/Test1.java @@ -1,59 +1,47 @@ - package org.csource.fastdfs.test; -import java.io.*; -import java.net.*; -import java.util.*; -import org.csource.common.*; +import org.csource.common.NameValuePair; import org.csource.fastdfs.*; -public class Test1 -{ - public static void main(String args[]) - { - try - { - ClientGlobal.init("fdfs_client.conf"); - System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); - System.out.println("charset=" + ClientGlobal.g_charset); +import java.net.InetSocketAddress; - TrackerGroup tg = new TrackerGroup(new InetSocketAddress[]{new InetSocketAddress("10.0.11.243", 22122)}); - TrackerClient tc = new TrackerClient(tg); +public class Test1 { + public static void main(String args[]) { + try { + ClientGlobal.init("fdfs_client.conf"); + System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + System.out.println("charset=" + ClientGlobal.g_charset); - TrackerServer ts = tc.getConnection(); - if (ts == null) - { - System.out.println("getConnection return null"); - return; - } + TrackerGroup tg = new TrackerGroup(new InetSocketAddress[]{new InetSocketAddress("10.0.11.243", 22122)}); + TrackerClient tc = new TrackerClient(tg); - StorageServer ss = tc.getStoreStorage(ts); - if (ss == null) - { - System.out.println("getStoreStorage return null"); - } + TrackerServer ts = tc.getConnection(); + if (ts == null) { + System.out.println("getConnection return null"); + return; + } - StorageClient1 sc1 = new StorageClient1(ts, ss); + StorageServer ss = tc.getStoreStorage(ts); + if (ss == null) { + System.out.println("getStoreStorage return null"); + } - NameValuePair[] meta_list = null; //new NameValuePair[0]; - String item; - String fileid; - if (System.getProperty("os.name").equalsIgnoreCase("windows")) - { - item = "c:/windows/system32/notepad.exe"; - fileid = sc1.upload_file1(item, "exe", meta_list); - } - else - { - item = "/etc/hosts"; - fileid = sc1.upload_file1(item, "", meta_list); - } + StorageClient1 sc1 = new StorageClient1(ts, ss); - System.out.println("Upload local file "+item+" ok, fileid="+fileid); - } - catch(Exception ex) - { - ex.printStackTrace(); - } - } + NameValuePair[] meta_list = null; //new NameValuePair[0]; + String item; + String fileid; + if (System.getProperty("os.name").equalsIgnoreCase("windows")) { + item = "c:/windows/system32/notepad.exe"; + fileid = sc1.upload_file1(item, "exe", meta_list); + } else { + item = "/etc/hosts"; + fileid = sc1.upload_file1(item, "", meta_list); + } + + System.out.println("Upload local file " + item + " ok, fileid=" + fileid); + } catch (Exception ex) { + ex.printStackTrace(); + } + } } diff --git a/src/main/java/org/csource/fastdfs/test/TestAppender.java b/src/main/java/org/csource/fastdfs/test/TestAppender.java index 99e097d..4a3d04e 100644 --- a/src/main/java/org/csource/fastdfs/test/TestAppender.java +++ b/src/main/java/org/csource/fastdfs/test/TestAppender.java @@ -1,67 +1,64 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs.test; -import java.io.*; -import java.net.*; -import java.util.*; -import org.csource.common.*; +import org.csource.common.NameValuePair; import org.csource.fastdfs.*; +import java.io.File; +import java.net.InetSocketAddress; + /** -* client test -* @author Happy Fish / YuQing -* @version Version 1.20 -*/ -public class TestAppender -{ - private TestAppender() - { - } - - /** - * entry point - * @param args comand arguments - *

- * - */ - public static void main(String args[]) - { - if (args.length < 2) - { - System.out.println("Error: Must have 2 parameters, one is config filename, " - + "the other is the local filename to upload"); - return; - } - - System.out.println("java.version=" + System.getProperty("java.version")); - - String conf_filename = args[0]; - String local_filename = args[1]; - - try - { - ClientGlobal.init(conf_filename); - System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); - System.out.println("charset=" + ClientGlobal.g_charset); - - long startTime; - String group_name; - String remote_filename; - ServerInfo[] servers; - TrackerClient tracker = new TrackerClient(); - TrackerServer trackerServer = tracker.getConnection(); - - StorageServer storageServer = null; - + * client test + * + * @author Happy Fish / YuQing + * @version Version 1.20 + */ +public class TestAppender { + private TestAppender() { + } + + /** + * entry point + * + * @param args comand arguments + * + * + */ + public static void main(String args[]) { + if (args.length < 2) { + System.out.println("Error: Must have 2 parameters, one is config filename, " + + "the other is the local filename to upload"); + return; + } + + System.out.println("java.version=" + System.getProperty("java.version")); + + String conf_filename = args[0]; + String local_filename = args[1]; + + try { + ClientGlobal.init(conf_filename); + System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + System.out.println("charset=" + ClientGlobal.g_charset); + + long startTime; + String group_name; + String remote_filename; + ServerInfo[] servers; + TrackerClient tracker = new TrackerClient(); + TrackerServer trackerServer = tracker.getConnection(); + + StorageServer storageServer = null; + /* - storageServer = tracker.getStoreStorage(trackerServer); + storageServer = tracker.getStoreStorage(trackerServer); if (storageServer == null) { System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode()); @@ -69,290 +66,237 @@ public class TestAppender } */ - StorageClient client = new StorageClient(trackerServer, storageServer); - byte[] file_buff; - NameValuePair[] meta_list; - String[] results; - String appender_filename; - String file_ext_name; - int errno; + StorageClient client = new StorageClient(trackerServer, storageServer); + byte[] file_buff; + NameValuePair[] meta_list; + String[] results; + String appender_filename; + String file_ext_name; + int errno; - meta_list = new NameValuePair[4]; - meta_list[0] = new NameValuePair("width", "800"); - meta_list[1] = new NameValuePair("heigth", "600"); - meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF"); - meta_list[3] = new NameValuePair("author", "Mike"); - - file_buff = "this is a test".getBytes(ClientGlobal.g_charset); - System.out.println("file length: " + file_buff.length); + meta_list = new NameValuePair[4]; + meta_list[0] = new NameValuePair("width", "800"); + meta_list[1] = new NameValuePair("heigth", "600"); + meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF"); + meta_list[3] = new NameValuePair("author", "Mike"); + + file_buff = "this is a test".getBytes(ClientGlobal.g_charset); + System.out.println("file length: " + file_buff.length); + + group_name = null; + StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name); + if (storageServers == null) { + System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode()); + } else { + System.err.println("store storage servers count: " + storageServers.length); + for (int k = 0; k < storageServers.length; k++) { + System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort()); + } + System.err.println(""); + } + + startTime = System.currentTimeMillis(); + results = client.upload_appender_file(file_buff, "txt", meta_list); + System.out.println("upload_appender_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - group_name = null; - StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name); - if (storageServers == null) - { - System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode()); - } - else - { - System.err.println("store storage servers count: " + storageServers.length); - for (int k=0; k 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) - { - file_ext_name = local_filename.substring(nPos+1); - } - else - { - file_ext_name = null; - } - - results = client.upload_appender_file(null, f.length(), - new UploadLocalFileSender(local_filename), file_ext_name, meta_list); - if (results != null) - { - group_name = results[0]; - remote_filename = results[1]; - - System.out.println("group name: " + group_name + ", remote filename: " + remote_filename); - System.out.println(client.get_file_info(group_name, remote_filename)); - - appender_filename = remote_filename; - startTime = System.currentTimeMillis(); - errno = client.append_file(group_name, appender_filename, f.length(), new UploadLocalFileSender(local_filename)); - System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - if (errno == 0) - { - System.err.println(client.get_file_info(group_name, appender_filename)); - } - else - { - System.err.println("append file fail, error no: " + errno); - } - - startTime = System.currentTimeMillis(); - errno = client.modify_file(group_name, appender_filename, 0, f.length(), new UploadLocalFileSender(local_filename)); - System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - if (errno == 0) - { - System.err.println(client.get_file_info(group_name, appender_filename)); - } - else - { - System.err.println("modify file fail, error no: " + errno); - } - - startTime = System.currentTimeMillis(); - errno = client.truncate_file(group_name, appender_filename); - System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - if (errno == 0) - { - System.err.println(client.get_file_info(group_name, appender_filename)); - } - else - { - System.err.println("truncate file fail, error no: " + errno); - } - } - else - { - System.err.println("Upload file fail, error no: " + errno); - } - - storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename); - if (storageServer == null) - { - System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode()); - return; - } + servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename); + if (servers == null) { + System.err.println("get storage servers fail, error code: " + tracker.getErrorCode()); + } else { + System.err.println("storage servers count: " + servers.length); + for (int k = 0; k < servers.length; k++) { + System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort()); + } + System.err.println(""); + } + + meta_list = new NameValuePair[4]; + meta_list[0] = new NameValuePair("width", "1024"); + meta_list[1] = new NameValuePair("heigth", "768"); + meta_list[2] = new NameValuePair("bgcolor", "#000000"); + meta_list[3] = new NameValuePair("title", "Untitle"); + + startTime = System.currentTimeMillis(); + errno = client.set_metadata(group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE); + System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println("set_metadata success"); + } else { + System.err.println("set_metadata fail, error no: " + errno); + } + + meta_list = client.get_metadata(group_name, remote_filename); + if (meta_list != null) { + for (int i = 0; i < meta_list.length; i++) { + System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue()); + } + } + + //Thread.sleep(30000); + + startTime = System.currentTimeMillis(); + file_buff = client.download_file(group_name, remote_filename); + System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + + if (file_buff != null) { + System.out.println("file length:" + file_buff.length); + System.out.println((new String(file_buff))); + } + + file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset); + appender_filename = remote_filename; + file_ext_name = "txt"; + startTime = System.currentTimeMillis(); + errno = client.append_file(group_name, appender_filename, file_buff); + System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info(group_name, appender_filename)); + } else { + System.err.println("append file fail, error no: " + errno); + } + + startTime = System.currentTimeMillis(); + errno = client.delete_file(group_name, remote_filename); + System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println("Delete file success"); + } else { + System.err.println("Delete file fail, error no: " + errno); + } + } + + results = client.upload_appender_file(local_filename, null, meta_list); + if (results != null) { + String file_id; + int ts; + String token; + String file_url; + InetSocketAddress inetSockAddr; + + group_name = results[0]; + remote_filename = results[1]; + file_id = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename; + + inetSockAddr = trackerServer.getInetSocketAddress(); + file_url = "http://" + inetSockAddr.getAddress().getHostAddress(); + if (ClientGlobal.g_tracker_http_port != 80) { + file_url += ":" + ClientGlobal.g_tracker_http_port; + } + file_url += "/" + file_id; + if (ClientGlobal.g_anti_steal_token) { + ts = (int) (System.currentTimeMillis() / 1000); + token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key); + file_url += "?token=" + token + "&ts=" + ts; + } + + System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename); + System.err.println(client.get_file_info(group_name, remote_filename)); + System.err.println("file url: " + file_url); + + errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_")); + if (errno == 0) { + System.err.println("Download file success"); + } else { + System.err.println("Download file fail, error no: " + errno); + } + + errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.replaceAll("/", "-"))); + if (errno == 0) { + System.err.println("Download file success"); + } else { + System.err.println("Download file fail, error no: " + errno); + } + + appender_filename = remote_filename; + file_ext_name = null; + startTime = System.currentTimeMillis(); + errno = client.append_file(group_name, appender_filename, local_filename); + System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info(group_name, appender_filename)); + } else { + System.err.println("append file fail, error no: " + errno); + } + } + + File f; + f = new File(local_filename); + int nPos = local_filename.lastIndexOf('.'); + if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) { + file_ext_name = local_filename.substring(nPos + 1); + } else { + file_ext_name = null; + } + + results = client.upload_appender_file(null, f.length(), + new UploadLocalFileSender(local_filename), file_ext_name, meta_list); + if (results != null) { + group_name = results[0]; + remote_filename = results[1]; + + System.out.println("group name: " + group_name + ", remote filename: " + remote_filename); + System.out.println(client.get_file_info(group_name, remote_filename)); + + appender_filename = remote_filename; + startTime = System.currentTimeMillis(); + errno = client.append_file(group_name, appender_filename, f.length(), new UploadLocalFileSender(local_filename)); + System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info(group_name, appender_filename)); + } else { + System.err.println("append file fail, error no: " + errno); + } + + startTime = System.currentTimeMillis(); + errno = client.modify_file(group_name, appender_filename, 0, f.length(), new UploadLocalFileSender(local_filename)); + System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info(group_name, appender_filename)); + } else { + System.err.println("modify file fail, error no: " + errno); + } + + startTime = System.currentTimeMillis(); + errno = client.truncate_file(group_name, appender_filename); + System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info(group_name, appender_filename)); + } else { + System.err.println("truncate file fail, error no: " + errno); + } + } else { + System.err.println("Upload file fail, error no: " + errno); + } + + storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename); + if (storageServer == null) { + System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode()); + return; + } /* for test only */ - System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket())); - - storageServer.close(); - + System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket())); + + storageServer.close(); + /* for test only */ - System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket())); - - trackerServer.close(); - } - catch(Exception ex) - { - ex.printStackTrace(); - } + System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket())); + + trackerServer.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } } } diff --git a/src/main/java/org/csource/fastdfs/test/TestAppender1.java b/src/main/java/org/csource/fastdfs/test/TestAppender1.java index 393cad7..e64e78e 100644 --- a/src/main/java/org/csource/fastdfs/test/TestAppender1.java +++ b/src/main/java/org/csource/fastdfs/test/TestAppender1.java @@ -1,65 +1,62 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs.test; -import java.io.*; -import java.net.*; -import java.util.*; -import org.csource.common.*; +import org.csource.common.NameValuePair; import org.csource.fastdfs.*; +import java.io.File; +import java.net.InetSocketAddress; + /** -* client test -* @author Happy Fish / YuQing -* @version Version 1.20 -*/ -public class TestAppender1 -{ - private TestAppender1() - { - } - - /** - * entry point - * @param args comand arguments - *

- * - */ - public static void main(String args[]) - { - if (args.length < 2) - { - System.out.println("Error: Must have 2 parameters, one is config filename, " - + "the other is the local filename to upload"); - return; - } - - System.out.println("java.version=" + System.getProperty("java.version")); - - String conf_filename = args[0]; - String local_filename = args[1]; - - try - { - ClientGlobal.init(conf_filename); - System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); - System.out.println("charset=" + ClientGlobal.g_charset); - - long startTime; - ServerInfo[] servers; - TrackerClient tracker = new TrackerClient(); - TrackerServer trackerServer = tracker.getConnection(); - - StorageServer storageServer = null; - + * client test + * + * @author Happy Fish / YuQing + * @version Version 1.20 + */ +public class TestAppender1 { + private TestAppender1() { + } + + /** + * entry point + * + * @param args comand arguments + * + * + */ + public static void main(String args[]) { + if (args.length < 2) { + System.out.println("Error: Must have 2 parameters, one is config filename, " + + "the other is the local filename to upload"); + return; + } + + System.out.println("java.version=" + System.getProperty("java.version")); + + String conf_filename = args[0]; + String local_filename = args[1]; + + try { + ClientGlobal.init(conf_filename); + System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + System.out.println("charset=" + ClientGlobal.g_charset); + + long startTime; + ServerInfo[] servers; + TrackerClient tracker = new TrackerClient(); + TrackerServer trackerServer = tracker.getConnection(); + + StorageServer storageServer = null; + /* - storageServer = tracker.getStoreStorage(trackerServer); + storageServer = tracker.getStoreStorage(trackerServer); if (storageServer == null) { System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode()); @@ -67,272 +64,219 @@ public class TestAppender1 } */ - StorageClient1 client = new StorageClient1(trackerServer, storageServer); - byte[] file_buff; - NameValuePair[] meta_list; - String group_name; - String appender_file_id; - String file_ext_name; - int errno; + StorageClient1 client = new StorageClient1(trackerServer, storageServer); + byte[] file_buff; + NameValuePair[] meta_list; + String group_name; + String appender_file_id; + String file_ext_name; + int errno; - meta_list = new NameValuePair[4]; - meta_list[0] = new NameValuePair("width", "800"); - meta_list[1] = new NameValuePair("heigth", "600"); - meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF"); - meta_list[3] = new NameValuePair("author", "Mike"); - - file_buff = "this is a test".getBytes(ClientGlobal.g_charset); - System.out.println("file length: " + file_buff.length); + meta_list = new NameValuePair[4]; + meta_list[0] = new NameValuePair("width", "800"); + meta_list[1] = new NameValuePair("heigth", "600"); + meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF"); + meta_list[3] = new NameValuePair("author", "Mike"); + + file_buff = "this is a test".getBytes(ClientGlobal.g_charset); + System.out.println("file length: " + file_buff.length); + + group_name = null; + StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name); + if (storageServers == null) { + System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode()); + } else { + System.err.println("store storage servers count: " + storageServers.length); + for (int k = 0; k < storageServers.length; k++) { + System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort()); + } + System.err.println(""); + } + + startTime = System.currentTimeMillis(); + appender_file_id = client.upload_appender_file1(file_buff, "txt", meta_list); + System.out.println("upload_appender_file1 time used: " + (System.currentTimeMillis() - startTime) + " ms"); - group_name = null; - StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name); - if (storageServers == null) - { - System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode()); - } - else - { - System.err.println("store storage servers count: " + storageServers.length); - for (int k=0; k 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) - { - file_ext_name = local_filename.substring(nPos+1); - } - else - { - file_ext_name = null; - } - - appender_file_id = client.upload_appender_file1(null, f.length(), - new UploadLocalFileSender(local_filename), file_ext_name, meta_list); - if (appender_file_id != null) - { - System.out.println(client.get_file_info1(appender_file_id)); - - startTime = System.currentTimeMillis(); - errno = client.append_file1(appender_file_id, f.length(), new UploadLocalFileSender(local_filename)); - System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - if (errno == 0) - { - System.err.println(client.get_file_info1(appender_file_id)); - } - else - { - System.err.println("append file fail, error no: " + errno); - } - - startTime = System.currentTimeMillis(); - errno = client.modify_file1(appender_file_id, 0, f.length(), new UploadLocalFileSender(local_filename)); - System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - if (errno == 0) - { - System.err.println(client.get_file_info1(appender_file_id)); - } - else - { - System.err.println("modify file fail, error no: " + errno); - } - - startTime = System.currentTimeMillis(); - errno = client.truncate_file1(appender_file_id, 0); - System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - if (errno == 0) - { - System.err.println(client.get_file_info1(appender_file_id)); - } - else - { - System.err.println("truncate file fail, error no: " + errno); - } - } - else - { - System.err.println("Upload file fail, error no: " + errno); - } - - storageServer = tracker.getFetchStorage1(trackerServer, appender_file_id); - if (storageServer == null) - { - System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode()); - return; - } + servers = tracker.getFetchStorages1(trackerServer, appender_file_id); + if (servers == null) { + System.err.println("get storage servers fail, error code: " + tracker.getErrorCode()); + } else { + System.err.println("storage servers count: " + servers.length); + for (int k = 0; k < servers.length; k++) { + System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort()); + } + System.err.println(""); + } + + meta_list = new NameValuePair[4]; + meta_list[0] = new NameValuePair("width", "1024"); + meta_list[1] = new NameValuePair("heigth", "768"); + meta_list[2] = new NameValuePair("bgcolor", "#000000"); + meta_list[3] = new NameValuePair("title", "Untitle"); + + startTime = System.currentTimeMillis(); + errno = client.set_metadata1(appender_file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE); + System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println("set_metadata success"); + } else { + System.err.println("set_metadata fail, error no: " + errno); + } + + meta_list = client.get_metadata1(appender_file_id); + if (meta_list != null) { + for (int i = 0; i < meta_list.length; i++) { + System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue()); + } + } + + startTime = System.currentTimeMillis(); + file_buff = client.download_file1(appender_file_id); + System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + + if (file_buff != null) { + System.out.println("file length:" + file_buff.length); + System.out.println((new String(file_buff))); + } + + file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset); + file_ext_name = "txt"; + startTime = System.currentTimeMillis(); + errno = client.append_file1(appender_file_id, file_buff); + System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info1(appender_file_id)); + } else { + System.err.println("append file fail, error no: " + errno); + } + + startTime = System.currentTimeMillis(); + errno = client.delete_file1(appender_file_id); + System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println("Delete file success"); + } else { + System.err.println("Delete file fail, error no: " + errno); + } + } + + appender_file_id = client.upload_appender_file1(local_filename, null, meta_list); + if (appender_file_id != null) { + int ts; + String token; + String file_url; + InetSocketAddress inetSockAddr; + + inetSockAddr = trackerServer.getInetSocketAddress(); + file_url = "http://" + inetSockAddr.getAddress().getHostAddress(); + if (ClientGlobal.g_tracker_http_port != 80) { + file_url += ":" + ClientGlobal.g_tracker_http_port; + } + file_url += "/" + appender_file_id; + if (ClientGlobal.g_anti_steal_token) { + ts = (int) (System.currentTimeMillis() / 1000); + token = ProtoCommon.getToken(appender_file_id, ts, ClientGlobal.g_secret_key); + file_url += "?token=" + token + "&ts=" + ts; + } + + System.err.println(client.get_file_info1(appender_file_id)); + System.err.println("file url: " + file_url); + + errno = client.download_file1(appender_file_id, 0, 0, "c:\\" + appender_file_id.replaceAll("/", "_")); + if (errno == 0) { + System.err.println("Download file success"); + } else { + System.err.println("Download file fail, error no: " + errno); + } + + errno = client.download_file1(appender_file_id, 0, 0, new DownloadFileWriter("c:\\" + appender_file_id.replaceAll("/", "-"))); + if (errno == 0) { + System.err.println("Download file success"); + } else { + System.err.println("Download file fail, error no: " + errno); + } + + file_ext_name = null; + startTime = System.currentTimeMillis(); + errno = client.append_file1(appender_file_id, local_filename); + System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info1(appender_file_id)); + } else { + System.err.println("append file fail, error no: " + errno); + } + } + + File f; + f = new File(local_filename); + int nPos = local_filename.lastIndexOf('.'); + if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) { + file_ext_name = local_filename.substring(nPos + 1); + } else { + file_ext_name = null; + } + + appender_file_id = client.upload_appender_file1(null, f.length(), + new UploadLocalFileSender(local_filename), file_ext_name, meta_list); + if (appender_file_id != null) { + System.out.println(client.get_file_info1(appender_file_id)); + + startTime = System.currentTimeMillis(); + errno = client.append_file1(appender_file_id, f.length(), new UploadLocalFileSender(local_filename)); + System.out.println("append_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info1(appender_file_id)); + } else { + System.err.println("append file fail, error no: " + errno); + } + + startTime = System.currentTimeMillis(); + errno = client.modify_file1(appender_file_id, 0, f.length(), new UploadLocalFileSender(local_filename)); + System.out.println("modify_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info1(appender_file_id)); + } else { + System.err.println("modify file fail, error no: " + errno); + } + + startTime = System.currentTimeMillis(); + errno = client.truncate_file1(appender_file_id, 0); + System.out.println("truncate_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println(client.get_file_info1(appender_file_id)); + } else { + System.err.println("truncate file fail, error no: " + errno); + } + } else { + System.err.println("Upload file fail, error no: " + errno); + } + + storageServer = tracker.getFetchStorage1(trackerServer, appender_file_id); + if (storageServer == null) { + System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode()); + return; + } /* for test only */ - System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket())); - - storageServer.close(); - + System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket())); + + storageServer.close(); + /* for test only */ - System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket())); - - trackerServer.close(); - } - catch(Exception ex) - { - ex.printStackTrace(); - } + System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket())); + + trackerServer.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } } } diff --git a/src/main/java/org/csource/fastdfs/test/TestClient.java b/src/main/java/org/csource/fastdfs/test/TestClient.java index 06afe38..3ef03a4 100644 --- a/src/main/java/org/csource/fastdfs/test/TestClient.java +++ b/src/main/java/org/csource/fastdfs/test/TestClient.java @@ -1,67 +1,64 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs.test; -import java.io.*; -import java.net.*; -import java.util.*; -import org.csource.common.*; +import org.csource.common.NameValuePair; import org.csource.fastdfs.*; +import java.io.File; +import java.net.InetSocketAddress; + /** -* client test -* @author Happy Fish / YuQing -* @version Version 1.18 -*/ -public class TestClient -{ - private TestClient() - { - } - - /** - * entry point - * @param args comand arguments - *

  • args[0]: config filename
- *
  • args[1]: local filename to upload
- */ - public static void main(String args[]) - { - if (args.length < 2) - { - System.out.println("Error: Must have 2 parameters, one is config filename, " - + "the other is the local filename to upload"); - return; - } - - System.out.println("java.version=" + System.getProperty("java.version")); - - String conf_filename = args[0]; - String local_filename = args[1]; - - try - { - ClientGlobal.init(conf_filename); - System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); - System.out.println("charset=" + ClientGlobal.g_charset); - - long startTime; - String group_name; - String remote_filename; - ServerInfo[] servers; - TrackerClient tracker = new TrackerClient(); - TrackerServer trackerServer = tracker.getConnection(); - - StorageServer storageServer = null; - + * client test + * + * @author Happy Fish / YuQing + * @version Version 1.18 + */ +public class TestClient { + private TestClient() { + } + + /** + * entry point + * + * @param args comand arguments + *
  • args[0]: config filename
+ *
  • args[1]: local filename to upload
+ */ + public static void main(String args[]) { + if (args.length < 2) { + System.out.println("Error: Must have 2 parameters, one is config filename, " + + "the other is the local filename to upload"); + return; + } + + System.out.println("java.version=" + System.getProperty("java.version")); + + String conf_filename = args[0]; + String local_filename = args[1]; + + try { + ClientGlobal.init(conf_filename); + System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + System.out.println("charset=" + ClientGlobal.g_charset); + + long startTime; + String group_name; + String remote_filename; + ServerInfo[] servers; + TrackerClient tracker = new TrackerClient(); + TrackerServer trackerServer = tracker.getConnection(); + + StorageServer storageServer = null; + /* - storageServer = tracker.getStoreStorage(trackerServer); + storageServer = tracker.getStoreStorage(trackerServer); if (storageServer == null) { System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode()); @@ -69,283 +66,239 @@ public class TestClient } */ - StorageClient client = new StorageClient(trackerServer, storageServer); - byte[] file_buff; - NameValuePair[] meta_list; - String[] results; - String master_filename; - String prefix_name; - String file_ext_name; - String generated_slave_filename; - int errno; - - meta_list = new NameValuePair[4]; - meta_list[0] = new NameValuePair("width", "800"); - meta_list[1] = new NameValuePair("heigth", "600"); - meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF"); - meta_list[3] = new NameValuePair("author", "Mike"); - - file_buff = "this is a test".getBytes(ClientGlobal.g_charset); - System.out.println("file length: " + file_buff.length); + StorageClient client = new StorageClient(trackerServer, storageServer); + byte[] file_buff; + NameValuePair[] meta_list; + String[] results; + String master_filename; + String prefix_name; + String file_ext_name; + String generated_slave_filename; + int errno; + + meta_list = new NameValuePair[4]; + meta_list[0] = new NameValuePair("width", "800"); + meta_list[1] = new NameValuePair("heigth", "600"); + meta_list[2] = new NameValuePair("bgcolor", "#FFFFFF"); + meta_list[3] = new NameValuePair("author", "Mike"); + + file_buff = "this is a test".getBytes(ClientGlobal.g_charset); + System.out.println("file length: " + file_buff.length); + + group_name = null; + StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name); + if (storageServers == null) { + System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode()); + } else { + System.err.println("store storage servers count: " + storageServers.length); + for (int k = 0; k < storageServers.length; k++) { + System.err.println((k + 1) + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort()); + } + System.err.println(""); + } + + startTime = System.currentTimeMillis(); + results = client.upload_file(file_buff, "txt", meta_list); + System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - group_name = null; - StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name); - if (storageServers == null) - { - System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode()); - } - else - { - System.err.println("store storage servers count: " + storageServers.length); - for (int k=0; k 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) - { - file_ext_name = local_filename.substring(nPos+1); - } - else - { - file_ext_name = null; - } - - results = client.upload_file(null, f.length(), - new UploadLocalFileSender(local_filename), file_ext_name, meta_list); - if (results != null) - { - group_name = results[0]; - remote_filename = results[1]; - - System.out.println("group name: " + group_name + ", remote filename: " + remote_filename); - System.out.println(client.get_file_info(group_name, remote_filename)); - - master_filename = remote_filename; - prefix_name = "-part3"; - startTime = System.currentTimeMillis(); - results = client.upload_file(group_name, master_filename, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list); - System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); - if (results != null) - { - System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]); - - generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name); - if (!generated_slave_filename.equals(results[1])) - { - System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]); - } - - System.err.println(client.get_file_info(results[0], results[1])); - } - } - else - { - System.err.println("Upload file fail, error no: " + errno); - } - - storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename); - if (storageServer == null) - { - System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode()); - return; - } + meta_list = new NameValuePair[4]; + meta_list[0] = new NameValuePair("width", "1024"); + meta_list[1] = new NameValuePair("heigth", "768"); + meta_list[2] = new NameValuePair("bgcolor", "#000000"); + meta_list[3] = new NameValuePair("title", "Untitle"); + + startTime = System.currentTimeMillis(); + errno = client.set_metadata(group_name, remote_filename, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE); + System.out.println("set_metadata time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println("set_metadata success"); + } else { + System.err.println("set_metadata fail, error no: " + errno); + } + + meta_list = client.get_metadata(group_name, remote_filename); + if (meta_list != null) { + for (int i = 0; i < meta_list.length; i++) { + System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue()); + } + } + + //Thread.sleep(30000); + + startTime = System.currentTimeMillis(); + file_buff = client.download_file(group_name, remote_filename); + System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + + if (file_buff != null) { + System.out.println("file length:" + file_buff.length); + System.out.println((new String(file_buff))); + } + + file_buff = "this is a slave buff".getBytes(ClientGlobal.g_charset); + master_filename = remote_filename; + prefix_name = "-part1"; + file_ext_name = "txt"; + startTime = System.currentTimeMillis(); + results = client.upload_file(group_name, master_filename, prefix_name, file_buff, file_ext_name, meta_list); + System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (results != null) { + System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]); + + generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name); + if (!generated_slave_filename.equals(results[1])) { + System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]); + } + + System.err.println(client.get_file_info(results[0], results[1])); + } + + startTime = System.currentTimeMillis(); + errno = client.delete_file(group_name, remote_filename); + System.out.println("delete_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (errno == 0) { + System.err.println("Delete file success"); + } else { + System.err.println("Delete file fail, error no: " + errno); + } + } + + results = client.upload_file(local_filename, null, meta_list); + if (results != null) { + String file_id; + int ts; + String token; + String file_url; + InetSocketAddress inetSockAddr; + + group_name = results[0]; + remote_filename = results[1]; + file_id = group_name + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remote_filename; + + inetSockAddr = trackerServer.getInetSocketAddress(); + file_url = "http://" + inetSockAddr.getAddress().getHostAddress(); + if (ClientGlobal.g_tracker_http_port != 80) { + file_url += ":" + ClientGlobal.g_tracker_http_port; + } + file_url += "/" + file_id; + if (ClientGlobal.g_anti_steal_token) { + ts = (int) (System.currentTimeMillis() / 1000); + token = ProtoCommon.getToken(file_id, ts, ClientGlobal.g_secret_key); + file_url += "?token=" + token + "&ts=" + ts; + } + + System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename); + System.err.println(client.get_file_info(group_name, remote_filename)); + System.err.println("file url: " + file_url); + + errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_")); + if (errno == 0) { + System.err.println("Download file success"); + } else { + System.err.println("Download file fail, error no: " + errno); + } + + errno = client.download_file(group_name, remote_filename, 0, 0, new DownloadFileWriter("c:\\" + remote_filename.replaceAll("/", "-"))); + if (errno == 0) { + System.err.println("Download file success"); + } else { + System.err.println("Download file fail, error no: " + errno); + } + + master_filename = remote_filename; + prefix_name = "-part2"; + file_ext_name = null; + startTime = System.currentTimeMillis(); + results = client.upload_file(group_name, master_filename, prefix_name, local_filename, null, meta_list); + System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (results != null) { + System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]); + + generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name); + if (!generated_slave_filename.equals(results[1])) { + System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]); + } + + System.err.println(client.get_file_info(results[0], results[1])); + } + } + + File f; + f = new File(local_filename); + int nPos = local_filename.lastIndexOf('.'); + if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) { + file_ext_name = local_filename.substring(nPos + 1); + } else { + file_ext_name = null; + } + + results = client.upload_file(null, f.length(), + new UploadLocalFileSender(local_filename), file_ext_name, meta_list); + if (results != null) { + group_name = results[0]; + remote_filename = results[1]; + + System.out.println("group name: " + group_name + ", remote filename: " + remote_filename); + System.out.println(client.get_file_info(group_name, remote_filename)); + + master_filename = remote_filename; + prefix_name = "-part3"; + startTime = System.currentTimeMillis(); + results = client.upload_file(group_name, master_filename, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list); + System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms"); + if (results != null) { + System.err.println("slave file group_name: " + results[0] + ", remote_filename: " + results[1]); + + generated_slave_filename = ProtoCommon.genSlaveFilename(master_filename, prefix_name, file_ext_name); + if (!generated_slave_filename.equals(results[1])) { + System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]); + } + + System.err.println(client.get_file_info(results[0], results[1])); + } + } else { + System.err.println("Upload file fail, error no: " + errno); + } + + storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename); + if (storageServer == null) { + System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode()); + return; + } /* for test only */ - System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket())); - - storageServer.close(); - + System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket())); + + storageServer.close(); + /* for test only */ - System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket())); - - trackerServer.close(); - } - catch(Exception ex) - { - ex.printStackTrace(); - } + System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket())); + + trackerServer.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } } } diff --git a/src/main/java/org/csource/fastdfs/test/TestClient1.java b/src/main/java/org/csource/fastdfs/test/TestClient1.java index 9b47cbb..ff241b8 100644 --- a/src/main/java/org/csource/fastdfs/test/TestClient1.java +++ b/src/main/java/org/csource/fastdfs/test/TestClient1.java @@ -1,313 +1,266 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs.test; -import java.io.*; -import java.net.*; -import java.util.*; -import org.csource.common.*; +import org.csource.common.NameValuePair; import org.csource.fastdfs.*; +import java.io.File; +import java.net.InetSocketAddress; + /** -* client test -* @author Happy Fish / YuQing -* @version Version 1.16 -*/ -public class TestClient1 -{ - private TestClient1() - { - } - - /** - * entry point - * @param args comand arguments - *

  • args[0]: config filename
- *
  • args[1]: local filename to upload
- */ - public static void main(String args[]) - { - if (args.length < 2) - { - System.out.println("Error: Must have 2 parameters, one is config filename, " - + "the other is the local filename to upload"); - return; - } - - System.out.println("java.version=" + System.getProperty("java.version")); - - String conf_filename = args[0]; - String local_filename = args[1]; - String group_name; - - try - { - ClientGlobal.init(conf_filename); - System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); - System.out.println("charset=" + ClientGlobal.g_charset); - - String file_id; - - TrackerClient tracker = new TrackerClient(); - TrackerServer trackerServer = tracker.getConnection(); - - StorageServer storageServer = null; - /* - storageServer = tracker.getStoreStorage(trackerServer); + * client test + * + * @author Happy Fish / YuQing + * @version Version 1.16 + */ +public class TestClient1 { + private TestClient1() { + } + + /** + * entry point + * + * @param args comand arguments + *
  • args[0]: config filename
+ *
  • args[1]: local filename to upload
+ */ + public static void main(String args[]) { + if (args.length < 2) { + System.out.println("Error: Must have 2 parameters, one is config filename, " + + "the other is the local filename to upload"); + return; + } + + System.out.println("java.version=" + System.getProperty("java.version")); + + String conf_filename = args[0]; + String local_filename = args[1]; + String group_name; + + try { + ClientGlobal.init(conf_filename); + System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + System.out.println("charset=" + ClientGlobal.g_charset); + + String file_id; + + TrackerClient tracker = new TrackerClient(); + TrackerServer trackerServer = tracker.getConnection(); + + StorageServer storageServer = null; + /* + storageServer = tracker.getStoreStorage(trackerServer); if (storageServer == null) { System.out.println("getStoreStorage fail, error code: " + tracker.getErrorCode()); return; } */ - StorageClient1 client = new StorageClient1(trackerServer, storageServer); - byte[] file_buff; - NameValuePair[] meta_list; - String master_file_id; - String prefix_name; - String file_ext_name; - String slave_file_id; - String generated_slave_file_id; - int errno; + StorageClient1 client = new StorageClient1(trackerServer, storageServer); + byte[] file_buff; + NameValuePair[] meta_list; + String master_file_id; + String prefix_name; + String file_ext_name; + String slave_file_id; + String generated_slave_file_id; + int errno; - group_name = "group1"; - StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name); - if (storageServers == null) - { - System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode()); - } - else - { - System.err.println("store storage servers count: " + storageServers.length); - for (int k=0; k 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) { + file_ext_name = local_filename.substring(nPos + 1); + } else { + file_ext_name = null; + } + + file_id = client.upload_file1(null, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list); + if (file_id != null) { + System.out.println("file id: " + file_id); + System.out.println(client.get_file_info1(file_id)); + master_file_id = file_id; + prefix_name = "-part3"; + slave_file_id = client.upload_file1(master_file_id, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list); + if (slave_file_id != null) { + System.err.println("slave file_id: " + slave_file_id); + generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name); + if (!generated_slave_file_id.equals(slave_file_id)) { + System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id); + } + } + } else { + System.err.println("Upload file fail, error no: " + errno); + } + + storageServer = tracker.getFetchStorage1(trackerServer, file_id); + if (storageServer == null) { + System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode()); + return; + } - File f; - f = new File(local_filename); - int nPos = local_filename.lastIndexOf('.'); - if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) - { - file_ext_name = local_filename.substring(nPos+1); - } - else - { - file_ext_name = null; - } - - file_id = client.upload_file1(null, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list); - if (file_id != null) - { - System.out.println("file id: " + file_id); - System.out.println(client.get_file_info1(file_id)); - master_file_id = file_id; - prefix_name = "-part3"; - slave_file_id = client.upload_file1(master_file_id, prefix_name, f.length(), new UploadLocalFileSender(local_filename), file_ext_name, meta_list); - if (slave_file_id != null) - { - System.err.println("slave file_id: " + slave_file_id); - generated_slave_file_id = ProtoCommon.genSlaveFilename(master_file_id, prefix_name, file_ext_name); - if (!generated_slave_file_id.equals(slave_file_id)) - { - System.err.println("generated slave file: " + generated_slave_file_id + "\n != returned slave file: " + slave_file_id); - } - } - } - else - { - System.err.println("Upload file fail, error no: " + errno); - } - - storageServer = tracker.getFetchStorage1(trackerServer, file_id); - if (storageServer == null) - { - System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode()); - return; - } - /* for test only */ - System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket())); - storageServer.close(); - + System.out.println("active test to storage server: " + ProtoCommon.activeTest(storageServer.getSocket())); + storageServer.close(); + /* for test only */ - System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket())); - - trackerServer.close(); - } - catch(Exception ex) - { - ex.printStackTrace(); - } + System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket())); + + trackerServer.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } } } diff --git a/src/main/java/org/csource/fastdfs/test/TestLoad.java b/src/main/java/org/csource/fastdfs/test/TestLoad.java index 7572379..bae9ff3 100644 --- a/src/main/java/org/csource/fastdfs/test/TestLoad.java +++ b/src/main/java/org/csource/fastdfs/test/TestLoad.java @@ -1,316 +1,263 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -**/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + **/ package org.csource.fastdfs.test; -import java.io.*; -import java.net.*; -import java.util.*; -import org.csource.common.*; import org.csource.fastdfs.*; /** -* load test class -* @author Happy Fish / YuQing -* @version Version 1.11 -*/ -public class TestLoad -{ - public static java.util.concurrent.ConcurrentLinkedQueue file_ids; - public static int total_download_count = 0; - public static int success_download_count = 0; - public static int fail_download_count = 0; - public static int total_upload_count = 0; - public static int success_upload_count = 0; - public static int upload_thread_count = 0; - -/** -* discard file content callback class when download file -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ - public static class DownloadFileDiscard implements DownloadCallback - { - public DownloadFileDiscard() - { - } - - public int recv(long file_size, byte[] data, int bytes) - { - return 0; - } - } + * load test class + * + * @author Happy Fish / YuQing + * @version Version 1.11 + */ +public class TestLoad { + public static java.util.concurrent.ConcurrentLinkedQueue file_ids; + public static int total_download_count = 0; + public static int success_download_count = 0; + public static int fail_download_count = 0; + public static int total_upload_count = 0; + public static int success_upload_count = 0; + public static int upload_thread_count = 0; -/** -* file uploader -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ - public static class Uploader - { - public TrackerClient tracker; - public TrackerServer trackerServer; - - public Uploader() throws Exception - { - this.tracker = new TrackerClient(); - this.trackerServer = tracker.getConnection(); - } - - public int uploadFile() throws Exception - { - StorageServer storageServer = null; - StorageClient1 client = new StorageClient1(trackerServer, storageServer); - byte[] file_buff; - String file_id; - - file_buff = new byte[2 * 1024]; - java.util.Arrays.fill(file_buff, (byte)65); - - try - { - file_id = client.upload_file1(file_buff, "txt", null); - if (file_id == null) - { - System.out.println("upload file fail, error code: " + client.getErrorCode()); - return -1; - } - - TestLoad.file_ids.offer(file_id); - return 0; - } - catch(Exception ex) - { - System.out.println("upload file fail, error mesg: " + ex.getMessage()); - return -1; - } - } - } + private TestLoad() { + } -/** -* file downloader -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ - public static class Downloader - { - public TrackerClient tracker; - public TrackerServer trackerServer; - public DownloadFileDiscard callback; - - public Downloader() throws Exception - { - this.tracker = new TrackerClient(); - this.trackerServer = tracker.getConnection(); - this.callback = new DownloadFileDiscard(); - } - - public int downloadFile(String file_id) throws Exception - { - int errno; - StorageServer storageServer = null; - StorageClient1 client = new StorageClient1(trackerServer, storageServer); - - try - { - errno = client.download_file1(file_id, this.callback); - if (errno != 0) - { - System.out.println("Download file fail, file_id: " + file_id + ", error no: " + errno); - } - return errno; - } - catch(Exception ex) - { - System.out.println("Download file fail, error mesg: " + ex.getMessage()); - return -1; - } - } - } - -/** -* upload file thread -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ - public static class UploadThread extends Thread - { - private int thread_index; - - public UploadThread(int index) - { - this.thread_index = index; - } - - public void run() - { - try - { - TestLoad.upload_thread_count++; - Uploader uploader = new Uploader(); - - System.out.println("upload thread " + this.thread_index + " start"); - - for (int i=0; i<50000; i++) - { - TestLoad.total_upload_count++; - if (uploader.uploadFile() == 0) - { - TestLoad.success_upload_count++; - } - } - } - catch(Exception ex) - { - ex.printStackTrace(); - } - finally - { - TestLoad.upload_thread_count--; - } - - System.out.println("upload thread " + this.thread_index - + " exit, total_upload_count: " + TestLoad.total_upload_count - + ", success_upload_count: " + TestLoad.success_upload_count - + ", total_download_count: " + TestLoad.total_download_count - + ", success_download_count: " + TestLoad.success_download_count); + /** + * entry point + * + * @param args comand arguments + *

  • args[0]: config filename
+ */ + public static void main(String args[]) { + if (args.length < 1) { + System.out.println("Error: Must have 1 parameter: config filename"); + return; + } + + System.out.println("java.version=" + System.getProperty("java.version")); + + try { + ClientGlobal.init(args[0]); + System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); + System.out.println("charset=" + ClientGlobal.g_charset); + + file_ids = new java.util.concurrent.ConcurrentLinkedQueue(); + + for (int i = 0; i < 10; i++) { + (new UploadThread(i)).start(); + } + + for (int i = 0; i < 20; i++) { + (new DownloadThread(i)).start(); + } + } catch (Exception ex) { + ex.printStackTrace(); } } - -/** -* download file thread -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ - public static class DownloadThread extends Thread - { - private int thread_index; - private static Integer counter_lock = new Integer(0); - - public DownloadThread(int index) - { - this.thread_index = index; - } - - public void run() - { - try - { - String file_id; - Downloader downloader = new Downloader(); - - System.out.println("download thread " + this.thread_index + " start"); - - file_id = ""; - while (TestLoad.upload_thread_count != 0 || file_id != null) - { - file_id = (String)TestLoad.file_ids.poll(); - if (file_id == null) - { - Thread.sleep(10); - continue; - } - - synchronized (this.counter_lock) - { - TestLoad.total_download_count++; - } - if (downloader.downloadFile(file_id) == 0) - { - synchronized (this.counter_lock) - { - TestLoad.success_download_count++; - } - } - else - { - TestLoad.fail_download_count++; - } - } - - for (int i=0; i<3 && TestLoad.total_download_count < TestLoad.total_upload_count; i++) - { - file_id = (String)TestLoad.file_ids.poll(); - if (file_id == null) - { - Thread.sleep(10); - continue; - } - - synchronized (this.counter_lock) - { - TestLoad.total_download_count++; - } - if (downloader.downloadFile(file_id) == 0) - { - synchronized (this.counter_lock) - { - TestLoad.success_download_count++; - } - } - else - { - TestLoad.fail_download_count++; - } - } - } - catch(Exception ex) - { - ex.printStackTrace(); - } - - System.out.println("download thread " + this.thread_index - + " exit, total_download_count: " + TestLoad.total_download_count - + ", success_download_count: " + TestLoad.success_download_count - + ", fail_download_count: " + TestLoad.fail_download_count); + + /** + * discard file content callback class when download file + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ + public static class DownloadFileDiscard implements DownloadCallback { + public DownloadFileDiscard() { + } + + public int recv(long file_size, byte[] data, int bytes) { + return 0; } } - - private TestLoad() - { - } - - /** - * entry point - * @param args comand arguments - *
  • args[0]: config filename
- */ - public static void main(String args[]) - { - if (args.length < 1) - { - System.out.println("Error: Must have 1 parameter: config filename"); - return; - } - - System.out.println("java.version=" + System.getProperty("java.version")); - - try - { - ClientGlobal.init(args[0]); - System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms"); - System.out.println("charset=" + ClientGlobal.g_charset); - - file_ids = new java.util.concurrent.ConcurrentLinkedQueue(); - - for (int i=0; i<10; i++) - { - (new UploadThread(i)).start(); - } - - for (int i=0; i<20; i++) - { - (new DownloadThread(i)).start(); - } - } - catch(Exception ex) - { - ex.printStackTrace(); - } + + /** + * file uploader + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ + public static class Uploader { + public TrackerClient tracker; + public TrackerServer trackerServer; + + public Uploader() throws Exception { + this.tracker = new TrackerClient(); + this.trackerServer = tracker.getConnection(); + } + + public int uploadFile() throws Exception { + StorageServer storageServer = null; + StorageClient1 client = new StorageClient1(trackerServer, storageServer); + byte[] file_buff; + String file_id; + + file_buff = new byte[2 * 1024]; + java.util.Arrays.fill(file_buff, (byte) 65); + + try { + file_id = client.upload_file1(file_buff, "txt", null); + if (file_id == null) { + System.out.println("upload file fail, error code: " + client.getErrorCode()); + return -1; + } + + TestLoad.file_ids.offer(file_id); + return 0; + } catch (Exception ex) { + System.out.println("upload file fail, error mesg: " + ex.getMessage()); + return -1; + } + } + } + + /** + * file downloader + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ + public static class Downloader { + public TrackerClient tracker; + public TrackerServer trackerServer; + public DownloadFileDiscard callback; + + public Downloader() throws Exception { + this.tracker = new TrackerClient(); + this.trackerServer = tracker.getConnection(); + this.callback = new DownloadFileDiscard(); + } + + public int downloadFile(String file_id) throws Exception { + int errno; + StorageServer storageServer = null; + StorageClient1 client = new StorageClient1(trackerServer, storageServer); + + try { + errno = client.download_file1(file_id, this.callback); + if (errno != 0) { + System.out.println("Download file fail, file_id: " + file_id + ", error no: " + errno); + } + return errno; + } catch (Exception ex) { + System.out.println("Download file fail, error mesg: " + ex.getMessage()); + return -1; + } + } + } + + /** + * upload file thread + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ + public static class UploadThread extends Thread { + private int thread_index; + + public UploadThread(int index) { + this.thread_index = index; + } + + public void run() { + try { + TestLoad.upload_thread_count++; + Uploader uploader = new Uploader(); + + System.out.println("upload thread " + this.thread_index + " start"); + + for (int i = 0; i < 50000; i++) { + TestLoad.total_upload_count++; + if (uploader.uploadFile() == 0) { + TestLoad.success_upload_count++; + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + TestLoad.upload_thread_count--; + } + + System.out.println("upload thread " + this.thread_index + + " exit, total_upload_count: " + TestLoad.total_upload_count + + ", success_upload_count: " + TestLoad.success_upload_count + + ", total_download_count: " + TestLoad.total_download_count + + ", success_download_count: " + TestLoad.success_download_count); + } + } + + /** + * download file thread + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ + public static class DownloadThread extends Thread { + private static Integer counter_lock = new Integer(0); + private int thread_index; + + public DownloadThread(int index) { + this.thread_index = index; + } + + public void run() { + try { + String file_id; + Downloader downloader = new Downloader(); + + System.out.println("download thread " + this.thread_index + " start"); + + file_id = ""; + while (TestLoad.upload_thread_count != 0 || file_id != null) { + file_id = (String) TestLoad.file_ids.poll(); + if (file_id == null) { + Thread.sleep(10); + continue; + } + + synchronized (this.counter_lock) { + TestLoad.total_download_count++; + } + if (downloader.downloadFile(file_id) == 0) { + synchronized (this.counter_lock) { + TestLoad.success_download_count++; + } + } else { + TestLoad.fail_download_count++; + } + } + + for (int i = 0; i < 3 && TestLoad.total_download_count < TestLoad.total_upload_count; i++) { + file_id = (String) TestLoad.file_ids.poll(); + if (file_id == null) { + Thread.sleep(10); + continue; + } + + synchronized (this.counter_lock) { + TestLoad.total_download_count++; + } + if (downloader.downloadFile(file_id) == 0) { + synchronized (this.counter_lock) { + TestLoad.success_download_count++; + } + } else { + TestLoad.fail_download_count++; + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + System.out.println("download thread " + this.thread_index + + " exit, total_download_count: " + TestLoad.total_download_count + + ", success_download_count: " + TestLoad.success_download_count + + ", fail_download_count: " + TestLoad.fail_download_count); + } } } diff --git a/src/main/java/org/csource/fastdfs/test/UploadLocalFileSender.java b/src/main/java/org/csource/fastdfs/test/UploadLocalFileSender.java index f5ccbd6..653c79a 100644 --- a/src/main/java/org/csource/fastdfs/test/UploadLocalFileSender.java +++ b/src/main/java/org/csource/fastdfs/test/UploadLocalFileSender.java @@ -1,61 +1,56 @@ /** -* Copyright (C) 2008 Happy Fish / YuQing -* -* FastDFS Java Client may be copied only under the terms of the GNU Lesser -* General Public License (LGPL). -* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. -*/ + * Copyright (C) 2008 Happy Fish / YuQing + *

+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser + * General Public License (LGPL). + * Please visit the FastDFS Home Page http://www.csource.org/ for more detail. + */ package org.csource.fastdfs.test; -import java.io.*; -import java.util.*; -import java.net.*; -import org.csource.fastdfs.*; +import org.csource.fastdfs.UploadCallback; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; /** -* upload file callback class, local file sender -* @author Happy Fish / YuQing -* @version Version 1.0 -*/ -public class UploadLocalFileSender implements UploadCallback -{ - private String local_filename; - - public UploadLocalFileSender(String szLocalFilename) - { - this.local_filename = szLocalFilename; - } - - /** - * 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 - { - FileInputStream fis; - int readBytes; - byte[] buff = new byte[256 * 1024]; - - fis = new FileInputStream(this.local_filename); - try - { - while ((readBytes=fis.read(buff)) >= 0) - { - if (readBytes == 0) - { - continue; - } - - out.write(buff, 0, readBytes); - } - } - finally - { - fis.close(); - } - - return 0; - } + * upload file callback class, local file sender + * + * @author Happy Fish / YuQing + * @version Version 1.0 + */ +public class UploadLocalFileSender implements UploadCallback { + private String local_filename; + + public UploadLocalFileSender(String szLocalFilename) { + this.local_filename = szLocalFilename; + } + + /** + * 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 { + FileInputStream fis; + int readBytes; + byte[] buff = new byte[256 * 1024]; + + fis = new FileInputStream(this.local_filename); + try { + while ((readBytes = fis.read(buff)) >= 0) { + if (readBytes == 0) { + continue; + } + + out.write(buff, 0, readBytes); + } + } finally { + fis.close(); + } + + return 0; + } }