📄 cbutility.java
字号:
* @deprecated use CBParse method instead
*/
static public byte hex2Byte(char hex1, char hex2)
{
byte a = CBParse.hexChar2Byte(hex1);
byte b = CBParse.hexChar2Byte(hex2);
return (byte) ((a << 4) + b);
}
/**
* Convert a single character to a byte...
*
* @deprecated use CBParse method instead
*/
static public byte hexChar2Byte(char hex)
{
if (hex <= '9')
return ((byte) (hex - 48)); // ('0' -> '9')
else if (hex <= 'F')
return ((byte) (hex - 55)); // ('A' -> 'F')
else
return ((byte) (hex - 87)); // ('a' -> 'f')
}
/**
* From Van Bui - prints out a hex string formatted with
* spaces between each hex word of length wordlength.
*
* @param in input array of bytes to convert
* @param wordlength the length of hex words to print otu.
* @deprecated use CBParse method instead
*/
public static String bytes2HexSplit(byte[] in, int wordlength)
{
String hex = CBParse.bytes2Hex(in);
StringBuffer buff = new StringBuffer();
for (int i = 0; i < hex.length(); i++)
{
buff.append(hex.charAt(i));
if ((i + 1) % wordlength == 0)
buff.append(" ");
}
return buff.toString();
}
/**
* From Van Bui - prints out a hex string formatted with
* spaces between each hex word of length wordlength, and
* new lines every linelength.
*
* @param in input array of bytes to convert
* @param wordlength the length of hex words to print otu.
* @param linelength the length of a line to print before inserting
* a line feed.
* @deprecated use CBParse method instead
*/
public static String bytes2HexSplit(byte[] in, int wordlength, int linelength)
{
String hex = CBParse.bytes2Hex(in);
StringBuffer buff = new StringBuffer();
for (int i = 0; i < hex.length(); i++)
{
buff.append(hex.charAt(i));
if ((i + 1) % wordlength == 0)
buff.append(" ");
if ((i + 1) % linelength == 0)
buff.append("\n");
}
return buff.toString();
}
/**
* Determines whether a given byte sequence is a valid utf-8
* encoding. While this does not mean that the byte *is* a
* utf-8 encoded string, the chance of a random byte sequence
* happening to be utf8 is roughly (1/2 ** (byte array length)).<p>
* Note that '7 bit ascii' is *always* a valid utf-8 string...<p>
* see rfc 2279
*
* @deprecated use CBParse method instead
*/
public static boolean isUTF8(byte[] sequence)
{
boolean debug = false;
if (debug) log.warning("\n\n Starting UTF8 Check\n\n");
int numberBytesInChar;
for (int i = 0; i < sequence.length; i++)
{
byte b = sequence[i];
if (debug) System.out.println("testing byte: " + CBParse.byte2Hex(b));
if (((b >> 6) & 0x03) == 2)
{
if (debug) System.out.println("start byte is invalid utf8 - has 10... start");
return false;
}
byte test = b;
numberBytesInChar = 0;
while ((test & 0x80) > 0)
{
test <<= 1;
numberBytesInChar++;
}
if (numberBytesInChar > 1) // check that extended bytes are also good...
{
for (int j = 1; j < numberBytesInChar; j++)
{
if (i + j >= sequence.length)
{
if (debug) System.out.println("following byte length is invalid - overruns end... ");
return false; // not a character encoding - probably random bytes
}
if (debug) System.out.println("testing byte: " + CBParse.byte2Hex(sequence[i + j]));
if (((sequence[i + j] >> 6) & 0x03) != 2)
{
if (debug) System.out.println("following byte is invalid utf8 - does *not* have 10... start");
return false;
}
}
i += numberBytesInChar - 1; // increment i to the next utf8 character start position.
}
}
return true;
}
/**
* Determines whether a given byte sequence is a valid utf-8
* encoding, encoding (at least in part) something *other* than
* normal Ascii (i.e.
* it is utf-8 encoding something that is not just 7-bit ascii,
* which in utf-8 is indistinguishable from the original text).<p>
* <p/>
* While this does not mean that the bytes *are* a
* utf-8 encoded string, the chance of a random byte sequence
* (containing bytes with the high-bit set)
* happening to be utf8 is roughly (1/2 ** (byte array length)).<p>
* see rfc 2279
*
* @deprecated use CBParse method instead
*/
public static boolean isNonAsciiUTF8(byte[] sequence)
{
log.finest("testing sequence for utf8: " + CBParse.bytes2Hex(sequence));
boolean nonAsciiDetected = false;
int numberBytesInChar;
for (int i = 0; i < sequence.length - 3; i++)
{
byte b = sequence[i];
if (((b >> 6) & 0x03) == 2) return false;
byte test = b;
numberBytesInChar = 0;
while ((test & 0x80) > 0)
{
test <<= 1;
numberBytesInChar++;
}
// check if multi-byte utf8 sequence found
if (numberBytesInChar > 1) // check that extended bytes are also good...
{
nonAsciiDetected = true;
for (int j = 1; j < numberBytesInChar; j++)
{
if (((sequence[i + j] >> 6) & 0x03) != 2)
return false;
}
i += numberBytesInChar - 1; // increment i to the next utf8 character start position.
}
}
return nonAsciiDetected;
}
/**
* This uses the implicit 'unicode marker' at the start of a
* Unicode file to determine whether a file is a unicode file.
* At the beginning of every unicode file is a two byte code
* indicating the endien-ness of the file (either FFFE or FEFF).
* If either of these sequences is found, this function returns
* true, otherwise it returns false. <i>Technically</i> this isn't
* a sure test, since a) something else could have this signiture,
* and b) unicode files are not absolutely required to have this
* signiture (but most do).
*
* @deprecated use CBParse method instead
*/
public static boolean isUnicode(byte[] sequence)
{
if (sequence.length >= 2)
{
if (sequence[0] == (byte) 0xFF && sequence[1] == (byte) 0xFE) return true;
if (sequence[0] == (byte) 0xFE && sequence[1] == (byte) 0xFF) return true;
}
return false;
}
/*
* Some refugees from com.ca.pki.util.StaticUtil
*/
/**
* Show file chooser to get a file location for saving data.
*/
public static String chooseFileToSave(Component parent, String title, String[] filter, String fileType)
{
JFileChooser chooser = new JFileChooser(System.getProperty("PKIHOME"));
chooser.setToolTipText(title);
chooser.setDialogTitle(title);
if (filter != null && fileType != null)
{
CBFileFilter filt = new CBFileFilter(filter, fileType);
chooser.setFileFilter(filt);
}
int returnVal = chooser.showSaveDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
if (chooser.getSelectedFile() != null)
return chooser.getSelectedFile().toString();
}
return null;
}
public static boolean okToWriteFile(Frame parent, String fileName)
{
File f = new File(fileName);
if (f.isDirectory())
{
JOptionPane.showMessageDialog(parent, fileName + " is a directory.", "Error!", JOptionPane.ERROR_MESSAGE);
return false;
}
else if (f.exists())
{
int saveAnswer = JOptionPane.showConfirmDialog(parent,
"File " + fileName + " already exists.\nDo you want to overwrite?",
"Question", JOptionPane.OK_CANCEL_OPTION);
return (saveAnswer == JOptionPane.OK_OPTION);
}
return true;
}
/**
* This Comparator compares two strings, ignoring case.
*
* @author Trudi.
*/
public static class IgnoreCaseStringComparator implements Comparator
{
/**
* This Comparator compares two strings, ignoring case.
*
* @param o1 one of the two items to be compared.
* @param o2 the other of the items to be compared.
* @return the result of the compare (0 if o1 & o2 are equal, -1 if o1 < o2, 1 if o1 > o2).
* NOTE: if o1 is null and o2 is not null, 1 is returned. If o2 is null and o1 is not null, -1 is returned.
* If both o1 and o2 are null, 0 is returned. If an error occurs trying to cast either o1 or o2, 0 is returned.
*/
public int compare(Object o1, Object o2)
{
if (o1 == null && o2 != null)
return 1;
else if (o2 == null && o1 != null)
return -1;
else if (o1 == null && o2 == null)
return 0;
try
{
return (o1.toString().toLowerCase()).compareTo(o2.toString().toLowerCase());
}
catch (ClassCastException e)
{
System.out.println("Error sorting values - invalid string in sort." + e);
return 0;
}
}
}
/**
* Searches for a config file. First checks if a property directory has been explicitly set
* with the System property 'jxplorer.config' (which can take either a path, or the
* value 'user.home' to set it to use the user home directory). Then checks the user
* home directory to see if the specific config file already exists there. Then reverts to the default
* location in the user.dir directory the program is run from.
*
* @param configFileName the name of the actual file - e.g. "bookmarks.txt".
* @return
*/
public static String getPropertyConfigPath(String configFileName)
{
String pathToConfigFile;
// the potential path to a config file in the user home directory
String userHomeConfigDir = System.getProperty("user.home") + File.separator + "jxplorer";
String userHomeConfigFilePath = userHomeConfigDir + File.separator + configFileName;
// the default config location is to the directory JXplorer is run from.
String defaultConfigFilePath = System.getProperty("user.dir") + File.separator + configFileName;
// see if a config file has been explicitly set
if (System.getProperty("jxplorer.config") != null)
{
pathToConfigFile = System.getProperty("jxplorer.config");
// check if we are being forced to use the user.home directory (we will create the file if it
// doesn't exist)
if (pathToConfigFile.equalsIgnoreCase("home") || pathToConfigFile.equalsIgnoreCase("user.home"))
{
// quick sanity check that there is a 'jxplorer' config directory under user home for us to save to
File configDir = new File(userHomeConfigDir);
if (configDir.exists() == false)
{
configDir.mkdir(); // - if not, we'll create it.
}
return userHomeConfigFilePath;
}
// otherwise return the parameter - again we will create it if it doesn't exist
if (pathToConfigFile.endsWith(File.separator))
return pathToConfigFile + configFileName;
else
return pathToConfigFile + File.separator + configFileName;
}
// try the user home directory next... if the user home directory config file exists read the config from there
if (new File(userHomeConfigFilePath).exists())
{
return userHomeConfigFilePath;
}
// if there is no config file in the user.home directory, and we haven't been told otherwise, default to
// the directory JXplorer is run from.
return defaultConfigFilePath;
}
/**
* This gets the working log level of a particular logger. You would think Sun would already
* have a method to do this. You would be wrong.
*
* @param log
* @return the active working log level - e.g. whether a particular log call will be called because this,
* or a parent, logger is set to a particular log level.
*/
public static Level getTrueLogLevel(Logger log)
{
if (log.getLevel() != null)
{
return log.getLevel();
}
if (log.getParent() != null)
return getTrueLogLevel(log.getParent());
// should never get here...
log.severe("no active log level initialised");
System.err.println("no active log level initialised");
return Level.ALL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -