📄 zlib.java
字号:
/** * if path has no extension, add the given one; * ext should include the period. * TODO this does not work properly if the path is abc.0120 */ public static String addExtensionIfMissing(String path, String ext) { String hasext = getExtension(path); if (hasext == null) path = path + ext; return path; } /** * Java link detection: * <p> * For a link that actually points to something (either a file or * a directory), the absolute path is the path through the link, * whereas the canonical path is the path the link references. * <p> * Dangling links appear as files of size zero - * no way to distinguish dangling links from non-existent files * other than by consulting the parent directory. */ public static boolean isLink(File file) { try { if (!file.exists()) return true; else { String cnnpath = file.getCanonicalPath(); String abspath = file.getAbsolutePath(); //System.out.println(abspath+" <-> "+cnnpath); return !abspath.equals(cnnpath); } } catch (java.io.FileNotFoundException ex) { return true; // a dangling link. } catch (IOException ex) { /*ignore other errors*/ } return false; } //isLink /** * launch JFileChooser to select a file */ public static String chooseFile(Component parent) { String wdir = System.getProperty("user.dir"); JFileChooser chooser = new JFileChooser(wdir); int retval = chooser.showOpenDialog(parent); if (retval != JFileChooser.APPROVE_OPTION) return null; File f = chooser.getSelectedFile(); if (f == null || f.isDirectory()) return null; return f.getPath(); } /** * launch JFileChooser to select a directory */ public static String chooseDir(Component parent) { String wdir = System.getProperty("user.dir"); JFileChooser chooser = new JFileChooser(wdir); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int retval = chooser.showOpenDialog(parent); if (retval != JFileChooser.APPROVE_OPTION) return null; File f = chooser.getSelectedFile(); if (f == null) return null; return f.getPath(); } //chooseDir /** pop up a GUI to ask the user to pick one of a couple choices * choces are passed like: Object[] values = { "draw mode", "paint mode" }; */ public static Object getUIInput(Object[] values) { Object selected = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, values, values[0]); return selected; } //getUIInput //---------------------------------------------------------------- /** * load properties from file */ public static java.util.Properties readProps(String path) throws IOException { java.util.Properties props = new java.util.Properties(); InputStream f = new FileInputStream(path); props.load(f); f.close(); return props; } //readProps /** * go over cmdline, for any arg that looks like an -option, * add the next arg as its value unless it too begins with a hypen, * in that case add True as its value. */ public static void addCmdlineProps(java.util.Properties props, String[] cmdline) { for( int i=0; i < cmdline.length; i++ ) { if (cmdline[i].startsWith("-")) { String key = cmdline[i]; Object val = Boolean.TRUE; if (i < (cmdline.length-1) && !cmdline[i+1].startsWith("-")) val = cmdline[i+1]; props.put(key, val); // relying on the fact that props is a hashtable } } } //addCmdlineProps //---------------------------------------------------------------- /** * return a string with any characters c removed */ public static String stripchar(String tok, char c) { if (tok.indexOf(c) < 0) return tok; StringBuffer sb = new StringBuffer(); int len = tok.length(); for( int i = 0; i < len; i++ ) { char c1 = tok.charAt(i); if (c1 != c) sb.append(c1); } return sb.toString(); } //stripchars /** */ public static void sleepfor(int msecs) { //System.out.println("sleeping for " + msecs); System.out.print("."); try { Thread.sleep(msecs); } catch(Exception x) {} } /** * Print --more--, wait for user to hit enter to continue. */ public static void more() { //if (verbose == 0) return; System.out.println("--more--"); try { System.in.read(); } catch(Exception x) {} } /** * substitute becomes for was in str, return new string */ public static String stringsubst(String str, String was, String becomes) { int idx = str.indexOf(was); if (idx < 0) return str; String start = str.substring(0, idx); String end = str.substring(idx+was.length(), str.length()); return start + becomes + end; } /** * Split string at delimiters, like python string.split. * NOTE There is a jdk1.4 String.split routine * TODO: could probably speed this up by coding without the stringtokenizer */ public static ArrayList stringSplit(String s, String delim) { ArrayList l = null; StringTokenizer st = new StringTokenizer(s, delim, false); while( st.hasMoreTokens() ) { String tok = st.nextToken(); if (l == null) l = new ArrayList(5); l.add(tok); } return l; } //stringSplit //---------------------------------------------------------------- /** * full clone of a 2d int array * @deprecated moved to zlib.array */ public static int[][] cloneArray(int[][] arr) { int[][] rarr = (int[][])arr.clone(); int nr = arr.length; for( int r=0; r < nr; r++ ) { rarr[r] = (int[])(arr[r]).clone(); } return rarr; } //cloneArray /** * full clone of a 2d float array * @deprecated moved to zlib.array */ public static float[][] cloneArray(float[][] arr) { float[][] rarr = (float[][])arr.clone(); int nr = arr.length; for( int r=0; r < nr; r++ ) { rarr[r] = (float[])(arr[r]).clone(); } return rarr; } //cloneArray /** * full clone of a 2d double array * @deprecated moved to zlib.array */ public static double[][] cloneArray(double[][] arr) { double[][] rarr = (double[][])arr.clone(); int nr = arr.length; for( int r=0; r < nr; r++ ) { rarr[r] = (double[])(arr[r]).clone(); } return rarr; } //cloneArray //---------------------------------------------------------------- /** * copy float[][] to double[][] */ public static double[] toDouble(float[] arr) { int len = arr.length; double[] d = new double[len]; for( int i=0; i < len; i++ ) { d[i] = arr[i]; } return d; } //toDouble /** * copy float[][] to double[][] * @deprecated moved to zlib.array */ public static double[][] toDouble(float[][] arr) { int nr = arr.length; int nc = arr[0].length; double[][] darr = new double[nr][nc]; for( int r=0; r < nr; r++ ) { for( int c=0; c < nc; c++ ) { darr[r][c] = arr[r][c]; } } return darr; } //toDouble /** * copy double[][] to float[][] * @deprecated moved to zlib.array */ public static float[] toFloat(double[] arr) { int len = arr.length; float[] d = new float[len]; for( int i=0; i < len; i++ ) { d[i] = (float)arr[i]; } return d; } //toFloat /** * copy double[][] to float[][] * @deprecated moved to zlib.array */ public static float[][] toFloat(double[][] arr) { int nr = arr.length; int nc = arr[0].length; float[][] farr = new float[nr][nc]; for( int r=0; r < nr; r++ ) { // TODO: row access to speed up for( int c=0; c < nc; c++ ) { farr[r][c] = (float)arr[r][c]; } } return farr; } //toFloat //---------------------------------------------------------------- /** * this is in java.util.Arrays * @deprecated */ public static boolean equals(double[] arr1, double[] arr2) { int len = arr1.length; if (len != arr2.length) return false; for( int i=0; i < len; i++ ) { if (arr1[i] != arr2[i]) return false; } return true; } //equals(double[],double[]) /** * this is in java.util.Arrays * @deprecated */ public static boolean equals(float[] arr1, float[] arr2) { int len = arr1.length; if (len != arr2.length) return false; for( int i=0; i < len; i++ ) { if (arr1[i] != arr2[i]) return false; } return true; } //equals(float[],float[]) //---------------------------------------------------------------- /** * @deprecated moved to zlib.array */ public static int max(int[] arr) { int len = arr.length; int max = Integer.MIN_VALUE; for( int i=0; i < len; i++ ) { if (arr[i] > max) max = arr[i]; } return max; } //max /** * @deprecated moved to zlib.array */ public static int min(int[] arr) { int len = arr.length; int min = Integer.MAX_VALUE; for( int i=0; i < len; i++ ) { if (arr[i] < min) min = arr[i]; } return min; } //min /** * @deprecated moved to zlib.array */ public static float max(float[] arr) { int len = arr.length; float max = Float.NEGATIVE_INFINITY; for( int i=0; i < len; i++ ) { if (arr[i] > max) max = arr[i]; } return max; } //max /** * @deprecated moved to zlib.array */ public static float min(float[] arr) { int len = arr.length; float min = Float.POSITIVE_INFINITY; for( int i=0; i < len; i++ ) { if (arr[i] < min) min = arr[i]; } return min; } //min //---------------------------------------------------------------- public static int atoi(String s) { return Integer.parseInt(s); } public static double atof(String s) { return Double.valueOf(s).doubleValue(); } //---------------------------------------------------------------- /** test the InputStreamReadLine */ public static void main(String[] args) { try { InputStream f = new FileInputStream("zlib/zlib.java"); String s = InputStreamReadLine(f); System.out.println("line :"+s+":"); f.close(); } catch(Exception ex) { System.err.println(ex); } }} //zlib
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -