⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mstringutil.java

📁 httptunnel.jar httptunnel java 源码
💻 JAVA
字号:
package net.jumperz.util;

import java.util.regex.*;
import java.util.*;
import java.io.*;
import java.nio.charset.UnsupportedCharsetException;
import java.security.*;
import java.text.*;
import java.net.*;
import java.beans.*;

public final class MStringUtil
{
private static final Pattern pattern1 = Pattern.compile( "[^-a-zA-Z0-9!\"#$%&')(*+,./:;<\\]>?@\\[\\\\^_`~|]=" );
//--------------------------------------------------------------------------------
public static boolean isPrintable( String str )
{
Matcher matcher = pattern1.matcher( str );
if( matcher.find() )
	{
	return false;
	}
else
	{
	return true;
	}
}
//--------------------------------------------------------------------------------
public static String loadStrFromFile( String fileName )
throws IOException
{
File file = new File( fileName );
if( !file.exists() )
	{
	throw new FileNotFoundException( fileName );
	}
int fileSize = ( int )file.length();
byte[] buffer = new byte[ fileSize ];
InputStream is = new FileInputStream( file );
int r = is.read( buffer, 0, fileSize );
if( r != fileSize )
	{
	throw new IOException( "File read error." );
	}
is.close();
return new String( buffer, "ISO-8859-1" );
}
// --------------------------------------------------------------------------------
public static String convertCharset( String target, String charFrom, String charTo )
{
try
	{
	byte[] buf = target.getBytes( "ISO-8859-1" );
	String s2 = new String( buf, charFrom );
	byte[] buf2 = s2.getBytes( charTo );
	String s3 = new String( buf2, "ISO-8859-1" );
	return s3;
	}
catch( IOException e )
	{
	e.printStackTrace();
	return target;
	}
}
// --------------------------------------------------------------------------------
public static boolean meansTrue( String t )
{
if( t.equalsIgnoreCase( "t" ) 
 || t.equalsIgnoreCase( "y" ) 
 || t.equalsIgnoreCase( "on" ) 
 || t.equalsIgnoreCase( "yes" ) 
 || t.equalsIgnoreCase( "true" ) 
 ||  t.equalsIgnoreCase( "1" ) 
  )
	{
	return true;
	}
return false;
}
// --------------------------------------------------------------------------------
public static String urlEncode( String target, String charset )
{
try
	{
	target = URLEncoder.encode( target, charset );			
	}
catch( UnsupportedEncodingException ignored )
	{
	}
return target;
}
//--------------------------------------------------------------------------------
public static String urlDecode( String target, String charset )
throws IllegalArgumentException
{
try
	{
	//target = URLDecoder.decode( target, charset );
	target = URLDecoder.decode( target, MCharset.CS_ISO_8859_1 );
	byte[] buf = target.getBytes( MCharset.CS_ISO_8859_1 );
	return new String( buf, charset );
	}
catch( UnsupportedEncodingException ignored )
	{
	}
//catch( IllegalArgumentException ignored )
	{
	}
return target;
}
// --------------------------------------------------------------------------------
public static String urlDecode( String target )
throws IllegalArgumentException
{
try
	{
	target = URLDecoder.decode( target, MCharset.CS_ISO_8859_1 );			
	}
catch( UnsupportedEncodingException ignored )
	{
	}
//catch( IllegalArgumentException ignored )
	{
	}
return target;
}
// --------------------------------------------------------------------------------
public static String replaceFirst( String target, String from, String to )
{
StringBuffer s = new StringBuffer( target.length() );
int index = target.indexOf( from );
if( index == -1 )
	{
	return target;
	}
s.append( target.substring( 0, index ) );
s.append( to );
s.append( target.substring( index + from.length() ) );
return s.toString();
}
//--------------------------------------------------------------------------------
public static String replaceAll( String target, String from, String to )
{
int length = from.length();
while( true )
	{
	int index = target.indexOf( from );
	if( index == -1 )
		{
		break;
		}
	StringBuffer strBuf = new StringBuffer();
	strBuf.append( target.substring( 0, index ) );
	strBuf.append( to );
	strBuf.append( target.substring( index + from.length() ) );
	target = strBuf.toString();
	}

return target;
}
//--------------------------------------------------------------------------------
public static String normalize( String path )
{
if( path == null )
	{
	return null;
	}
if ( path.equals( "/." ) )
	{
	return "/";
	}

String normalized = path;

while( normalized.indexOf( '\\' ) > -1 )
	{
	normalized = normalized.replace( '\\', '/' );
	}

boolean flag1 = false;
if( !normalized.startsWith( "/" ) )
	{
	normalized = "/" + normalized;
	flag1 = true;
	}

while( normalized.indexOf( "//" ) > -1 )
	{
	normalized = normalized.replaceFirst( "//", "/" );
	}

while( normalized.indexOf( "/./" ) > -1 )
	{
	normalized = normalized.replaceFirst( "/\\./", "/" );
	}

StringBuffer buf = new StringBuffer();
while( true )
	{
	int index = normalized.indexOf( "/../" );
	if( index < 0 )
		{
		break;
		}
	else if( index == 0 )
		{
		buf.append( "/.." );
		normalized = normalized.substring( 3 );
		}
	else
		{
		int index2 = normalized.lastIndexOf( '/', index - 1 );
		normalized = normalized.substring( 0, index2 ) + normalized.substring( index + 3 );
		}
	}
buf.append( normalized );

if( flag1 )
	{
	return buf.toString().substring( 1 );
	}
else
	{
	return buf.toString();
	}
}
//---------------------------------------------------------
public static final String generateRandomString()
{
Calendar cal = Calendar.getInstance();
long currentTimeLong = cal.getTimeInMillis();

Random random = new Random();
int id1 = random.nextInt( 10000 );
int id2 = random.nextInt( 10000 );

return String.valueOf( id1 ) + String.valueOf( currentTimeLong ) + String.valueOf( id2 );
}
//--------------------------------------------------------------------------------
public static final String hexDecode( String in )
{
int length = in.length();
byte[] data = new byte[ length/2 ];
for( int i = 0, j = 0; i < length; i+=2, ++j )
	{
	char c1 = in.charAt( i );
	char c2 = in.charAt( i + 1 );
	int i1 = Character.digit( c1, 16 ) * 16;
	int i2 = Character.digit( c2, 16 );
	data[ j ] = ( byte )( i1 + i2 );
	}

String out = null;
try
	{
	out = new String( data, "ISO-8859-1" );
	}
catch( UnsupportedEncodingException e )
	{
	e.printStackTrace();
	}
return  out;
}
//-------------------------------------------------------------------------------
public static final String byteToHexString( byte[] data )
{
StringBuffer strBuf = new StringBuffer( data.length * 2 );

int length = data.length;
for( int i = 0; i < length; ++i )
	{
	int j = data[ i ];
	if( j < 0 )
		{
		j += 256;
		}
	String tmpStr = Integer.toHexString( j );
	if( tmpStr.length() == 1 )
		{
		strBuf.append( "0" );
		}
	strBuf.append( tmpStr );
	}

return strBuf.toString();
}
// --------------------------------------------------------------------------------
public static final byte[] hexStringToByteArray( String s )
{
byte[] array = new byte[ s.length()/2 ];
for( int i = 0, j = 0; ( i + 1 ) < s.length(); i+=2, j++ )
	{
	int k = Integer.parseInt( s.substring( i, i + 2 ), 16 );
	array[ j ] = ( byte )k;
	}
return array;
}
// --------------------------------------------------------------------------------
public static String byteArrayToString( byte[] b )
{
return byteArrayToString( b, MCharset.CS_ISO_8859_1 );
}
// --------------------------------------------------------------------------------
public static String byteArrayToString( byte[] b, String enc )
{
try
	{
	return new String( b, enc );
	}
catch( UnsupportedEncodingException e )
	{
	e.printStackTrace();
	return null;
	}
}
// --------------------------------------------------------------------------------
public static final byte[] getBytes( String s, String enc )
{
try
	{
	byte[] buf = s.getBytes( enc );
	return buf;
	}
catch( UnsupportedEncodingException e )
	{
	e.printStackTrace();
	return null;
	}
}
// --------------------------------------------------------------------------------
public static final byte[] getBytes( String s )
{
return getBytes( s, MCharset.CS_ISO_8859_1 );
}
//-------------------------------------------------------------------------------
public static final String jisToSjis( String jisStr )
throws Exception
{
String sjisStr = new String( jisStr.getBytes( "JIS" ) );
return sjisStr;
}
//-------------------------------------------------------------------------------
public static String getHostFromPeer( String peer )
{
int pos = peer.indexOf( ":" );
if( pos == -1 )
	{
	return peer;
	}
else
	{
	return peer.substring( 0, pos );
	}
}
//-------------------------------------------------------------------------------
public static int getPortFromPeer( String peer )
{
int pos = peer.indexOf( ":" );
if( pos == -1 )
	{
	return 80;
	}
else
	{
	String portStr = peer.substring( pos + 1 );
	return Integer.parseInt( portStr );
	}
}
// --------------------------------------------------------------------------------
public static int parseInt( String s, int defaultValue )
{
int i;
try
	{
	i = Integer.parseInt( s );
	return i;
	}
catch( NumberFormatException e )
	{
	return defaultValue;
	}
}
//-------------------------------------------------------------------------------
public static ArrayList getErrorAddrList( net.jumperz.net.MMail mail )
throws IOException
{
ArrayList errorAddrList = new ArrayList();
String mailBody = mail.getBody();
byte[] mailBodyBuf = mailBody.getBytes();
BufferedReader reader = new BufferedReader( new InputStreamReader( new ByteArrayInputStream( mailBodyBuf ) ) );
String line;
while( true )
	{
	line = reader.readLine();
	if( line == null )
		{
		break;
		}
	String errorAddr = MRegEx.getMatch( " ([^ ]{1,}@docomo.ne.jp)", line );
	if( !errorAddr.equals( "" ) )
		{
		errorAddrList.add( errorAddr );
		}
	}
return errorAddrList;
}
//-------------------------------------------------------------------------------
public static boolean isComment( String line )
{
if( line.indexOf( "#" ) == 0
 || line.indexOf( " " ) == 0
 || line.indexOf( "	" ) == 0
 || line.indexOf( "//" ) == 0 
 || line.equals( "" )
  )
	{
	return true;
	}
else
	{
	return false;
	}
}
// --------------------------------------------------------------------------------
public static void saveStringToFile( String str, String fileName, String charset )
throws IOException
{
OutputStream os = new FileOutputStream( fileName );
os.write( str.getBytes( charset ) );
os.close();
}
//--------------------------------------------------------------------------------
public static void saveCollectionToFile( Collection collection, String fileName )
throws IOException
{
BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( fileName ) );
try
	{
	Iterator p = collection.iterator();
	while( p.hasNext() )
		{
		String str = ( String )p.next();
		out.write( str.getBytes( "ISO-8859-1" ) );
		out.write( "\r\n".getBytes( "ISO-8859-1" ) );
		}
	}
finally
	{
	out.close();
	}
}
//--------------------------------------------------------------------------------
public static Set loadSetFromFile( String fileName )
throws IOException
{
BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( fileName ), MCharset.CS_ISO_8859_1 ) );
String line;
Set set = new HashSet( 20 );
try
	{
	while( true )
		{
		line = reader.readLine();
		if( line == null )
			{
			break;
			}
		if( !MStringUtil.isComment( line ) )
			{
			set.add( line );
			}
		}
	}
