重新格式化所有java代码

This commit is contained in:
veryben 2017-05-19 09:56:24 +08:00
parent 69417006f4
commit 75931a8053
32 changed files with 7826 additions and 8880 deletions

View File

@ -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
*
* <p>
* 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,13 +34,13 @@ 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.
*
* <p>
* 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.
*
* <p>
* 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.
*
* <p>
* 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.
@ -53,384 +53,68 @@ import java.io.IOException;
* Futures Streams or files.
*/
public class Base64
{
/**
* 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];
/* 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;
}
/* 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;
}
// 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;
}
public class Base64 {
/**
* 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];
/* 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;
}
/**
* debug display array
*/
public static void show (byte[] b)
{
public static void show(byte[] b) {
int count = 0;
int rows = 0;
for ( int i=0; i<b.length; i++ )
{
if(count == 8)
{
for (int i = 0; i < b.length; i++) {
if (count == 8) {
System.out.print(" ");
}
else if(count == 16)
{
} else if (count == 16) {
System.out.println("");
count = 0;
continue;
}
System.out.print( Integer.toHexString(b[i] & 0xFF).toUpperCase() + " ");
count ++;
System.out.print(Integer.toHexString(b[i] & 0xFF).toUpperCase() + " ");
count++;
}
System.out.println();
@ -439,11 +123,9 @@ public class Base64
/**
* debug display array
*/
public static void display (byte[] b)
{
for ( int i=0; i<b.length; i++ )
{
System.out.print( (char)b[i]);
public static void display(byte[] b) {
for (int i = 0; i < b.length; i++) {
System.out.print((char) b[i]);
}
System.out.println();
}
@ -451,15 +133,12 @@ public class Base64
/**
* test driver
*/
public static void main(String[] args)
{
public static void main(String[] args) {
test();
System.exit(1);
if ( debug )
{
try
{
if (debug) {
try {
Base64 b64 = new Base64();
String str = "agfrtu¿¦etʲ1234¼Ù´óerty¿Õ234·¢¿¦2344ʲµÄ";
String str64 = "";
@ -474,9 +153,7 @@ public class Base64
String rst = new String(theBytes);
System.out.println(rst);
System.out.println(str);
}
catch(Exception e)
{
} catch (Exception e) {
e.printStackTrace();
}
//getBytes(String charsetName);
@ -515,10 +192,8 @@ public class Base64
}
}// end main
public static void test()
{
try
{
public static void test() {
try {
Base64 b64 = new Base64();
//encode
@ -529,11 +204,288 @@ public class Base64
//decode
byte[] theBytes = b64.decode(str64);
show(theBytes);
}
catch(Exception e)
{
} catch (Exception e) {
e.printStackTrace();
}
}
/* 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;
}
// 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

View File

@ -1,95 +1,98 @@
/**
* 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
* <p>
* 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
{
* 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
{
/**
* @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()
{
private static ClassLoader classLoader() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
return loader;
}
/**
* get the config filename
*
* @return config filename
*/
public String getConfFilename() {
return this.conf_filename;
}
/**
* get string value from config file
* @param name item name in config file
* @return string value
*/
public String getStrValue(String name)
{
/**
* 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)
{
if (obj == null) {
return null;
}
if (obj instanceof String)
{
return (String)obj;
if (obj instanceof String) {
return (String) obj;
}
return (String)((ArrayList)obj).get(0);
return (String) ((ArrayList) obj).get(0);
}
/**
* 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)
{
/**
* 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)
{
if (szValue == null) {
return default_value;
}
return Integer.parseInt(szValue);
}
/**
* 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)
{
/**
* 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)
{
if (szValue == null) {
return default_value;
}
@ -97,30 +100,28 @@ public class IniFileReader
szValue.equalsIgnoreCase("true") || szValue.equals("1");
}
/**
* get all values from config file
* @param name item name in config file
* @return string values (array)
*/
public String[] getValues(String name)
{
/**
* 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;
obj = this.paramTable.get(name);
if (obj == null)
{
if (obj == null) {
return null;
}
if (obj instanceof String)
{
if (obj instanceof String) {
values = new String[1];
values[0] = (String)obj;
values[0] = (String) obj;
return values;
}
Object[] objs = ((ArrayList)obj).toArray();
Object[] objs = ((ArrayList) obj).toArray();
values = new String[objs.length];
System.arraycopy(objs, 0, values, 0, objs.length);
return values;
@ -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;
}
}

View File

@ -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()
{
* My Exception
*
* @author Happy Fish / YuQing
* @version Version 1.0
*/
public class MyException extends Exception {
public MyException() {
}
public MyException(String message)
{
public MyException(String message) {
super(message);
}
}

View File

@ -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
{
* 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)
{
public NameValuePair(String name) {
this.name = name;
}
public NameValuePair(String name, String value)
{
public NameValuePair(String name, String value) {
this.name = name;
this.value = value;
}
public String getName()
{
public String getName() {
return this.name;
}
public String getValue()
{
return this.value;
}
public void setName(String name)
{
public void setName(String name) {
this.name = name;
}
public void setValue(String value)
{
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -1,25 +1,29 @@
/**
* 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
* <p>
* 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
{
* 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;
@ -28,19 +32,15 @@ public class ClientGlobal
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()
{
private ClientGlobal() {
}
/**
* load global variables
* @param conf_filename config filename
*/
public static void init(String conf_filename) throws FileNotFoundException, IOException, MyException
{
/**
* load global variables
*
* @param conf_filename config filename
*/
public static void init(String conf_filename) throws IOException, MyException {
IniFileReader iniReader;
String[] szTrackerServers;
String[] parts;
@ -48,37 +48,31 @@ public class ClientGlobal
iniReader = new IniFileReader(conf_filename);
g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
if (g_connect_timeout < 0)
{
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)
{
if (g_network_timeout < 0) {
g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
}
g_network_timeout *= 1000; //millisecond
g_charset = iniReader.getStrValue("charset");
if (g_charset == null || g_charset.length() == 0)
{
if (g_charset == null || g_charset.length() == 0) {
g_charset = "ISO8859-1";
}
szTrackerServers = iniReader.getValues("tracker_server");
if (szTrackerServers == null)
{
if (szTrackerServers == null) {
throw new MyException("item \"tracker_server\" in " + conf_filename + " not found");
}
InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
for (int i=0; i<szTrackerServers.length; i++)
{
for (int i = 0; i < szTrackerServers.length; i++) {
parts = szTrackerServers[i].split("\\:", 2);
if (parts.length != 2)
{
if (parts.length != 2) {
throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
}
@ -88,111 +82,95 @@ public class ClientGlobal
g_tracker_http_port = iniReader.getIntValue("http.tracker_http_port", 80);
g_anti_steal_token = iniReader.getBoolValue("http.anti_steal_token", false);
if (g_anti_steal_token)
{
if (g_anti_steal_token) {
g_secret_key = iniReader.getStrValue("http.secret_key");
}
}
/**
* construct Socket object
* @param ip_addr ip address or hostname
* @param port port number
* @return connected Socket object
*/
public static Socket getSocket(String ip_addr, int port) throws IOException
{
/**
* construct Socket object
*
* @param ip_addr ip address or hostname
* @param port port number
* @return connected Socket object
*/
public static Socket getSocket(String ip_addr, int port) throws IOException {
Socket sock = new Socket();
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
return sock;
}
/**
* construct Socket object
* @param addr InetSocketAddress object, including ip address and port
* @return connected Socket object
*/
public static Socket getSocket(InetSocketAddress addr) throws IOException
{
/**
* construct Socket object
*
* @param addr InetSocketAddress object, including ip address and port
* @return connected Socket object
*/
public static Socket getSocket(InetSocketAddress addr) throws IOException {
Socket sock = new Socket();
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(addr, ClientGlobal.g_connect_timeout);
return sock;
}
public static int getG_connect_timeout()
{
public static int getG_connect_timeout() {
return g_connect_timeout;
}
public static void setG_connect_timeout(int connect_timeout)
{
public static void setG_connect_timeout(int connect_timeout) {
ClientGlobal.g_connect_timeout = connect_timeout;
}
public static int getG_network_timeout()
{
public static int getG_network_timeout() {
return g_network_timeout;
}
public static void setG_network_timeout(int network_timeout)
{
public static void setG_network_timeout(int network_timeout) {
ClientGlobal.g_network_timeout = network_timeout;
}
public static String getG_charset()
{
public static String getG_charset() {
return g_charset;
}
public static void setG_charset(String charset)
{
public static void setG_charset(String charset) {
ClientGlobal.g_charset = charset;
}
public static int getG_tracker_http_port()
{
public static int getG_tracker_http_port() {
return g_tracker_http_port;
}
public static void setG_tracker_http_port(int tracker_http_port)
{
public static void setG_tracker_http_port(int tracker_http_port) {
ClientGlobal.g_tracker_http_port = tracker_http_port;
}
public static boolean getG_anti_steal_token()
{
public static boolean getG_anti_steal_token() {
return g_anti_steal_token;
}
public static boolean isG_anti_steal_token()
{
public static boolean isG_anti_steal_token() {
return g_anti_steal_token;
}
public static void setG_anti_steal_token(boolean anti_steal_token)
{
public static void setG_anti_steal_token(boolean anti_steal_token) {
ClientGlobal.g_anti_steal_token = anti_steal_token;
}
public static String getG_secret_key()
{
public static String getG_secret_key() {
return g_secret_key;
}
public static void setG_secret_key(String secret_key)
{
public static void setG_secret_key(String secret_key) {
ClientGlobal.g_secret_key = secret_key;
}
public static TrackerGroup getG_tracker_group()
{
public static TrackerGroup getG_tracker_group() {
return g_tracker_group;
}
public static void setG_tracker_group(TrackerGroup tracker_group)
{
public static void setG_tracker_group(TrackerGroup tracker_group) {
ClientGlobal.g_tracker_group = tracker_group;
}
}

View File

@ -1,27 +1,23 @@
/**
* 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
* <p>
* 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
{
* 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

View File

@ -2,46 +2,40 @@ 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
{
* 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)
{
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
{
public int recv(long fileSize, byte[] data, int bytes) {
try {
out.write(data, 0, bytes);
}
catch(IOException ex)
{
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}
currentBytes += bytes;
if (this.currentBytes == fileSize)
{
if (this.currentBytes == fileSize) {
this.currentBytes = 0;
}

View File

@ -1,121 +1,121 @@
/**
* 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
* <p>
* 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
{
* 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)
{
/**
* 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()
{
/**
* 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;
/**
* set the source ip address of the file uploaded to
*
* @param source_ip_addr the source ip address
*/
public void setSourceIpAddr(String source_ip_addr) {
this.source_ip_addr = source_ip_addr;
}
/**
* get the file size
* @return the file size
*/
public long getFileSize()
{
/**
* get the file size
*
* @return the file size
*/
public long getFileSize() {
return this.file_size;
}
/**
* set the create timestamp of the file
* @param create_timestamp create timestamp in seconds
*/
public void setCreateTimestamp(int create_timestamp)
{
this.create_timestamp = new Date(create_timestamp * 1000L);
/**
* set the 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()
{
/**
* 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 crc32 the crc32 signature
*/
public void setCrc32(int crc32)
{
this.crc32 = crc32;
/**
* 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()
{
/**
* get the file CRC32 signature
*
* @return the file CRC32 signature
*/
public long getCrc32() {
return this.crc32;
}
/**
* to string
* @return string
*/
public String toString()
{
/**
* 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 + ", " +

View File

@ -1,64 +1,34 @@
/**
* 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
* <p>
* 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;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/**
* protocol common functions
* @author Happy Fish / YuQing
* @version Version 1.18
*/
public class ProtoCommon
{
/**
* receive package info
* protocol common functions
*
* @author Happy Fish / YuQing
* @version Version 1.18
*/
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 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;
@ -75,13 +45,11 @@ public class ProtoCommon
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_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;
@ -91,17 +59,14 @@ public class ProtoCommon
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;
@ -109,24 +74,17 @@ public class ProtoCommon
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;
@ -134,21 +92,19 @@ public class ProtoCommon
public static final byte ERR_NO_ENOSPC = 28;
public static final byte ECONNREFUSED = 61;
public static final byte ERR_NO_EALREADY = 114;
public static final long INFINITE_FILE_SIZE = 256 * 1024L * 1024 * 1024 * 1024 * 1024L;
public static final long APPENDER_FILE_SIZE = INFINITE_FILE_SIZE;
public static final long TRUNK_FILE_MARK_SIZE = 512 * 1024L * 1024 * 1024 * 1024 * 1024L;
public static final long NORMAL_LOGIC_FILENAME_LENGTH = FDFS_FILE_PATH_LEN + FDFS_FILENAME_BASE64_LENGTH + FDFS_FILE_EXT_NAME_MAX_LEN + 1;
public static final long TRUNK_LOGIC_FILENAME_LENGTH = NORMAL_LOGIC_FILENAME_LENGTH + FDFS_TRUNK_FILE_INFO_LEN;
protected static final int PROTO_HEADER_CMD_INDEX = FDFS_PROTO_PKG_LEN_SIZE;
protected static final int PROTO_HEADER_STATUS_INDEX = FDFS_PROTO_PKG_LEN_SIZE + 1;
private ProtoCommon()
{
private ProtoCommon() {
}
public static String getStorageStatusCaption(byte status)
{
switch(status)
{
public static String getStorageStatusCaption(byte status) {
switch (status) {
case FDFS_STORAGE_STATUS_INIT:
return "INIT";
case FDFS_STORAGE_STATUS_WAIT_SYNC:
@ -170,22 +126,22 @@ public class ProtoCommon
default:
return "UNKOWN";
}
}
}
/**
* pack header by FastDFS transfer protocol
* @param cmd which command to send
* @param pkg_len package body length
* @param errno status code, should be (byte)0
* @return packed byte buffer
*/
public static byte[] packHeader(byte cmd, long pkg_len, byte errno) throws UnsupportedEncodingException
{
/**
* 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);
Arrays.fill(header, (byte) 0);
hex_len = ProtoCommon.long2buff(pkg_len);
System.arraycopy(hex_len, 0, header, 0, hex_len.length);
@ -194,74 +150,66 @@ public class ProtoCommon
return header;
}
/**
* receive pack header
* @param in input stream
* @param expect_cmd expect response command
* @param expect_body_len expect response package body length
* @return RecvHeaderInfo: errno and pkg body length
*/
public static RecvHeaderInfo recvHeader(InputStream in, byte expect_cmd, long expect_body_len) throws IOException
{
/**
* 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)
{
if ((bytes = in.read(header)) != header.length) {
throw new IOException("recv package size " + bytes + " != " + header.length);
}
if (header[PROTO_HEADER_CMD_INDEX] != expect_cmd)
{
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)
{
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)
{
if (pkg_len < 0) {
throw new IOException("recv body length: " + pkg_len + " < 0!");
}
if (expect_body_len >= 0 && pkg_len != expect_body_len)
{
if (expect_body_len >= 0 && pkg_len != expect_body_len) {
throw new IOException("recv body length: " + pkg_len + " is not correct, expect length: " + expect_body_len);
}
return new RecvHeaderInfo((byte)0, pkg_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
{
/**
* 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)
{
if (header.errno != 0) {
return new RecvPackageInfo(header.errno, null);
}
byte[] body = new byte[(int)header.body_len];
byte[] body = new byte[(int) header.body_len];
int totalBytes = 0;
int remainBytes = (int)header.body_len;
int remainBytes = (int) header.body_len;
int bytes;
while (totalBytes < header.body_len)
{
if ((bytes=in.read(body, totalBytes, remainBytes)) < 0)
{
while (totalBytes < header.body_len) {
if ((bytes = in.read(body, totalBytes, remainBytes)) < 0) {
break;
}
@ -269,46 +217,43 @@ public class ProtoCommon
remainBytes -= bytes;
}
if (totalBytes != header.body_len)
{
if (totalBytes != header.body_len) {
throw new IOException("recv package size " + totalBytes + " != " + header.body_len);
}
return new RecvPackageInfo((byte)0, body);
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)
{
/**
* 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
*/
/**
* 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 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++)
{
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)
{
if (cols.length == 2) {
meta_list[i].setValue(cols[1]);
}
}
@ -316,22 +261,20 @@ public class ProtoCommon
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)
{
/**
* 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++)
{
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());
}
@ -339,94 +282,94 @@ public class ProtoCommon
return sb.toString();
}
/**
* send quit command to server and close socket
* @param sock the Socket object
*/
public static void closeSocket(Socket sock) throws IOException
{
/**
* 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);
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
{
/**
* 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);
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)
{
/**
* 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);
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 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 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)
{
/**
* 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 "";
@ -434,11 +377,9 @@ public class ProtoCommon
int n;
StringBuilder sbResult = new StringBuilder(16);
for (int i=offset; i<offset+4; i++)
{
for (int i = offset; i < offset + 4; i++) {
n = (bs[i] >= 0) ? bs[i] : 256 + bs[i];
if (sbResult.length() > 0)
{
if (sbResult.length() > 0) {
sbResult.append(".");
}
sbResult.append(String.valueOf(n));
@ -448,20 +389,19 @@ public class ProtoCommon
}
/**
* md5 function
* @param source the input buffer
* @return md5 string
*/
public static String md5(byte[] source) throws NoSuchAlgorithmException
{
* 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++)
{
for (int i = 0; i < 16; i++) {
str[k++] = hexDigits[tmp[i] >>> 4 & 0xf];
str[k++] = hexDigits[tmp[i] & 0xf];
}
@ -470,14 +410,14 @@ public class ProtoCommon
}
/**
* 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
{
* 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);
@ -491,63 +431,73 @@ public class ProtoCommon
}
/**
* 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
*/
* 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 prefix_name, String ext_name) throws MyException {
String true_ext_name;
int dotIndex;
if (master_filename.length() < 28 + FDFS_FILE_EXT_NAME_MAX_LEN)
{
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)
{
if (ext_name != null) {
if (ext_name.length() == 0) {
true_ext_name = "";
}
else if (ext_name.charAt(0) == '.')
{
} else if (ext_name.charAt(0) == '.') {
true_ext_name = ext_name;
}
else
{
} else {
true_ext_name = "." + ext_name;
}
}
else
{
if (dotIndex < 0)
{
} else {
if (dotIndex < 0) {
true_ext_name = "";
}
else
{
} else {
true_ext_name = master_filename.substring(dotIndex);
}
}
if (true_ext_name.length() == 0 && prefix_name.equals("-m"))
{
if (true_ext_name.length() == 0 && prefix_name.equals("-m")) {
throw new MyException("prefix_name \"" + prefix_name + "\" is invalid");
}
if (dotIndex < 0)
{
if (dotIndex < 0) {
return master_filename + prefix_name + true_ext_name;
}
else
{
} 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;
}
}
}

View File

@ -1,50 +1,43 @@
/**
* 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
* <p>
* 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<T extends StructBase>
{
/**
* Constructor
*/
public ProtoStructDecoder()
{
* C struct body decoder
*
* @author Happy Fish / YuQing
* @version Version 1.17
*/
public class ProtoStructDecoder<T extends StructBase> {
/**
* Constructor
*/
public ProtoStructDecoder() {
}
/**
* decode byte buffer
*/
public T[] decode(byte[] bs, Class<T> clazz, int fieldsTotalSize) throws Exception
{
if (bs.length % fieldsTotalSize != 0)
{
/**
* decode byte buffer
*/
public T[] decode(byte[] bs, Class<T> 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);
T[] results = (T[]) Array.newInstance(clazz, count);
offset = 0;
for (int i=0; i<results.length; i++)
{
for (int i = 0; i < results.length; i++) {
results[i] = clazz.newInstance();
results[i].setFields(bs, offset);
offset += fieldsTotalSize;

View File

@ -1,63 +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
* <p>
* 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
{
* 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)
{
/**
* 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 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 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
{
/**
* 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);

File diff suppressed because it is too large Load Diff

View File

@ -1,50 +1,47 @@
/**
* 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
* <p>
* 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
{
* 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()
{
/**
* 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)
{
/**
* 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);
}
public static byte split_file_id(String file_id, String[] results)
{
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))
{
if ((pos <= 0) || (pos == file_id.length() - 1)) {
return ProtoCommon.ERR_NO_EINVAL;
}
@ -55,6 +52,7 @@ public class StorageClient1 extends StorageClient
/**
* 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
@ -62,21 +60,18 @@ public class StorageClient1 extends StorageClient
* return null if fail
*/
public String upload_file1(String local_filename, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_file(local_filename, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -85,21 +80,18 @@ public class StorageClient1 extends StorageClient
* return null if fail
*/
public String upload_file1(String group_name, String local_filename, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_file(group_name, local_filename, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -107,21 +99,18 @@ public class StorageClient1 extends StorageClient
* return null if fail
*/
public String upload_file1(byte[] file_buff, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_file(file_buff, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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(.)
@ -130,21 +119,18 @@ public class StorageClient1 extends StorageClient
* return null if fail
*/
public String upload_file1(String group_name, byte[] file_buff, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_file(group_name, file_buff, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -155,21 +141,18 @@ public class StorageClient1 extends StorageClient
*/
public String upload_file1(String group_name, long file_size,
UploadCallback callback, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_file(group_name, file_size, callback, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} else {
return null;
}
}
/**
* upload appender file to storage server (by file name)
*
* @param local_filename local filename to upload
* @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename
* @param meta_list meta info array
@ -177,21 +160,18 @@ public class StorageClient1 extends StorageClient
* return null if fail
*/
public String upload_appender_file1(String local_filename, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_appender_file(local_filename, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -200,21 +180,18 @@ public class StorageClient1 extends StorageClient
* 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
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_appender_file(group_name, local_filename, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -222,21 +199,18 @@ public class StorageClient1 extends StorageClient
* return null if fail
*/
public String upload_appender_file1(byte[] file_buff, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_appender_file(file_buff, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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(.)
@ -245,21 +219,18 @@ public class StorageClient1 extends StorageClient
* 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
{
NameValuePair[] meta_list) throws IOException, MyException {
String parts[] = this.upload_appender_file(group_name, file_buff, file_ext_name, meta_list);
if (parts != null)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -270,21 +241,18 @@ public class StorageClient1 extends StorageClient
*/
public String upload_appender_file1(String group_name, long file_size,
UploadCallback callback, String file_ext_name,
NameValuePair[] meta_list) throws IOException, MyException
{
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)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -294,29 +262,25 @@ public class StorageClient1 extends StorageClient
* 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 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)
{
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)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -326,28 +290,24 @@ public class StorageClient1 extends StorageClient
* 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
{
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)
{
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)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -358,29 +318,25 @@ public class StorageClient1 extends StorageClient
*/
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
{
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)
{
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)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
@ -392,38 +348,32 @@ public class StorageClient1 extends StorageClient
*/
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
{
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)
{
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)
{
if (parts != null) {
return parts[0] + SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + parts[1];
}
else
{
} 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -432,16 +382,15 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -450,18 +399,17 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -470,17 +418,16 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -489,18 +436,17 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -509,18 +455,17 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -529,6 +474,7 @@ public class StorageClient1 extends StorageClient
/**
* 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
@ -537,12 +483,10 @@ public class StorageClient1 extends StorageClient
* @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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -552,6 +496,7 @@ public class StorageClient1 extends StorageClient
/**
* 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
@ -559,12 +504,10 @@ public class StorageClient1 extends StorageClient
* @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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -573,15 +516,14 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -590,15 +532,14 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -607,16 +548,15 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -625,11 +565,11 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
public byte[] download_file1(String file_id) throws IOException, MyException {
final long file_offset = 0;
final long download_bytes = 0;
@ -638,17 +578,16 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return null;
}
@ -657,12 +596,12 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
public int download_file1(String file_id, String local_filename) throws IOException, MyException {
final long file_offset = 0;
final long download_bytes = 0;
@ -671,18 +610,17 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -691,12 +629,12 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
public int download_file1(String file_id, DownloadCallback callback) throws IOException, MyException {
final long file_offset = 0;
final long download_bytes = 0;
@ -705,18 +643,17 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -725,15 +662,14 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return null;
}
@ -742,6 +678,7 @@ public class StorageClient1 extends StorageClient
/**
* 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: <br>
@ -751,12 +688,10 @@ public class StorageClient1 extends StorageClient
* the metadata item not exist, otherwise update it</li></ul>
* @return 0 for success, !=0 fail (error code)
*/
public int set_metadata1(String file_id, NameValuePair[] meta_list, byte op_flag) throws IOException, MyException
{
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)
{
if (this.errno != 0) {
return this.errno;
}
@ -765,15 +700,14 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return null;
}
@ -782,15 +716,14 @@ public class StorageClient1 extends StorageClient
/**
* 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
{
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)
{
if (this.errno != 0) {
return null;
}

View File

@ -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
* <p>
* 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
{
* 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
{
/**
* 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
{
/**
* 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)
{
if (store_path < 0) {
this.store_path_index = 256 + store_path;
}
else
{
} else {
this.store_path_index = store_path;
}
}
/**
* @return the store path index on the storage server
*/
public int getStorePathIndex()
{
/**
* @return the store path index on the storage server
*/
public int getStorePathIndex() {
return this.store_path_index;
}
}

View File

@ -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
* <p>
* 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
*/
* 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 String stringValue(byte[] bs, int offset, FieldInfo filedInfo)
{
try
{
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)
{
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
return null;
}
}
protected long longValue(byte[] bs, int offset, FieldInfo filedInfo)
{
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 intValue(byte[] bs, int offset, FieldInfo filedInfo) {
return (int) ProtoCommon.buff2long(bs, offset + filedInfo.offset);
}
protected int int32Value(byte[] bs, int offset, FieldInfo filedInfo)
{
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)
{
protected byte byteValue(byte[] bs, int offset, FieldInfo filedInfo) {
return bs[offset + filedInfo.offset];
}
protected boolean booleanValue(byte[] bs, int offset, FieldInfo filedInfo)
{
protected boolean booleanValue(byte[] bs, int offset, FieldInfo filedInfo) {
return bs[offset + filedInfo.offset] != 0;
}
protected Date dateValue(byte[] bs, int offset, FieldInfo filedInfo)
{
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;
}
}
}

View File

@ -1,20 +1,20 @@
/**
* 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
* <p>
* 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
{
* 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;
@ -31,8 +31,7 @@ public class StructGroupStat extends StructBase
protected static int fieldsTotalSize;
protected static StructBase.FieldInfo[] fieldsArray = new StructBase.FieldInfo[12];
static
{
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;
@ -86,121 +85,130 @@ public class StructGroupStat extends StructBase
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()
{
/**
* get fields total size
*
* @return fields total size
*/
public static int getFieldsTotalSize() {
return fieldsTotalSize;
}
/**
* get group name
*
* @return group name
*/
public String getGroupName() {
return this.groupName;
}
/**
* get total disk space in MB
* @return total disk space in MB
*/
public long getTotalMB()
{
/**
* 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()
{
/**
* get free disk space in MB
*
* @return free disk space in MB
*/
public long getFreeMB() {
return this.freeMB;
}
/**
* get trunk free space in MB
* @return trunk free space in MB
*/
public long getTrunkFreeMB()
{
/**
* get trunk free space in MB
*
* @return trunk free space in MB
*/
public long getTrunkFreeMB() {
return this.trunkFreeMB;
}
/**
* get storage server count in this group
* @return storage server count in this group
*/
public int getStorageCount()
{
/**
* 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()
{
/**
* 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()
{
/**
* 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()
{
/**
* 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()
{
/**
* 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()
{
/**
* 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()
{
/**
* 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()
{
/**
* 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)
{
/**
* 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]);
@ -214,13 +222,4 @@ public class StructGroupStat extends StructBase
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;
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,46 +1,45 @@
/**
* 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
* <p>
* 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;
* 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;
/**
* Constructor
* @param tracker_servers tracker servers
*/
public TrackerGroup(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;
}
/**
* return connected tracker server
* @return connected tracker server, null for fail
*/
public TrackerServer getConnection(int serverIndex) throws IOException
{
/**
* 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);
@ -48,58 +47,46 @@ public class TrackerGroup
return new TrackerServer(sock, this.tracker_servers[serverIndex]);
}
/**
* return connected tracker server
* @return connected tracker server, null for fail
*/
public TrackerServer getConnection() throws IOException
{
/**
* return connected tracker server
*
* @return connected tracker server, null for fail
*/
public TrackerServer getConnection() throws IOException {
int current_index;
synchronized(this.lock)
{
synchronized (this.lock) {
this.tracker_server_index++;
if (this.tracker_server_index >= this.tracker_servers.length)
{
if (this.tracker_server_index >= this.tracker_servers.length) {
this.tracker_server_index = 0;
}
current_index = this.tracker_server_index;
}
try
{
try {
return this.getConnection(current_index);
}
catch(IOException ex)
{
} 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)
{
for (int i = 0; i < this.tracker_servers.length; i++) {
if (i == current_index) {
continue;
}
try
{
try {
TrackerServer trackerServer = this.getConnection(i);
synchronized(this.lock)
{
if (this.tracker_server_index == current_index)
{
synchronized (this.lock) {
if (this.tracker_server_index == current_index) {
this.tracker_server_index = i;
}
}
return trackerServer;
}
catch(IOException ex)
{
} 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);
}
@ -108,11 +95,9 @@ public class TrackerGroup
return null;
}
public Object clone()
{
public Object clone() {
InetSocketAddress[] trackerServers = new InetSocketAddress[this.tracker_servers.length];
for (int i=0; i<trackerServers.length; i++)
{
for (int i = 0; i < trackerServers.length; i++) {
trackerServers[i] = new InetSocketAddress(this.tracker_servers[i].getAddress().getHostAddress(), this.tracker_servers[i].getPort());
}

View File

@ -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
* <p>
* 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
{
* 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)
{
/**
* 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)
{
/**
* 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()
{
/**
* get the server info
*
* @return the server info
*/
public InetSocketAddress getInetSocketAddress() {
return this.inetSockAddr;
}
public OutputStream getOutputStream() throws IOException
{
public OutputStream getOutputStream() throws IOException {
return this.sock.getOutputStream();
}
public InputStream getInputStream() throws IOException
{
public InputStream getInputStream() throws IOException {
return this.sock.getInputStream();
}
public void close() throws IOException
{
if (this.sock != null)
{
try
{
public void close() throws IOException {
if (this.sock != null) {
try {
ProtoCommon.closeSocket(this.sock);
}
finally
{
} finally {
this.sock = null;
}
}
}
protected void finalize() throws Throwable
{
protected void finalize() throws Throwable {
this.close();
}
}

View File

@ -1,27 +1,26 @@
/**
* 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
* <p>
* 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
{
* 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
*/

View File

@ -1,27 +1,26 @@
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
{
* 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)
{
public UploadStream(InputStream inputStream, long fileSize) {
super();
this.inputStream = inputStream;
this.fileSize = fileSize;
@ -29,25 +28,20 @@ public class UploadStream implements 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
{
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)
{
while (remainBytes > 0) {
try {
if ((bytes = inputStream.read(buff, 0, remainBytes > buff.length ? buff.length : (int) remainBytes)) < 0) {
return -1;
}
}
catch(IOException ex)
{
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}

View File

@ -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
* <p>
* 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
{
* 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)
{
public DownloadFileWriter(String filename) {
this.filename = filename;
}
public int recv(long file_size, byte[] data, int bytes)
{
try
{
if (this.out == null)
{
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)
{
if (this.current_bytes == file_size) {
this.out.close();
this.out = null;
this.current_bytes = 0;
}
}
catch(IOException ex)
{
} catch (IOException ex) {
ex.printStackTrace();
return -1;
}
@ -57,10 +50,8 @@ public class DownloadFileWriter implements DownloadCallback
return 0;
}
protected void finalize() throws Throwable
{
if (this.out != null)
{
protected void finalize() throws Throwable {
if (this.out != null) {
this.out.close();
this.out = null;
}

View File

@ -1,45 +1,42 @@
/**
* 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
* <p>
* 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()
{
* load test class
*
* @author Happy Fish / YuQing
* @version Version 1.20
*/
public class Monitor {
private Monitor() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
*/
public static void main(String args[])
{
if (args.length < 1)
{
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
{
try {
ClientGlobal.init(args[0]);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
@ -52,15 +49,13 @@ public class Monitor
*/
TrackerServer trackerServer = tracker.getConnection();
if (trackerServer == null)
{
if (trackerServer == null) {
return;
}
int count;
StructGroupStat[] groupStats = tracker.listGroups(trackerServer);
if (groupStats == null)
{
if (groupStats == null) {
System.out.println("");
System.out.println("ERROR! list groups error, error no: " + tracker.getErrorCode());
System.out.println("");
@ -70,8 +65,7 @@ public class Monitor
System.out.println("group count: " + groupStats.length);
count = 0;
for (StructGroupStat groupStat : groupStats)
{
for (StructGroupStat groupStat : groupStats) {
count++;
System.out.println("Group " + count + ":");
System.out.println("group name = " + groupStat.getGroupName());
@ -88,8 +82,7 @@ public class Monitor
System.out.println("current trunk file id = " + groupStat.getCurrentTrunkFileId());
StructStorageStat[] storageStats = tracker.listStorages(trackerServer, groupStat.getGroupName());
if (storageStats == null)
{
if (storageStats == null) {
System.out.println("");
System.out.println("ERROR! list storage error, error no: " + tracker.getErrorCode());
System.out.println("");
@ -98,8 +91,7 @@ public class Monitor
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int stroageCount = 0;
for (StructStorageStat storageStat : storageStats)
{
for (StructStorageStat storageStat : storageStats) {
stroageCount++;
System.out.println("\tStorage " + stroageCount + ":");
System.out.println("\t\tstorage id = " + storageStat.getId());
@ -167,35 +159,28 @@ public class Monitor
}
trackerServer.close();
}
catch(Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
}
protected static String getSyncedDelayString(StructStorageStat[] storageStats, StructStorageStat currentStorageStat)
{
protected static String getSyncedDelayString(StructStorageStat[] storageStats, StructStorageStat currentStorageStat) {
long maxLastSourceUpdate = 0;
for (StructStorageStat storageStat : storageStats)
{
if (storageStat != currentStorageStat && storageStat.getLastSourceUpdate().getTime() > maxLastSourceUpdate)
{
for (StructStorageStat storageStat : storageStats) {
if (storageStat != currentStorageStat && storageStat.getLastSourceUpdate().getTime() > maxLastSourceUpdate) {
maxLastSourceUpdate = storageStat.getLastSourceUpdate().getTime();
}
}
if (maxLastSourceUpdate == 0)
{
if (maxLastSourceUpdate == 0) {
return "";
}
if (currentStorageStat.getLastSyncedTimestamp().getTime() == 0)
{
if (currentStorageStat.getLastSyncedTimestamp().getTime() == 0) {
return " (never synced)";
}
int delaySeconds = (int)((maxLastSourceUpdate - currentStorageStat.getLastSyncedTimestamp().getTime()) / 1000);
int delaySeconds = (int) ((maxLastSourceUpdate - currentStorageStat.getLastSyncedTimestamp().getTime()) / 1000);
int day = delaySeconds / (24 * 3600);
int remainSeconds = delaySeconds % (24 * 3600);
int hour = remainSeconds / 3600;
@ -203,20 +188,13 @@ public class Monitor
int minute = remainSeconds / 60;
int second = remainSeconds % 60;
String delayTimeStr;
if (day != 0)
{
if (day != 0) {
delayTimeStr = String.format("%1$d days %2$02dh:%3$02dm:%4$02ds", day, hour, minute, second);
}
else if (hour != 0)
{
} else if (hour != 0) {
delayTimeStr = String.format("%1$02dh:%2$02dm:%3$02ds", hour, minute, second);
}
else if (minute != 0)
{
} else if (minute != 0) {
delayTimeStr = String.format("%1$02dm:%2$02ds", minute, second);
}
else
{
} else {
delayTimeStr = String.format("%1$ds", second);
}

View File

@ -1,40 +1,35 @@
/**
* 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
* <p>
* 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()
{
* client test
*
* @author Happy Fish / YuQing
* @version Version 1.18
*/
public class Test {
private Test() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[])
{
if (args.length < 2)
{
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;
@ -45,8 +40,7 @@ public class Test
String conf_filename = args[0];
String local_filename = args[1];
try
{
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
@ -68,9 +62,7 @@ public class Test
}
trackerServer.close();
}
catch(Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
}

View File

@ -1,18 +1,13 @@
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
{
import java.net.InetSocketAddress;
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);
@ -21,15 +16,13 @@ public class Test1
TrackerClient tc = new TrackerClient(tg);
TrackerServer ts = tc.getConnection();
if (ts == null)
{
if (ts == null) {
System.out.println("getConnection return null");
return;
}
StorageServer ss = tc.getStoreStorage(ts);
if (ss == null)
{
if (ss == null) {
System.out.println("getStoreStorage return null");
}
@ -38,21 +31,16 @@ public class Test1
NameValuePair[] meta_list = null; //new NameValuePair[0];
String item;
String fileid;
if (System.getProperty("os.name").equalsIgnoreCase("windows"))
{
if (System.getProperty("os.name").equalsIgnoreCase("windows")) {
item = "c:/windows/system32/notepad.exe";
fileid = sc1.upload_file1(item, "exe", meta_list);
}
else
{
} else {
item = "/etc/hosts";
fileid = sc1.upload_file1(item, "", meta_list);
}
System.out.println("Upload local file "+item+" ok, fileid="+fileid);
}
catch(Exception ex)
{
System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
} catch (Exception ex) {
ex.printStackTrace();
}
}

View File

@ -1,40 +1,38 @@
/**
* 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
* <p>
* 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()
{
* client test
*
* @author Happy Fish / YuQing
* @version Version 1.20
*/
public class TestAppender {
private TestAppender() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[])
{
if (args.length < 2)
{
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;
@ -45,8 +43,7 @@ public class TestAppender
String conf_filename = args[0];
String local_filename = args[1];
try
{
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
@ -88,16 +85,12 @@ public class TestAppender
group_name = null;
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null)
{
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
}
else
{
} 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());
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("");
}
@ -110,29 +103,22 @@ public class TestAppender
group_name = "";
results = client.upload_appender_file(group_name, file_buff, "txt", meta_list);
*/
if (results == null)
{
if (results == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
}
else
{
} else {
group_name = results[0];
remote_filename = results[1];
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(client.get_file_info(group_name, remote_filename));
servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
if (servers == null)
{
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
}
else
{
} 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());
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
@ -144,22 +130,17 @@ public class TestAppender
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);
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)
{
if (errno == 0) {
System.err.println("set_metadata success");
}
else
{
} 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++)
{
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
@ -170,8 +151,7 @@ public class TestAppender
file_buff = client.download_file(group_name, remote_filename);
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (file_buff != null)
{
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
@ -182,31 +162,24 @@ public class TestAppender
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)
{
if (errno == 0) {
System.err.println(client.get_file_info(group_name, appender_filename));
}
else
{
} 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)
{
if (errno == 0) {
System.err.println("Delete file success");
}
else
{
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
results = client.upload_appender_file(local_filename, null, meta_list);
if (results != null)
{
if (results != null) {
String file_id;
int ts;
String token;
@ -219,14 +192,12 @@ public class TestAppender
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80)
{
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);
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;
}
@ -236,22 +207,16 @@ public class TestAppender
System.err.println("file url: " + file_url);
errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_"));
if (errno == 0)
{
if (errno == 0) {
System.err.println("Download file success");
}
else
{
} 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)
{
if (errno == 0) {
System.err.println("Download file success");
}
else
{
} else {
System.err.println("Download file fail, error no: " + errno);
}
@ -260,12 +225,9 @@ public class TestAppender
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)
{
if (errno == 0) {
System.err.println(client.get_file_info(group_name, appender_filename));
}
else
{
} else {
System.err.println("append file fail, error no: " + errno);
}
}
@ -273,19 +235,15 @@ public class TestAppender
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
{
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)
{
if (results != null) {
group_name = results[0];
remote_filename = results[1];
@ -296,47 +254,35 @@ public class TestAppender
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)
{
if (errno == 0) {
System.err.println(client.get_file_info(group_name, appender_filename));
}
else
{
} 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)
{
if (errno == 0) {
System.err.println(client.get_file_info(group_name, appender_filename));
}
else
{
} 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)
{
if (errno == 0) {
System.err.println(client.get_file_info(group_name, appender_filename));
}
else
{
} else {
System.err.println("truncate file fail, error no: " + errno);
}
}
else
{
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
if (storageServer == null)
{
if (storageServer == null) {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
@ -349,9 +295,7 @@ public class TestAppender
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
trackerServer.close();
}
catch(Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
}

View File

@ -1,40 +1,38 @@
/**
* 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
* <p>
* 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()
{
* client test
*
* @author Happy Fish / YuQing
* @version Version 1.20
*/
public class TestAppender1 {
private TestAppender1() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[])
{
if (args.length < 2)
{
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;
@ -45,8 +43,7 @@ public class TestAppender1
String conf_filename = args[0];
String local_filename = args[1];
try
{
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
@ -86,16 +83,12 @@ public class TestAppender1
group_name = null;
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null)
{
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
}
else
{
} 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());
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("");
}
@ -108,26 +101,19 @@ public class TestAppender1
group_name = "";
appender_file_id = client.upload_appender_file1(group_name, file_buff, "txt", meta_list);
*/
if (appender_file_id == null)
{
if (appender_file_id == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
}
else
{
} else {
System.err.println(client.get_file_info1(appender_file_id));
servers = tracker.getFetchStorages1(trackerServer, appender_file_id);
if (servers == null)
{
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
}
else
{
} 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());
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
@ -139,22 +125,17 @@ public class TestAppender1
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);
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)
{
if (errno == 0) {
System.err.println("set_metadata success");
}
else
{
} 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++)
{
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
@ -163,8 +144,7 @@ public class TestAppender1
file_buff = client.download_file1(appender_file_id);
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (file_buff != null)
{
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
@ -174,31 +154,24 @@ public class TestAppender1
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)
{
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
}
else
{
} 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)
{
if (errno == 0) {
System.err.println("Delete file success");
}
else
{
} 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)
{
if (appender_file_id != null) {
int ts;
String token;
String file_url;
@ -206,14 +179,12 @@ public class TestAppender1
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80)
{
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);
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;
}
@ -222,22 +193,16 @@ public class TestAppender1
System.err.println("file url: " + file_url);
errno = client.download_file1(appender_file_id, 0, 0, "c:\\" + appender_file_id.replaceAll("/", "_"));
if (errno == 0)
{
if (errno == 0) {
System.err.println("Download file success");
}
else
{
} 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)
{
if (errno == 0) {
System.err.println("Download file success");
}
else
{
} else {
System.err.println("Download file fail, error no: " + errno);
}
@ -245,12 +210,9 @@ public class TestAppender1
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)
{
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
}
else
{
} else {
System.err.println("append file fail, error no: " + errno);
}
}
@ -258,65 +220,49 @@ public class TestAppender1
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
{
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)
{
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)
{
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
}
else
{
} 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)
{
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
}
else
{
} 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)
{
if (errno == 0) {
System.err.println(client.get_file_info1(appender_file_id));
}
else
{
} else {
System.err.println("truncate file fail, error no: " + errno);
}
}
else
{
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage1(trackerServer, appender_file_id);
if (storageServer == null)
{
if (storageServer == null) {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
@ -329,9 +275,7 @@ public class TestAppender1
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
trackerServer.close();
}
catch(Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
}

View File

@ -1,40 +1,38 @@
/**
* 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
* <p>
* 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()
{
* client test
*
* @author Happy Fish / YuQing
* @version Version 1.18
*/
public class TestClient {
private TestClient() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[])
{
if (args.length < 2)
{
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;
@ -45,8 +43,7 @@ public class TestClient
String conf_filename = args[0];
String local_filename = args[1];
try
{
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
@ -90,16 +87,12 @@ public class TestClient
group_name = null;
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null)
{
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
}
else
{
} 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());
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("");
}
@ -112,29 +105,22 @@ public class TestClient
group_name = "";
results = client.upload_file(group_name, file_buff, "txt", meta_list);
*/
if (results == null)
{
if (results == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
}
else
{
} else {
group_name = results[0];
remote_filename = results[1];
System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
System.err.println(client.get_file_info(group_name, remote_filename));
servers = tracker.getFetchStorages(trackerServer, group_name, remote_filename);
if (servers == null)
{
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
}
else
{
} 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());
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
@ -146,22 +132,17 @@ public class TestClient
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);
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)
{
if (errno == 0) {
System.err.println("set_metadata success");
}
else
{
} 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++)
{
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
@ -172,8 +153,7 @@ public class TestClient
file_buff = client.download_file(group_name, remote_filename);
System.out.println("download_file time used: " + (System.currentTimeMillis() - startTime) + " ms");
if (file_buff != null)
{
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
@ -185,13 +165,11 @@ public class TestClient
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)
{
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]))
{
if (!generated_slave_filename.equals(results[1])) {
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
}
@ -201,19 +179,15 @@ public class TestClient
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)
{
if (errno == 0) {
System.err.println("Delete file success");
}
else
{
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
results = client.upload_file(local_filename, null, meta_list);
if (results != null)
{
if (results != null) {
String file_id;
int ts;
String token;
@ -226,14 +200,12 @@ public class TestClient
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80)
{
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);
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;
}
@ -243,22 +215,16 @@ public class TestClient
System.err.println("file url: " + file_url);
errno = client.download_file(group_name, remote_filename, 0, 0, "c:\\" + remote_filename.replaceAll("/", "_"));
if (errno == 0)
{
if (errno == 0) {
System.err.println("Download file success");
}
else
{
} 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)
{
if (errno == 0) {
System.err.println("Download file success");
}
else
{
} else {
System.err.println("Download file fail, error no: " + errno);
}
@ -268,13 +234,11 @@ public class TestClient
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)
{
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]))
{
if (!generated_slave_filename.equals(results[1])) {
System.err.println("generated slave file: " + generated_slave_filename + "\n != returned slave file: " + results[1]);
}
@ -285,19 +249,15 @@ public class TestClient
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
{
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)
{
if (results != null) {
group_name = results[0];
remote_filename = results[1];
@ -309,27 +269,22 @@ public class TestClient
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)
{
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]))
{
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
{
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage(trackerServer, group_name, remote_filename);
if (storageServer == null)
{
if (storageServer == null) {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
@ -342,9 +297,7 @@ public class TestClient
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
trackerServer.close();
}
catch(Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
}

View File

@ -1,40 +1,38 @@
/**
* 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
* <p>
* 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()
{
* client test
*
* @author Happy Fish / YuQing
* @version Version 1.16
*/
public class TestClient1 {
private TestClient1() {
}
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
* <ul><li>args[1]: local filename to upload</li></ul>
*/
public static void main(String args[])
{
if (args.length < 2)
{
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;
@ -46,8 +44,7 @@ public class TestClient1
String local_filename = args[1];
String group_name;
try
{
try {
ClientGlobal.init(conf_filename);
System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
System.out.println("charset=" + ClientGlobal.g_charset);
@ -78,16 +75,12 @@ public class TestClient1
group_name = "group1";
StorageServer[] storageServers = tracker.getStoreStorages(trackerServer, group_name);
if (storageServers == null)
{
if (storageServers == null) {
System.err.println("get store storage servers fail, error code: " + tracker.getErrorCode());
}
else
{
} 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());
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("");
}
@ -106,27 +99,20 @@ public class TestClient1
group_name = "group1";
file_id = client.upload_file1(group_name, file_buff, "txt", meta_list);
*/
if (file_id == null)
{
if (file_id == null) {
System.err.println("upload file fail, error code: " + client.getErrorCode());
return;
}
else
{
} else {
System.err.println("file_id: " + file_id);
System.err.println(client.get_file_info1(file_id));
ServerInfo[] servers = tracker.getFetchStorages1(trackerServer, file_id);
if (servers == null)
{
if (servers == null) {
System.err.println("get storage servers fail, error code: " + tracker.getErrorCode());
}
else
{
} 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());
for (int k = 0; k < servers.length; k++) {
System.err.println((k + 1) + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
}
System.err.println("");
}
@ -137,20 +123,15 @@ public class TestClient1
meta_list[2] = new NameValuePair("bgcolor", "#000000");
meta_list[3] = new NameValuePair("title", "Untitle");
if ((errno=client.set_metadata1(file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE)) == 0)
{
if ((errno = client.set_metadata1(file_id, meta_list, ProtoCommon.STORAGE_SET_METADATA_FLAG_MERGE)) == 0) {
System.err.println("set_metadata success");
}
else
{
} else {
System.err.println("set_metadata fail, error no: " + errno);
}
meta_list = client.get_metadata1(file_id);
if (meta_list != null)
{
for (int i=0; i<meta_list.length; i++)
{
if (meta_list != null) {
for (int i = 0; i < meta_list.length; i++) {
System.out.println(meta_list[i].getName() + " " + meta_list[i].getValue());
}
}
@ -158,8 +139,7 @@ public class TestClient1
//Thread.sleep(30000);
file_buff = client.download_file1(file_id);
if (file_buff != null)
{
if (file_buff != null) {
System.out.println("file length:" + file_buff.length);
System.out.println((new String(file_buff)));
}
@ -169,31 +149,25 @@ public class TestClient1
file_ext_name = "txt";
file_buff = "this is a slave buff.".getBytes(ClientGlobal.g_charset);
slave_file_id = client.upload_file1(master_file_id, prefix_name, file_buff, file_ext_name, meta_list);
if (slave_file_id != null)
{
if (slave_file_id != null) {
System.err.println("slave file_id: " + slave_file_id);
System.err.println(client.get_file_info1(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))
{
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);
}
}
//Thread.sleep(10000);
if ((errno=client.delete_file1(file_id)) == 0)
{
if ((errno = client.delete_file1(file_id)) == 0) {
System.err.println("Delete file success");
}
else
{
} else {
System.err.println("Delete file fail, error no: " + errno);
}
}
if ((file_id=client.upload_file1(local_filename, null, meta_list)) != null)
{
if ((file_id = client.upload_file1(local_filename, null, meta_list)) != null) {
int ts;
String token;
String file_url;
@ -204,36 +178,28 @@ public class TestClient1
inetSockAddr = trackerServer.getInetSocketAddress();
file_url = "http://" + inetSockAddr.getAddress().getHostAddress();
if (ClientGlobal.g_tracker_http_port != 80)
{
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);
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("file url: " + file_url);
errno = client.download_file1(file_id, 0, 100, "c:\\" + file_id.replaceAll("/", "_"));
if (errno == 0)
{
if (errno == 0) {
System.err.println("Download file success");
}
else
{
} else {
System.err.println("Download file fail, error no: " + errno);
}
errno = client.download_file1(file_id, new DownloadFileWriter("c:\\" + file_id.replaceAll("/", "-")));
if (errno == 0)
{
if (errno == 0) {
System.err.println("Download file success");
}
else
{
} else {
System.err.println("Download file fail, error no: " + errno);
}
@ -241,14 +207,12 @@ public class TestClient1
prefix_name = "-part2";
file_ext_name = null;
slave_file_id = client.upload_file1(master_file_id, prefix_name, local_filename, file_ext_name, meta_list);
if (slave_file_id != null)
{
if (slave_file_id != null) {
System.err.println("slave file_id: " + slave_file_id);
System.err.println(client.get_file_info1(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))
{
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);
}
}
@ -257,41 +221,32 @@ public class TestClient1
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
{
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)
{
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)
{
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))
{
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
{
} else {
System.err.println("Upload file fail, error no: " + errno);
}
storageServer = tracker.getFetchStorage1(trackerServer, file_id);
if (storageServer == null)
{
if (storageServer == null) {
System.out.println("getFetchStorage fail, errno code: " + tracker.getErrorCode());
return;
}
@ -304,9 +259,7 @@ public class TestClient1
System.out.println("active test to tracker server: " + ProtoCommon.activeTest(trackerServer.getSocket()));
trackerServer.close();
}
catch(Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
}

View File

@ -1,26 +1,22 @@
/**
* 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
* <p>
* 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
{
* 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;
@ -29,148 +25,161 @@ public class TestLoad
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()
{
private TestLoad() {
}
public int recv(long file_size, byte[] data, int bytes)
{
/**
* entry point
*
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
*/
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();
}
}
/**
* 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;
}
}
/**
* file uploader
* @author Happy Fish / YuQing
* @version Version 1.0
*/
public static class Uploader
{
/**
* file uploader
*
* @author Happy Fish / YuQing
* @version Version 1.0
*/
public static class Uploader {
public TrackerClient tracker;
public TrackerServer trackerServer;
public Uploader() throws Exception
{
public Uploader() throws Exception {
this.tracker = new TrackerClient();
this.trackerServer = tracker.getConnection();
}
public int uploadFile() throws Exception
{
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);
java.util.Arrays.fill(file_buff, (byte) 65);
try
{
try {
file_id = client.upload_file1(file_buff, "txt", null);
if (file_id == 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)
{
} 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
{
/**
* 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
{
public Downloader() throws Exception {
this.tracker = new TrackerClient();
this.trackerServer = tracker.getConnection();
this.callback = new DownloadFileDiscard();
}
public int downloadFile(String file_id) throws Exception
{
public int downloadFile(String file_id) throws Exception {
int errno;
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
try
{
try {
errno = client.download_file1(file_id, this.callback);
if (errno != 0)
{
if (errno != 0) {
System.out.println("Download file fail, file_id: " + file_id + ", error no: " + errno);
}
return errno;
}
catch(Exception ex)
{
} 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
{
/**
* 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)
{
public UploadThread(int index) {
this.thread_index = index;
}
public void run()
{
try
{
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++)
{
for (int i = 0; i < 50000; i++) {
TestLoad.total_upload_count++;
if (uploader.uploadFile() == 0)
{
if (uploader.uploadFile() == 0) {
TestLoad.success_upload_count++;
}
}
}
catch(Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
finally
{
} finally {
TestLoad.upload_thread_count--;
}
@ -182,85 +191,66 @@ public class TestLoad
}
}
/**
* download file thread
* @author Happy Fish / YuQing
* @version Version 1.0
*/
public static class DownloadThread extends Thread
{
private int thread_index;
/**
* 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)
{
public DownloadThread(int index) {
this.thread_index = index;
}
public void run()
{
try
{
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)
{
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)
{
synchronized (this.counter_lock) {
TestLoad.total_download_count++;
}
if (downloader.downloadFile(file_id) == 0)
{
synchronized (this.counter_lock)
{
if (downloader.downloadFile(file_id) == 0) {
synchronized (this.counter_lock) {
TestLoad.success_download_count++;
}
}
else
{
} 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)
{
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)
{
synchronized (this.counter_lock) {
TestLoad.total_download_count++;
}
if (downloader.downloadFile(file_id) == 0)
{
synchronized (this.counter_lock)
{
if (downloader.downloadFile(file_id) == 0) {
synchronized (this.counter_lock) {
TestLoad.success_download_count++;
}
}
else
{
} else {
TestLoad.fail_download_count++;
}
}
}
catch(Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
@ -269,48 +259,5 @@ public class TestLoad
+ ", success_download_count: " + TestLoad.success_download_count
+ ", fail_download_count: " + TestLoad.fail_download_count);
}
}
private TestLoad()
{
}
/**
* entry point
* @param args comand arguments
* <ul><li>args[0]: config filename</li></ul>
*/
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();
}
}
}

View File

@ -1,58 +1,53 @@
/**
* 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
* <p>
* 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
{
* 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)
{
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
{
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)
{
try {
while ((readBytes = fis.read(buff)) >= 0) {
if (readBytes == 0) {
continue;
}
out.write(buff, 0, readBytes);
}
}
finally
{
} finally {
fis.close();
}