📄 utils.java
字号:
String t = strings[i];
strings[i] = strings[j];
strings[j] = t;
}
}
}
}
/// Locates a String in an array of Strings.
// Returns -1 if the String is not found.
public static int indexOfString( String[] strings, String string )
{
for ( int i = 0; i < strings.length; ++i )
if ( string.equals( strings[i] ) )
return i;
return -1;
}
/// Locates a String in an array of Strings, ignoring case.
// Returns -1 if the String is not found.
public static int indexOfStringIgnoreCase( String[] strings, String string )
{
for ( int i = 0; i < strings.length; ++i )
if ( string.equalsIgnoreCase( strings[i] ) )
return i;
return -1;
}
/// Compares two arrays of Strings for equality.
public static boolean equalsStrings( String[] strings1, String[] strings2 )
{
if ( strings1.length != strings2.length )
return false;
for ( int i = 0; i < strings1.length; ++i )
if ( ! strings1[i].equals( strings2[i] ) )
return false;
return true;
}
/// Returns the number a raised to the power of b. Long version
// of Math.pow(). Throws ArithmeticException if b is negative.
public static long pow( long a, long b ) throws ArithmeticException
{
if ( b < 0 )
throw new ArithmeticException();
long r = 1;
while ( b != 0 )
{
if ( odd( b ) )
r *= a;
b >>>= 1;
a *= a;
}
return r;
}
/// Parse an integer, returning a default value on errors.
public static int parseInt( String str, int def )
{
try
{
return Integer.parseInt( str );
}
catch ( Exception e )
{
return def;
}
}
/// Parse a long, returning a default value on errors.
public static long parseLong( String str, long def )
{
try
{
return Long.parseLong( str );
}
catch ( Exception e )
{
return def;
}
}
/// An array-to-String routine. Handles arrays of arbitrary
// type, including nested arrays. Sample output:
// <BLOCKQUOTE><CODE><PRE>
// byte[]: { (byte)0, (byte)1, (byte)2 }
// char[]: { '0', '1', '2' }
// short[]: { (short)0, (short)1, (short)2 }
// int[]: { 0, 1, 2 }
// long[]: { 0L, 1L, 2L }
// float[]: { 0F, 1F, 2F }
// double[]: { 0D, 1D, 2D }
// String[]: { "0", "1", "2" }
// int[][]: { { 0, 1, 2 }, { 3, 4, 5 } }
// </PRE></CODE></BLOCKQUOTE>
public static String arrayToString( Object o )
{
if ( o == null )
return "null";
String cl = o.getClass().getName();
if ( ! cl.startsWith( "[" ) )
// It's not an array; just call its toString method.
return o.toString();
StringBuffer sb = new StringBuffer( "{ " );
if ( o instanceof byte[] )
{
byte[] ba = (byte[]) o;
for ( int i = 0; i < ba.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( "(byte)" );
sb.append( ba[i] );
}
}
else if ( o instanceof char[] )
{
char[] ca = (char[]) o;
for ( int i = 0; i < ca.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( "'" );
sb.append( ca[i] );
sb.append( "'" );
}
}
else if ( o instanceof short[] )
{
short[] sa = (short[]) o;
for ( int i = 0; i < sa.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( "(short)" );
sb.append( sa[i] );
}
}
else if ( o instanceof int[] )
{
int[] ia = (int[]) o;
for ( int i = 0; i < ia.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( ia[i] );
}
}
else if ( o instanceof long[] )
{
long[] la = (long[]) o;
for ( int i = 0; i < la.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( la[i] );
sb.append( "L" );
}
}
else if ( o instanceof float[] )
{
float[] fa = (float[]) o;
for ( int i = 0; i < fa.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( fa[i] );
sb.append( "F" );
}
}
else if ( o instanceof double[] )
{
double[] da = (double[]) o;
for ( int i = 0; i < da.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( da[i] );
sb.append( "D" );
}
}
else if ( o instanceof String )
{
// Special-case Strings so we can surround them with quotes.
String[] sa = (String[]) o;
for ( int i = 0; i < sa.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( "\"" );
sb.append( sa[i] );
sb.append( "\"" );
}
}
else if ( cl.startsWith( "[L" ) )
{
// Some random class.
Object[] oa = (Object[]) o;
for ( int i = 0; i < oa.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( oa[i] );
}
}
else if ( cl.startsWith( "[[" ) )
{
// Nested arrays.
Object[] aa = (Object[]) o;
for ( int i = 0; i < aa.length; ++i )
{
if ( i > 0 ) sb.append( ", " );
sb.append( arrayToString( aa[i] ) );
}
}
else
sb.append( "(unknown array type)" );
sb.append( " }" );
return sb.toString();
}
/// Check if an object extends a given class or one of its superclasses.
// An instanceof that works on Class objects at runtime, instead
// of type descriptors at compile time.
public static boolean instanceOf( Object o, Class cl )
{
// Null check.
if ( o == null || cl == null )
return false;
Class ocl = o.getClass();
// Check if they are the same class.
if ( ocl.equals( cl ) )
return true;
// If the class is not itself an interface, then check its interfaces.
if ( ! cl.isInterface() )
{
Class ifs[] = cl.getInterfaces();
for ( int i = 0; i < ifs.length; ++i )
if ( instanceOf( o, ifs[i] ) )
return true;
}
// And check supeclasses.
Class scl = cl.getSuperclass();
if ( scl != null )
if ( instanceOf( o, scl ) )
return true;
// Guess not.
return false;
}
/// Test is a number is even.
public static boolean even( long n )
{
return ( n & 1 ) == 0;
}
/// Test is a number is odd.
public static boolean odd( long n )
{
return ( n & 1 ) != 0;
}
/// Count the number of 1-bits in a byte.
public static int countOnes( byte n )
{
return countOnes( n & 0xffL );
}
/// Count the number of 1-bits in an int.
public static int countOnes( int n )
{
return countOnes( n & 0xffffffffL );
}
/// Count the number of 1-bits in a long.
public static int countOnes( long n )
{
// There are faster ways to do this, all the way up to looking
// up bytes in a 256-element table. But this is not too bad.
int count = 0;
while ( n != 0 )
{
if ( odd( n ) )
++count;
n >>>= 1;
}
return count;
}
/// A fixed version of java.io.InputStream.read(byte[], int, int). The
// standard version catches and ignores IOExceptions from below.
// This version sends them on to the caller.
public static int read( InputStream in, byte[] b, int off, int len ) throws IOException
{
if ( len <= 0 )
return 0;
int c = in.read();
if ( c == -1 )
return -1;
if ( b != null )
b[off] = (byte) c;
int i;
for ( i = 1; i < len ; ++i )
{
c = in.read();
if ( c == -1 )
break;
if ( b != null )
b[off + i] = (byte) c;
}
return i;
}
/// A version of read that reads the entire requested block, instead
// of sometimes terminating early.
// @return -1 on EOF, otherwise len
public static int readFully( InputStream in, byte[] b, int off, int len ) throws IOException
{
int l, r;
for ( l = 0; l < len; )
{
r = read( in, b, l, len - l );
if ( r == -1 )
return -1;
l += r;
}
return len;
}
/// Make a URL with no ref part and no query string. Also, if it's
// a directory then make sure there's a trailing slash.
public static URL plainUrl( URL context, String urlStr ) throws MalformedURLException
{
URL url = new URL( context, urlStr );
String fileStr = url.getFile();
int i = fileStr.indexOf( '?' );
if ( i != -1 )
fileStr = fileStr.substring( 0, i );
url = new URL(
url.getProtocol(), url.getHost(), url.getPort(), fileStr );
if ( ( ! fileStr.endsWith( "/" ) ) &&
urlStrIsDir( url.toExternalForm() ) )
{
fileStr = fileStr + "/";
url = new URL(
url.getProtocol(), url.getHost(), url.getPort(), fileStr );
}
return url;
}
/// Make a URL with no ref part and no query string. Also, if it's
// a directory then make sure there's a trailing slash.
public static URL plainUrl( String urlStr ) throws MalformedURLException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -