📄 util.java
字号:
* @return DOCUMENT ME! */ public static String scramble(String message, String password) { long[] hashPass; long[] hashMessage; byte[] to = new byte[8]; String val = ""; //$NON-NLS-1$ message = message.substring(0, 8); if ((password != null) && (password.length() > 0)) { hashPass = newHash(password); hashMessage = newHash(message); RandStructcture randStruct = randomInit(hashPass[0] ^ hashMessage[0], hashPass[1] ^ hashMessage[1]); int msgPos = 0; int msgLength = message.length(); int toPos = 0; while (msgPos++ < msgLength) { to[toPos++] = (byte) (Math.floor(rnd(randStruct) * 31) + 64); } /* Make it harder to break */ byte extra = (byte) (Math.floor(rnd(randStruct) * 31)); for (int i = 0; i < to.length; i++) { to[i] ^= extra; } val = new String(to); } return val; } // ~ Inner Classes // ---------------------------------------------------------- /** * Converts a nested exception into a nicer message * * @param ex * the exception to expand into a message. * * @return a message containing the exception, the message (if any), and a * stacktrace. */ public static String stackTraceToString(Throwable ex) { StringBuffer traceBuf = new StringBuffer(); traceBuf.append(Messages.getString("Util.1")); //$NON-NLS-1$ if (ex != null) { traceBuf.append(ex.getClass().getName()); String message = ex.getMessage(); if (message != null) { traceBuf.append(Messages.getString("Util.2")); //$NON-NLS-1$ traceBuf.append(message); } StringWriter out = new StringWriter(); PrintWriter printOut = new PrintWriter(out); ex.printStackTrace(printOut); traceBuf.append(Messages.getString("Util.3")); //$NON-NLS-1$ traceBuf.append(out.toString()); } traceBuf.append(Messages.getString("Util.4")); //$NON-NLS-1$ return traceBuf.toString(); } public static Object getInstance(String className, Class[] argTypes, Object[] args) throws SQLException { try { return handleNewInstance(Class.forName(className).getConstructor( argTypes), args); } catch (SecurityException e) { throw SQLError.createSQLException( "Can't instantiate required class", SQLError.SQL_STATE_GENERAL_ERROR, e); } catch (NoSuchMethodException e) { throw SQLError.createSQLException( "Can't instantiate required class", SQLError.SQL_STATE_GENERAL_ERROR, e); } catch (ClassNotFoundException e) { throw SQLError.createSQLException( "Can't instantiate required class", SQLError.SQL_STATE_GENERAL_ERROR, e); } } /** * Handles constructing new instance with the given constructor and wrapping * (or not, as required) the exceptions that could possibly be generated */ public static final Object handleNewInstance(Constructor ctor, Object[] args) throws SQLException { try { return ctor.newInstance(args); } catch (IllegalArgumentException e) { throw SQLError.createSQLException( "Can't instantiate required class", SQLError.SQL_STATE_GENERAL_ERROR, e); } catch (InstantiationException e) { throw SQLError.createSQLException( "Can't instantiate required class", SQLError.SQL_STATE_GENERAL_ERROR, e); } catch (IllegalAccessException e) { throw SQLError.createSQLException( "Can't instantiate required class", SQLError.SQL_STATE_GENERAL_ERROR, e); } catch (InvocationTargetException e) { Throwable target = e.getTargetException(); if (target instanceof SQLException) { throw (SQLException) target; } if (target instanceof ExceptionInInitializerError) { target = ((ExceptionInInitializerError) target).getException(); } throw SQLError.createSQLException(target.toString(), SQLError.SQL_STATE_GENERAL_ERROR); } } /** * Does a network interface exist locally with the given hostname? * * @param hostname * the hostname (or IP address in string form) to check * @return true if it exists, false if no, or unable to determine due to VM * version support of java.net.NetworkInterface */ public static boolean interfaceExists(String hostname) { try { Class networkInterfaceClass = Class .forName("java.net.NetworkInterface"); return networkInterfaceClass.getMethod("getByName", null).invoke( networkInterfaceClass, new Object[] { hostname }) != null; } catch (Throwable t) { return false; } } /** * Reflexive access on JDK-1.5's Class.cast() method so we don't have to * move that out into separate classes built for JDBC-4.0. * * @param invokeOn * @param toCast * @return */ public static Object cast(Object invokeOn, Object toCast) { if (CAST_METHOD != null) { try { return CAST_METHOD.invoke(invokeOn, new Object[] { toCast }); } catch (Throwable t) { return null; } } return null; } public static long getCurrentTimeNanosOrMillis() { if (systemNanoTimeMethod != null) { try { return ((Long) systemNanoTimeMethod.invoke(null, null)) .longValue(); } catch (IllegalArgumentException e) { // ignore - fall through to currentTimeMillis() } catch (IllegalAccessException e) { // ignore - fall through to currentTimeMillis() } catch (InvocationTargetException e) { // ignore - fall through to currentTimeMillis() } } return System.currentTimeMillis(); } public static void resultSetToMap(Map mappedValues, java.sql.ResultSet rs) throws SQLException { while (rs.next()) { mappedValues.put(rs.getObject(1), rs.getObject(2)); } } public static Map calculateDifferences(Map map1, Map map2) { Map diffMap = new HashMap(); Iterator map1Entries = map1.entrySet().iterator(); while (map1Entries.hasNext()) { Map.Entry entry = (Map.Entry) map1Entries.next(); Object key = entry.getKey(); Number value1 = null; Number value2 = null; if (entry.getValue() instanceof Number) { value1 = (Number) entry.getValue(); value2 = (Number) map2.get(key); } else { try { value1 = new Double(entry.getValue().toString()); value2 = new Double(map2.get(key).toString()); } catch (NumberFormatException nfe) { continue; } } if (value1.equals(value2)) { continue; } if (value1 instanceof Byte) { diffMap.put(key, new Byte( (byte) (((Byte) value2).byteValue() - ((Byte) value1) .byteValue()))); } else if (value1 instanceof Short) { diffMap.put(key, new Short((short) (((Short) value2) .shortValue() - ((Short) value1).shortValue()))); } else if (value1 instanceof Integer) { diffMap.put(key, new Integer( (((Integer) value2).intValue() - ((Integer) value1) .intValue()))); } else if (value1 instanceof Long) { diffMap.put(key, new Long( (((Long) value2).longValue() - ((Long) value1) .longValue()))); } else if (value1 instanceof Float) { diffMap.put(key, new Float(((Float) value2).floatValue() - ((Float) value1).floatValue())); } else if (value1 instanceof Double) { diffMap.put(key, new Double( (((Double) value2).shortValue() - ((Double) value1) .shortValue()))); } else if (value1 instanceof BigDecimal) { diffMap.put(key, ((BigDecimal) value2) .subtract((BigDecimal) value1)); } else if (value1 instanceof BigInteger) { diffMap.put(key, ((BigInteger) value2) .subtract((BigInteger) value1)); } } return diffMap; } public static List loadExtensions(Connection conn, Properties props, String extensionClassNames, String errorMessageKey) throws SQLException { List extensionList = new LinkedList(); List interceptorsToCreate = StringUtils.split(extensionClassNames, ",", true); Iterator iter = interceptorsToCreate.iterator(); String className = null; try { while (iter.hasNext()) { className = iter.next().toString(); Extension extensionInstance = (Extension) Class.forName( className).newInstance(); extensionInstance.init(conn, props); extensionList.add(extensionInstance); } } catch (Throwable t) { SQLException sqlEx = SQLError.createSQLException(Messages .getString(errorMessageKey, new Object[] { className })); sqlEx.initCause(t); throw sqlEx; } return extensionList; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -