📄 utils.java
字号:
/*
* ====================================================================
* Copyright (c) 1995-1999 Purple Technology, Inc. All rights
* reserved.
*
* PLAIN LANGUAGE LICENSE: Do whatever you like with this code, free
* of charge, just give credit where credit is due. If you improve it,
* please send your improvements to alex@purpletech.com. Check
* http://www.purpletech.com/code/ for the latest version and news.
*
* LEGAL LANGUAGE LICENSE: Redistribution and use in source and binary
* forms, with or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. The names of the authors and the names "Purple Technology,"
* "Purple Server" and "Purple Chat" must not be used to endorse or
* promote products derived from this software without prior written
* permission. For written permission, please contact
* server@purpletech.com.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND PURPLE TECHNOLOGY ``AS
* IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHORS OR PURPLE TECHNOLOGY BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
**/
package com.sslexplorer.util;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Utils {
/**
* fills the left side of a number with zeros <br>
* e.g. zerofill(14, 3) -> "014" <br>
* e.g. zerofill(187, 6) -> "000014" <br>
* e.g. zerofill(-33, 4) -> "-033" <br>
**/
public static String zerofill(int x, int desiredWidth) {
StringBuffer buf = new StringBuffer();
if (x < 0) {
buf.append("-");
desiredWidth--;
x = -x;
}
while (desiredWidth>7) {
buf.append("0");
desiredWidth--;
}
switch (desiredWidth) {
case 7:
if (x<1000000) buf.append("0");
case 6:
if (x<100000) buf.append("0");
case 5:
if (x<10000) buf.append("0");
case 4:
if (x<1000) buf.append("0");
case 3:
if (x<100) buf.append("0");
case 2:
if (x<10) buf.append( "0" );
}
buf.append(x);
return buf.toString();
}
public static void printIndent(PrintWriter out, int indent) {
out.print(indent(indent));
}
public static String indent(int indent) {
switch (indent) {
case 8:
return(" ");
case 7:
return(" ");
case 6:
return(" ");
case 5:
return(" ");
case 4:
return(" ");
case 3:
return(" ");
case 2:
return(" ");
case 1:
return(" ");
default:
StringBuffer buf = new StringBuffer();
for (int i=0; i<indent; ++i) { buf.append(" "); }
return buf.toString();
}
}
/**
* @deprecated use org.apache.commons.lang.StringUtils.join()
**/
public static String commaList(Object[] a) {
return commaList(Arrays.asList(a).iterator());
}
/**
* @deprecated use org.apache.commons.lang.StringUtils.join()
**/
public static String commaList(Collection c) {
return commaList(c.iterator());
}
/**
* @deprecated use org.apache.commons.lang.StringUtils.join()
**/
public static String commaList(Iterator i) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
printCommaList(pw, i);
pw.close();
return sw.toString();
}
/**
* Given an iterator, prints it as a comma-delimited list
* (actually a comma-and-space delimited list). E.g. If the
* iterator contains the strings { "my", "dog", "has fleas" } it
* will print "my, dog, has fleas".
*
* @param out the stream to write to
* @param i an iterator containing printable (toString) objects, e.g. strings
**/
public static void printCommaList(PrintWriter out, Iterator i) {
boolean first = true;
while (i.hasNext()) {
if (first) first = false;
else out.print(", ");
out.print(i.next());
}
}
/**
* @return true if all characters in the string are whitespace characters, or the string is empty
**/
public static boolean isWhitespace(String s) {
for (int i=0; i<s.length(); ++i) {
if (!Character.isWhitespace(s.charAt(i))) return false;
}
return true;
}
/**
* Class encapsulating information from an exec call -- slightly
* easier than the standard API
**/
public static class ExecInfo {
public int exit;
public String stdout;
public String stderr;
public String toString() {
return "ExecInfo[exit=" + exit + "," +
"stdout=" + javaEscape(stdout) + "," +
"stderr=" + javaEscape(stderr) + "]";
}
}
/**
* Wrapper for Runtime.exec. Takes input as a String. Times out
* after sleep msec. Returns an object containing exit value,
* standard output, and error output.
* @param command the command-line to execute
* @param input a string to pass to the process as standard input
* @param sleep msec to wait before terminating process (if <= 0, waits forever)
**/
/*public static ExecInfo exec(String command, String input, long sleep) throws IOException {
Process process = null;
ExecInfo info = new ExecInfo();
try {
Alarm a = null;
if (sleep>0) {
a = new Alarm(Thread.currentThread(), sleep);
a.start();
}
process = Runtime.getRuntime().exec(command);
if (input != null) {
PrintWriter pw = new PrintWriter(process.getOutputStream());
pw.print(input);
pw.close();
}
info.stdout = IOUtils.readStream(process.getInputStream());
info.stderr = IOUtils.readStream(process.getErrorStream());
process.waitFor();
if (a!=null) a.stop = true;
}
catch (InterruptedIOException iioe) {
throw new IOException("Process '" + command + "' took more than " + sleep/1000 + " sec");
}
catch (InterruptedException ie) {
throw new IOException("Process '" + command + "' took more than " + sleep/1000 + " sec");
}
finally {
if (process != null)
process.destroy();
}
info.exit = process.exitValue();
return info;
}*/
/**
* Turn "Now is the time for all good men" into "Now is the time for..."
* <p>
* Specifically:
* <p>
* If str is less than max characters long, return it.
* Else abbreviate it to (substring(str, 0, max-3) + "...").
* If max is less than 3, throw an IllegalArgumentException.
* In no case will it return a string of length greater than max.
*
* @param max maximum length of result string
**/
public static String abbreviate(String s, int max) {
if (max < 4)
throw new IllegalArgumentException("Minimum abbreviation is 3 chars");
if (s.length() <= max) return s;
// todo: break into words
return s.substring(0, max-3) + "...";
}
/**
* pad or truncate
**/
public static String pad(String s, int length) {
if (s.length() < length) return s + indent(length - s.length());
else return s.substring(0,length);
}
/**
* Compare two strings, and return the portion where they differ.
* (More precisely, return the remainder of the second string,
* starting from where it's different from the first.)
* <p>
* E.g. strdiff("i am a machine", "i am a robot") -> "robot"
*
**/
public static String strdiff(String s1, String s2) {
int at = strdiffat(s1, s2);
if (at == -1)
return "";
return s2.substring(at);
}
/**
* Compare two strings, and return the index at which the strings begin to diverge<p>
* E.g. strdiff("i am a machine", "i am a robot") -> 7<p>
* @return -1 if they are the same
*
**/
public static int strdiffat(String s1, String s2)
{
int i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -