Bug fix and some other things

This commit is contained in:
Ziver Koc 2009-03-31 17:56:23 +00:00
parent 9297bea93d
commit 7cb0fdb435
4 changed files with 129 additions and 37 deletions

View file

@ -35,8 +35,8 @@ public class MultiPrintStream extends PrintStream {
}
/**
* this constructor makes a simple PrintStream that prints to the console and to a file
* @param file the file name to output to
* This constructor makes a simple PrintStream that prints to the console and to a file
* @param file is the file name to output to
*/
public MultiPrintStream(String file){
super(new PrintStream(System.out));
@ -51,8 +51,8 @@ public class MultiPrintStream extends PrintStream {
}
/**
* this constructor takes a array of PrintStreams to be used
* @param streams a array of the streams that will be used
* This constructor takes a array of PrintStreams to be used
* @param streams is a array of the streams that will be used
*/
public MultiPrintStream(PrintStream[] streams){
super(streams[0]);
@ -63,39 +63,39 @@ public class MultiPrintStream extends PrintStream {
}
/**
* this constructor takes a array of PrintStreams to be used
* @param streams a array of the streams that will be used
* This constructor takes a array of PrintStreams to be used
* @param streams is a array of the streams that will be used
*/
public static void makeInstance(MultiPrintStream instanceStream){
out = instanceStream;
}
/**
* Adds a printstream to the list pf streams
* @param p The Printstream to add
* Adds a PrintStream to the list of streams
* @param p is the PrintStream to add
*/
public void addPrintStream(PrintStream p){
streams.add(p);
}
/**
* Remove a printstream from the list
* @param p The PrintStream to remove
* Remove a PrintStream from the list
* @param p is the PrintStream to remove
*/
public void removePrintStream(PrintStream p){
streams.remove(p);
}
/**
* Remove a printstream from the list
* @param p The index of the PrintStream to remove
* Remove a PrintStream from the list
* @param p is the index of the PrintStream to remove
*/
public void removePrintStream(int p){
streams.remove(p);
}
/**
* prints whit a new line to all the PrintStreams
* Prints with a new line to all the PrintStreams
*/
public void println(String s){
if(!s.equals(""))s = getTime() + s;
@ -104,7 +104,7 @@ public class MultiPrintStream extends PrintStream {
}
/**
* prints to all the PrintStreams
* Prints to all the PrintStreams
*/
public void print(String s){
for(int i=0; i<streams.size() ;i++)
@ -132,7 +132,7 @@ public class MultiPrintStream extends PrintStream {
/**
* If the streams should print timestamp in front
* If the streams should print time stamp in front
* of the msgs
* @param enable True to activate
*/
@ -142,7 +142,7 @@ public class MultiPrintStream extends PrintStream {
/**
* The DateFormat to print in the time stamp
* @param ts String to send to SimpleDateFormat
* @param ts is the String to send to SimpleDateFormat
*/
public void setTimeStamp(String ts){
timeStampString = ts;
@ -173,18 +173,17 @@ public class MultiPrintStream extends PrintStream {
/**
* Dumps the content of:
* - Array content
* - Map content (HashMap etc.)
* - List content (ArrayList, LinkedList etc.)
* - InputStream content (Prints out until the end of the stream)
* - Reader content (Prints out until the end of the reader)
* - Instance variables of a Object
* <br>- Array content
* <br>- Map content (HashMap etc.)
* <br>- List content (ArrayList, LinkedList etc.)
* <br>- InputStream content (Prints out until the end of the stream)
* <br>- Reader content (Prints out until the end of the reader)
* <br>- Instance variables of a Object
*
* @param o The Object to dump
* @return A String with all the printed data
* @param o is the Object to dump
*/
public String dump( Object o ){
return dump( o , true);
public void dump( Object o ){
dumpToString( o );
}
/**
@ -196,12 +195,12 @@ public class MultiPrintStream extends PrintStream {
* <br>- Reader content (Prints out until the end of the reader)
* <br>- Instance variables of a Object
*
* @param o The Object to dump
* @param print If the method should print the data or just return it
* @param o is the Object to dump
* @param print is if the method should print the data or just return it
* @return A String with all the printed data
*/
@SuppressWarnings("unchecked")
private String dump( Object o , boolean print) {
public String dumpToString( Object o ) {
if(o == null) return "NULL";
StringBuffer buffer = new StringBuffer();
Class oClass = o.getClass();
@ -213,7 +212,7 @@ public class MultiPrintStream extends PrintStream {
if ( i > 0 )
buffer.append( ", " );
Object value = Array.get(o,i);
buffer.append( (dumbCapable(value) ? dump(value,false) : value) );
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
}
buffer.append( "]" );
}
@ -223,7 +222,7 @@ public class MultiPrintStream extends PrintStream {
buffer.append( "{" );
while(it.hasNext()){
Object value = it.next();
buffer.append( (dumbCapable(value) ? dump(value,false) : value) );
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
if(it.hasNext())
buffer.append( ", " );
}
@ -238,7 +237,7 @@ public class MultiPrintStream extends PrintStream {
Object value = ((Map)o).get(key);
buffer.append( key );
buffer.append( "=>" );
buffer.append( (dumbCapable(value) ? dump(value,false) : value) );
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
if(it.hasNext())
buffer.append( ", " );
}
@ -288,7 +287,7 @@ public class MultiPrintStream extends PrintStream {
try {
Object value = fields[i].get(o);
if (value != null) {
buffer.append( (dumbCapable(value) ? dump(value,false) : value) );
buffer.append( (dumbCapable(value) ? dumpToString(value) : value) );
}
} catch ( IllegalAccessException e ) {}
}
@ -297,14 +296,18 @@ public class MultiPrintStream extends PrintStream {
buffer.append( "}" );
}
if(print) out.println(buffer.toString());
return buffer.toString();
}
/**
* An helper function for the dump function.
*/
private boolean dumbCapable(Object o){
if(o.getClass().isArray()) return true;
else if(o instanceof Collection)return true;
else if(o instanceof Map)return true;
if(o != null){
if(o.getClass().isArray()) return true;
else if(o instanceof Collection)return true;
else if(o instanceof Map)return true;
}
return false;
}
}

View file

@ -0,0 +1,87 @@
/**
*
*/
package zutil.algo;
import java.math.BigInteger;
/**
* The algorithm solves the discreet log equation x^2 = n mod p
*
* @author Ziver
* @see http://en.wikipedia.org/wiki/Shanks-Tonelli_algorithm
*/
public class ShanksTonelliAlgo {
public static BigInteger calc(BigInteger n, BigInteger p){
BigInteger nOrg = n;
BigInteger S = null, V, R, U, X;
BigInteger ONE = BigInteger.ONE;
BigInteger TWO = BigInteger.valueOf( 2 );
BigInteger Q = p.add( ONE ).divide( TWO );
switch( p.mod( BigInteger.valueOf(4) ).intValue() ){
case 3:
S = n.pow( Q.divide( TWO ).intValue() ).mod( p );
break;
case 1:
S = ONE;
n = n.subtract( ONE );
while (n.divide( p ).compareTo( ONE ) == 0) {
S = S.add( ONE );
//n = (n-2s+1) mod p
n = n.subtract( TWO.multiply( S ) ).add( ONE ).mod( p );
if (n.compareTo( BigInteger.ZERO ) == 0){
return S;
}
}
Q = Q.divide( TWO );
V = ONE;
R = S;
U = ONE;
while (Q.compareTo( BigInteger.ZERO ) > 0) {
X = R.pow(2).subtract( n.multiply( U.pow(2) ) ).mod( p );
U = TWO.multiply( R ).multiply( U ).mod( p );
R = X;
if ( Q.testBit(0) ){
X = S.multiply( R ).subtract( n.multiply(V).multiply(U) ).mod( p );
V = V.multiply(R).add( S.multiply(U) ).mod( p );
S = X;
}
Q = Q.divide( TWO );
}
}
if( S != null && S.multiply( S ).mod( p ).compareTo( nOrg ) != 0 ){
return null;
}
return S;
/*
//p-1 = Q*2^S
BigInteger S = null, Q = null, R = null, V = null, W = null;
//Q = ( 2^S )/( 1-p );
p-1 = ( 2^S )/( 1-p ) * 2^S;
// R = n^( (Q+1)/2 ) mod p
R = n.pow( Q.add(BigInteger.ONE).divide(BigInteger.valueOf(2)).intValue() ).mod( p );
// V = W^Q mod p
V = W.pow( Q.intValue() ).mod( p );
for(int i=S.intValue(); true ;){
while( true ){
i--;
// 1 = ( ( R^2 * n^-1 )^2^i ) mod p
if( ( R.pow(2).multiply( n.pow(-1) ) ).pow( (int)Math.pow(2, i) ).mod( p ).compareTo( BigInteger.ONE ) == 0 )
break;
}
if(i == 0) return R;
//R = ( RV^(2^(S-i-1)) ) mod p
else R = ( R.multiply( V.pow( (int)Math.pow( 2, S.intValue()-i-1) ) )).mod( p );
}
*/
}
}

View file

@ -46,6 +46,7 @@ public class WienersAlgo {
d1 = g.multiply( d1 ).add( d0 );
d0 = t;
// (d1*e-1) % c1 == 0
n1 = d1.multiply( e ).subtract( BigInteger.ONE );
if( n1.mod( c1 ).equals( BigInteger.ZERO ) ){
n1 = n1.divide( c1 );

View file

@ -312,6 +312,7 @@ public abstract class NioNetwork implements Runnable {
//pendingReadData.get(socketChannel).clear();
}catch(Exception e){
e.printStackTrace();
//pendingReadData.get(socketChannel).reset();
}
}