/* Copyright (c) 2004 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.nio.channels.FileChannel; import java.security.MessageDigest; import java.util.Calendar; //Basic util functions public class Util { public static void Touch(String FileName) throws java.io.IOException { FileWriter FW=new FileWriter(FileName,true); FW.flush(); FW.close(); } public static boolean BufferedReaderOk(BufferedReader IN) throws Exception { IN.mark(100); int I=IN.read(); IN.reset(); if (I >= 0) return true; return false; } public static boolean FileExists(String FileName) { File F=new File(FileName); return F.exists(); } public static String SHA1(byte[] P) { try { MessageDigest SIG=MessageDigest.getInstance("SHA"); SIG.update(P, 0, P.length); byte D[]=SIG.digest(); StringBuffer S=new StringBuffer(40); BigInteger BI=new BigInteger(1,D); S.append(BI.toString(16)); while (S.length() < 40) { S.insert(0,'0'); } return S.toString(); } catch (java.security.NoSuchAlgorithmException e) { System.err.println("Unable to find SHA"); System.out.println(e.toString()); e.printStackTrace(); } return null; } public static String SHA1(String Plain) { try { byte P[]=Plain.getBytes("UTF-8"); return SHA1(P); } catch (java.io.UnsupportedEncodingException uee) { uee.printStackTrace(); } return null; } public static int getUnixTime() { Calendar C=Calendar.getInstance(); long T=C.getTimeInMillis(); int I=new Long(T/1000).intValue(); return I; } // Acts like unix cut -d $D -f $f public static String Cut(String S, char D, int F) { int Idx=0; int f=0; //read through to find F-1 instances of D //F-1 because if you want the 1st field, you read //through 0 D's. If you want the 2nd, you read 1, etc. while ((Idx < S.length()) && (f < F-1)) { if (S.charAt(Idx) == D) f++; Idx++; } //Read until end of string or anouther D StringBuffer Out=new StringBuffer(100); while ((Idx < S.length()) && (S.charAt(Idx) != D)) { Out.append(S.charAt(Idx)); Idx++; } return new String(Out); } /** Uses 1mb blocks */ public static boolean FileCopy(String Src, String Dst) throws java.io.IOException, java.io.FileNotFoundException { FileInputStream InStream=new FileInputStream(Src); FileChannel IN=InStream.getChannel(); FileOutputStream OutStream=new FileOutputStream(Dst,false); FileChannel OUT=OutStream.getChannel(); long SrcSize=new File(Src).length(); long Copied=0; while(Copied < SrcSize) { long CopySize=1048576; long Left=SrcSize - Copied; if (Left < CopySize) CopySize=Left; long Xfer=OUT.transferFrom(IN,Copied,CopySize); Copied+=Xfer; } IN.close(); InStream.close(); OUT.close(); OutStream.close(); if (Copied==SrcSize) return true; return false; } public static String MD5File(String Src) throws java.io.IOException { return MD5File(new File(Src)); } public static String MD5File(File Src) throws java.io.IOException { try { MessageDigest SIG=MessageDigest.getInstance("MD5"); File F=Src; long sz=F.length(); long read=0; FileInputStream FIS=new FileInputStream(F); byte B[]=new byte[1048576]; while(read0) { char[] C=new char[sz]; for(int i=0; i=0)) { SB.append(c); } else { SB.append('_'); } } return SB.toString(); } }