finally
	{
	reader.close();
	}

return set;
}
//-------------------------------------------------------------------------------
public static List loadListFromFile( String fileName )
throws IOException
{
BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( fileName ), MCharset.CS_ISO_8859_1 ) );
String line;
List list = new ArrayList( 20 );
try
	{
	while( true )
		{
		line = reader.readLine();
		if( line == null )
			{
			break;
			}
		if( !MStringUtil.isComment( line ) )
			{
			list.add( line );
			}
		}
	}
finally
	{
	reader.close();
	}
return list;
}
//-------------------------------------------------------------------------------
public static final String getMd5Hash( String str )
{
MessageDigest md = null;
try
	{
	md = MessageDigest.getInstance( "MD5" );
	}
catch( NoSuchAlgorithmException e )
	{
	e.printStackTrace();
	}
md.update( str.getBytes() );
return MStringUtil.byteToHexString( md.digest() );
}
//----------------------------------------------------------------------------
public static final String toDateString( long time, String s )
{
DecimalFormat format = new DecimalFormat( "00" );
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis( time );

StringBuffer strbuf = new StringBuffer();
strbuf.append( cal.get( Calendar.YEAR ) );
strbuf.append( s );
strbuf.append( format.format( cal.get( Calendar.MONTH ) + 1 ) );
strbuf.append( s );
strbuf.append( format.format( cal.get( Calendar.DAY_OF_MONTH ) ) );
return strbuf.toString();
}
//----------------------------------------------------------------------------
public static final String toDateString( long time )
{
return toDateString( time, "/" );
}
//--------------------------------------------------------------------------------
public static long ipToLong( String ip )
{
String[] array = ip.split( "\\." );
if( array.length != 4 )
	{
	throw new NumberFormatException( "invalid IP address format" );
	}
long l;
l  = 256L * 256L * 256L * Integer.parseInt( array[ 0 ] );
l += 256L * 256L        * Integer.parseInt( array[ 1 ] );
l += 256L               * Integer.parseInt( array[ 2 ] );
l +=                      Integer.parseInt( array[ 3 ] );

return l;
}
// --------------------------------------------------------------------------------
public static String objectToString( Object o )
throws IOException
{
if( o == null )
	{
	return "";
	}
ByteArrayOutputStream buf = new ByteArrayOutputStream();
XMLEncoder e = new XMLEncoder( buf );
e.writeObject( o );
e.close();
String s = MStreamUtil.streamToString( new ByteArrayInputStream( buf.toByteArray() ) );
return URLEncoder.encode( s, MCharset.CS_ISO_8859_1 );
}
// --------------------------------------------------------------------------------
public static Object stringToObject( String s )
throws IOException
{
String s2 = URLDecoder.decode( s, MCharset.CS_ISO_8859_1 );
ByteArrayInputStream buf = new ByteArrayInputStream( s2.getBytes( MCharset.CS_ISO_8859_1 ) );
XMLDecoder d = new XMLDecoder( buf );
Object o = d.readObject();
d.close();

return o;
}
//-------------------------------------------------------------------------------
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -