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

📄 miscutilities.java

📁 Linux下面最好用的程序、文本编辑工具之一。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			char ch = str.charAt(i);			switch(ch)			{			case '<':				buf.append("&lt;");				break;			case '>':				buf.append("&gt;");				break;			case '&':				buf.append("&amp;");				break;			default:				buf.append(ch);				break;			}		}		return buf.toString();	} //}}}	//{{{ formatFileSize() method	public static final DecimalFormat KB_FORMAT = new DecimalFormat("#.# KB");	public static final DecimalFormat MB_FORMAT = new DecimalFormat("#.# MB");	/**	 * Formats the given file size into a nice string (123 bytes, 10.6 KB,	 * 1.2 MB).	 * @param length The size	 * @since jEdit 4.2pre1	 */	public static String formatFileSize(long length)	{		if(length < 1024)			return length + " bytes";		else if(length < 1024*1024)			return KB_FORMAT.format((double)length / 1024);		else			return MB_FORMAT.format((double)length / 1024 / 1024);	} //}}}	//{{{ getLongestPrefix() method	/**	 * Returns the longest common prefix in the given set of strings.	 * @param str The strings	 * @param ignoreCase If true, case insensitive	 * @since jEdit 4.2pre2	 */	public static String getLongestPrefix(List str, boolean ignoreCase)	{		if(str.size() == 0)			return "";		int prefixLength = 0;loop:		for(;;)		{			String s = str.get(0).toString();			if(prefixLength >= s.length())				break loop;			char ch = s.charAt(prefixLength);			for(int i = 1; i < str.size(); i++)			{				s = str.get(i).toString();				if(prefixLength >= s.length())					break loop;				if(!compareChars(s.charAt(prefixLength),ch,ignoreCase))					break loop;			}			prefixLength++;		}		return str.get(0).toString().substring(0,prefixLength);	} //}}}	//{{{ getLongestPrefix() method	/**	 * Returns the longest common prefix in the given set of strings.	 * @param str The strings	 * @param ignoreCase If true, case insensitive	 * @since jEdit 4.2pre2	 */	public static String getLongestPrefix(String[] str, boolean ignoreCase)	{		return getLongestPrefix((Object[])str,ignoreCase);	} //}}}	//{{{ getLongestPrefix() method	/**	 * Returns the longest common prefix in the given set of strings.	 * @param str The strings (calls <code>toString()</code> on each object)	 * @param ignoreCase If true, case insensitive	 * @since jEdit 4.2pre6	 */	public static String getLongestPrefix(Object[] str, boolean ignoreCase)	{		if(str.length == 0)			return "";		int prefixLength = 0;		String first = str[0].toString();loop:		for(;;)		{			if(prefixLength >= first.length())				break loop;			char ch = first.charAt(prefixLength);			for(int i = 1; i < str.length; i++)			{				String s = str[i].toString();				if(prefixLength >= s.length())					break loop;				if(!compareChars(s.charAt(prefixLength),ch,ignoreCase))					break loop;			}			prefixLength++;		}		return first.substring(0,prefixLength);	} //}}}	//}}}	//{{{ Sorting methods	//{{{ quicksort() method	/**	 * Sorts the specified array. Equivalent to calling	 * <code>Arrays.sort()</code>.	 * @param obj The array	 * @param compare Compares the objects	 * @since jEdit 4.0pre4	 */	public static void quicksort(Object[] obj, Comparator compare)	{		Arrays.sort(obj,compare);	} //}}}	//{{{ quicksort() method	/**	 * Sorts the specified vector.	 * @param vector The vector	 * @param compare Compares the objects	 * @since jEdit 4.0pre4	 */	public static void quicksort(Vector vector, Comparator compare)	{		Collections.sort(vector,compare);	} //}}}	//{{{ quicksort() method	/**	 * Sorts the specified list.	 * @param list The list	 * @param compare Compares the objects	 * @since jEdit 4.0pre4	 */	public static void quicksort(List list, Comparator compare)	{		Collections.sort(list,compare);	} //}}}	//{{{ quicksort() method	/**	 * Sorts the specified array. Equivalent to calling	 * <code>Arrays.sort()</code>.	 * @param obj The array	 * @param compare Compares the objects	 */	public static void quicksort(Object[] obj, Compare compare)	{		Arrays.sort(obj,compare);	} //}}}	//{{{ quicksort() method	/**	 * Sorts the specified vector.	 * @param vector The vector	 * @param compare Compares the objects	 */	public static void quicksort(Vector vector, Compare compare)	{		Collections.sort(vector,compare);	} //}}}	//{{{ Compare interface	/**	 * An interface for comparing objects. This is a hold-over from	 * they days when jEdit had its own sorting API due to JDK 1.1	 * compatibility requirements. Use <code>java.util.Comparable</code>	 * instead.	 */	public interface Compare extends Comparator	{		int compare(Object obj1, Object obj2);	} //}}}	//{{{ StringCompare class	/**	 * Compares strings.	 */	public static class StringCompare implements Compare	{		public int compare(Object obj1, Object obj2)		{			return compareStrings(obj1.toString(),				obj2.toString(),false);		}	} //}}}	//{{{ StringICaseCompare class	/**	 * Compares strings ignoring case.	 */	public static class StringICaseCompare implements Compare	{		public int compare(Object obj1, Object obj2)		{			return compareStrings(obj1.toString(),				obj2.toString(),true);		}	} //}}}	//{{{ MenuItemCompare class	/**	 * Compares menu item labels.	 */	public static class MenuItemCompare implements Compare	{		public int compare(Object obj1, Object obj2)		{			return compareStrings(((JMenuItem)obj1).getText(),				((JMenuItem)obj2).getText(),true);		}	} //}}}	//}}}	//{{{ buildToVersion() method	/**	 * Converts an internal version number (build) into a	 * `human-readable' form.	 * @param build The build	 */	public static String buildToVersion(String build)	{		if(build.length() != 11)			return "<unknown version: " + build + ">";		// First 2 chars are the major version number		int major = Integer.parseInt(build.substring(0,2));		// Second 2 are the minor number		int minor = Integer.parseInt(build.substring(3,5));		// Then the pre-release status		int beta = Integer.parseInt(build.substring(6,8));		// Finally the bug fix release		int bugfix = Integer.parseInt(build.substring(9,11));		return major + "." + minor			+ (beta != 99 ? "pre" + beta :			(bugfix != 0 ? "." + bugfix : "final"));	} //}}}	//{{{ isToolsJarAvailable() method	/**	 * If on JDK 1.2 or higher, make sure that tools.jar is available.	 * This method should be called by plugins requiring the classes	 * in this library.	 * <p>	 * tools.jar is searched for in the following places:	 * <ol>	 *   <li>the classpath that was used when jEdit was started,	 *   <li>jEdit's jars folder in the user's home,	 *   <li>jEdit's system jars folder,	 *   <li><i>java.home</i>/lib/. In this case, tools.jar is added to	 *       jEdit's list of known jars using jEdit.addPluginJAR(),	 *       so that it gets loaded through JARClassLoader.	 * </ol><p>	 *	 * On older JDK's this method does not perform any checks, and returns	 * <code>true</code> (even though there is no tools.jar).	 *	 * @return <code>false</code> if and only if on JDK 1.2 and tools.jar	 *    could not be found. In this case it prints some warnings on Log,	 *    too, about the places where it was searched for.	 * @since jEdit 3.2.2	 */	public static boolean isToolsJarAvailable()	{		Log.log(Log.DEBUG, MiscUtilities.class,"Searching for tools.jar...");		Vector paths = new Vector();		//{{{ 1. Check whether tools.jar is in the system classpath:		paths.addElement("System classpath: "			+ System.getProperty("java.class.path"));		try		{			// Either class sun.tools.javac.Main or			// com.sun.tools.javac.Main must be there:			try			{				Class.forName("sun.tools.javac.Main");			}			catch(ClassNotFoundException e1)			{				Class.forName("com.sun.tools.javac.Main");			}			Log.log(Log.DEBUG, MiscUtilities.class,				"- is in classpath. Fine.");			return true;		}		catch(ClassNotFoundException e)		{			//Log.log(Log.DEBUG, MiscUtilities.class,			//	"- is not in system classpath.");		} //}}}		//{{{ 2. Check whether it is in the jEdit user settings jars folder:		String settingsDir = jEdit.getSettingsDirectory();		if(settingsDir != null)		{			String toolsPath = constructPath(settingsDir, "jars",				"tools.jar");			paths.addElement(toolsPath);			if(new File(toolsPath).exists())			{				Log.log(Log.DEBUG, MiscUtilities.class,					"- is in the user's jars folder. Fine.");				// jEdit will load it automatically				return true;			}		} //}}}		//{{{ 3. Check whether it is in jEdit's system jars folder:		String jEditDir = jEdit.getJEditHome();		if(jEditDir != null)		{			String toolsPath = constructPath(jEditDir, "jars", "tools.jar");			paths.addElement(toolsPath);			if(new File(toolsPath).exists())			{				Log.log(Log.DEBUG, MiscUtilities.class,					"- is in jEdit's system jars folder. Fine.");				// jEdit will load it automatically				return true;			}		} //}}}		//{{{ 4. Check whether it is in <java.home>/lib:		String toolsPath = System.getProperty("java.home");		if(toolsPath.toLowerCase().endsWith(File.separator + "jre"))			toolsPath = toolsPath.substring(0, toolsPath.length() - 4);		toolsPath = constructPath(toolsPath, "lib", "tools.jar");		paths.addElement(toolsPath);		if(!(new File(toolsPath).exists()))		{			Log.log(Log.WARNING, MiscUtilities.class,				"Could not find tools.jar.\n"				+ "I checked the following locations:\n"				+ paths.toString());			return false;		} //}}}		//{{{ Load it, if not yet done:		PluginJAR jar = jEdit.getPluginJAR(toolsPath);		if(jar == null)		{			Log.log(Log.DEBUG, MiscUtilities.class,				"- adding " + toolsPath + " to jEdit plugins.");			jEdit.addPluginJAR(toolsPath);		}		else			Log.log(Log.DEBUG, MiscUtilities.class,				"- has been loaded before.");		//}}}		return true;	} //}}}	//{{{ parsePermissions() method	/**	 * Parse a Unix-style permission string (rwxrwxrwx).	 * @param s The string (must be 9 characters long).	 * @since jEdit 4.1pre8	 */	public static int parsePermissions(String s)	{		int permissions = 0;		if(s.length() == 9)		{			if(s.charAt(0) == 'r')				permissions += 0400;			if(s.charAt(1) == 'w')				permissions += 0200;			if(s.charAt(2) == 'x')				permissions += 0100;			else if(s.charAt(2) == 's')				permissions += 04100;			else if(s.charAt(2) == 'S')				permissions += 04000;			if(s.charAt(3) == 'r')				permissions += 040;			if(s.charAt(4) == 'w')				permissions += 020;			if(s.charAt(5) == 'x')				permissions += 010;			else if(s.charAt(5) == 's')				permissions += 02010;			else if(s.charAt(5) == 'S')				permissions += 02000;			if(s.charAt(6) == 'r')				permissions += 04;			if(s.charAt(7) == 'w')				permissions += 02;			if(s.charAt(8) == 'x')				permissions += 01;			else if(s.charAt(8) == 't')				permissions += 01001;			else if(s.charAt(8) == 'T')				permissions += 01000;		}		return permissions;	} //}}}	//{{{ getEncodings() method	/**	 * Returns a list of supported character encodings.	 * On Java 1.3, returns a fixed list.	 * On Java 1.4, uses reflection to call an NIO API.	 * @since jEdit 4.2pre5	 */	public static String[] getEncodings()	{		List returnValue = new ArrayList();		if(OperatingSystem.hasJava14())		{			try			{				Class clazz = Class.forName(					"java.nio.charset.Charset");				Method method = clazz.getMethod(					"availableCharsets",					new Class[0]);				Map map = (Map)method.invoke(null,					new Object[0]);				Iterator iter = map.keySet().iterator();				returnValue.add(UTF_8_Y);				while(iter.hasNext())				{					returnValue.add(iter.next());				}			}			catch(Exception e)			{				Log.log(Log.ERROR,MiscUtilities.class,e);			}		}		else		{			StringTokenizer st = new StringTokenizer(				jEdit.getProperty("encodings"));			while(st.hasMoreTokens())			{				returnValue.add(st.nextToken());			}		}		return (String[])returnValue.toArray(			new String[returnValue.size()]);	} //}}}	//{{{ isSupportedEncoding() method	/**	 * Returns if the given character encoding is supported.	 * Uses reflection to call a Java 1.4 API on Java 1.4, and always	 * returns true on Java 1.3.	 * @since jEdit 4.2pre7	 */	public static boolean isSupportedEncoding(String encoding)	{		if(OperatingSystem.hasJava14())		{			try			{				Class clazz = Class.forName(					"java.nio.charset.Charset");				Method method = clazz.getMethod(					"isSupported",					new Class[] { String.class });				return ((Boolean)method.invoke(null,					new Object[] { encoding }))					.booleanValue();			}			catch(Exception e)			{				Log.log(Log.ERROR,MiscUtilities.class,e);			}		}		return true;	} //}}}	//{{{ throwableToString() method	/**	 * Returns a string containing the stack trace of the given throwable.	 * @since jEdit 4.2pre6	 */	public static String throwableToString(Throwable t)	{		StringWriter s = new StringWriter();		t.printStackTrace(new PrintWriter(s));		return s.toString();	} //}}}	//{{{ Private members	private MiscUtilities() {}	//{{{ compareChars()	/** should this be public? */	private static boolean compareChars(char ch1, char ch2, boolean ignoreCase)	{		if(ignoreCase)			return Character.toUpperCase(ch1) == Character.toUpperCase(ch2);		else			return ch1 == ch2;	} //}}}	//}}}}

⌨️ 快捷键说明

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