📄 utils.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . 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. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS 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 COPYRIGHT OWNER OR CONTRIBUTORS 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. * * $Id: Utils.java,v 1.79 2005/07/13 20:53:38 jmettraux Exp $ *///// Utils.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.0.04 20.11.2001 John Mettraux (jmettraux@openwfe.org)//package openwfe.org;import openwfe.org.misc.ByteUtils;/** * Static methods for misc usages * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Date: 2005/07/13 20:53:38 $ * <br>$Id: Utils.java,v 1.79 2005/07/13 20:53:38 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public abstract class Utils{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(Utils.class.getName()); /** * Turns a serializable object into a byte array */ public static byte[] serialize (java.io.Serializable object) throws java.io.IOException { java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos); oos.writeObject(object); oos.flush(); return baos.toByteArray(); } /** * A method for moving files */ public static void move (java.io.File file, String destinationPath) { try { java.io.FileInputStream in = new java.io.FileInputStream(file); java.io.FileOutputStream out = new java.io.FileOutputStream(destinationPath); byte[] buffer = new byte[1024]; while (true) { int count = in.read(buffer); if (count < 1) break; out.write(buffer, 0, count); if (count < 1024) break; } file.delete(); } catch (java.io.IOException ie) { log.info("Failed to reject "+file.getPath(), ie); } } /** * simply calls the other move method */ public static void move (String fileName, String destinationFileName) { move(new java.io.File(fileName), destinationFileName); } public static String getMd5Digest (byte[] data) { java.security.MessageDigest digest = null; try { digest = java.security.MessageDigest.getInstance("MD5"); } catch (java.security.NoSuchAlgorithmException nsae) { log.warn("cannot compute MD5 digest", nsae); return null; } digest.update(data); return ByteUtils.toString(digest.digest()); } /** * On a unix-like platform, will return "file:", * on a windows one, will return "file:/". */ public static String getFileProtocolPrefix () { if (java.io.File.separatorChar == '\\') return "file:/"; return "file:"; } /** * Makes sure the file protocol prefix is removed. */ public static String removeFileProtocolPrefix (final String url) { String result = url.substring(5); if (getFileProtocolPrefix().length() == 6) // // win { if (url.startsWith(getFileProtocolPrefix())) result = url.substring(6); } log.debug("removeFileProtocolPrefix() returning '"+result+"'"); return result; } /** * Concats the app directory and the path and * return the resulting canonical (unique) path. */ public static String getCanonicalPath (final String applicationDirectory, final String path) { java.io.File f = new java.io.File(path); if ( ! f.isAbsolute()) f = new java.io.File(applicationDirectory + path); try { final String result = f.getCanonicalPath(); log.debug("getCanonicalPath() returning "+result); return result; } catch (final java.io.IOException ie) { log.warn("getCanonicalPath() exception", ie); } return f.getAbsolutePath(); } /** * Returns true if the given string is an URL. */ public static boolean isUrl (final String s) { try { new java.net.URL(s); } catch (final Throwable t) { return false; } return true; } /** * Turns a relative file url into an absolute one (and makes sure that * the "file:" prefix is set. * If the url is for another protocol, the output will be the same as the * input. */ public static String expandUrl (final String url) { return expandUrl(null, url); } /** * Makes sure the result is an URL, and if its a path URL, will make * sure that it's an absolute and canonical path. */ public static String expandUrl (final String applicationDirectory, final String url) { log.debug("expandUrl() considering >"+url+"<"); // openwfe exceptions... if (url.startsWith("field:")) return url; if (url.startsWith("resource:")) return url; if (url.startsWith("file:")) { final String fn = expandFileName (applicationDirectory, removeFileProtocolPrefix(url)); final String result = getFileProtocolPrefix() + fn; log.debug("expandUrl() 0 returning >"+result+"<"); return result; } if ( ! isUrl(url)) return expandUrl(applicationDirectory, getFileProtocolPrefix()+url); log.debug("expandUrl() 1 returning >"+url+"<"); return url; } /** * Canonicalizes a filename. */ public static String expandFileName (final String applicationDirectory, final String filename) { log.debug("expandFileName() considering >"+filename+"<"); log.debug("expandFileName() adir is '"+applicationDirectory+"'"); String result = null; String adir = ""; if (applicationDirectory != null) adir = applicationDirectory.trim() + java.io.File.separator; log.debug("expandFileName() adir is '"+adir+"'"); if (java.io.File.separatorChar == '\\') // // windows { //if (adir.equals(".\\") || adir.equals(".\\\\")) adir = ""; if (filename.charAt(1) == ':') // // absolute { result = filename; } else // // relative { result = adir + filename; } } else // // *nix { //if (adir.equals("./") || adir.equals(".//")) adir = ""; if (filename.charAt(0) == java.io.File.separatorChar) // // absolute { result = filename; } else // // relative { result = adir + filename; } } final java.io.File f = new java.io.File(result); try { result = f.getCanonicalPath(); } catch (final java.io.IOException ie) { log.warn("expandFileName() failed to get canonical path", ie); } log.debug("expandFileName() returning >"+result+"<"); return result; } /* public static String expandUrl (final String applicationDirectory, String url) { log.debug("expandUrl() url is >"+url+"<"); url = ensureProtocol(url); log.debug("expandUrl() url is >"+url+"<"); if ( ! url.startsWith("file:")) return url; if (url.startsWith("file:/")) url = url.substring(6); else url = url.substring(5); log.debug("expandUrl() url is >"+url+"<"); if ((applicationDirectory != null) && ( ! url.startsWith(applicationDirectory)) && (url.charAt(1) != ':')) { url = applicationDirectory + url; } log.debug("expandUrl() url is >"+url+"<"); java.io.File f = new java.io.File(url); / * String path = f.getAbsolutePath(); //log.debug("expandUrl() path was "+path); path = path.replaceAll("/\\./", "/"); path = path.replaceAll("\\\\\\.\\\\", "\\\\"); //log.debug("expandUrl() path is "+path); final String result = "file:" + path; * / String result = f.getAbsolutePath(); try { result = f.getCanonicalPath(); } catch (final java.io.IOException ie) { log.warn("expandUrl() getCanonicalPath() error", ie); } result = getFileProtocolPrefix() + result; log.debug("expandUrl() result is >"+result+"<"); return result; } */ /** * Takes as input a String containing an url and makes sure * to output the same url with a protocol if missing, the protocol
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -