| 1 | /* |
| 2 | * Created on Sep 5, 2004 |
| 3 | * |
| 4 | * TODO To change the template for this generated file go to |
| 5 | * Window - Preferences - Java - Code Style - Code Templates |
| 6 | */ |
| 7 | package biz.xsoftware.buildtemplate; |
| 8 | |
| 9 | import java.io.File; |
| 10 | import java.io.FileInputStream; |
| 11 | import java.io.FileOutputStream; |
| 12 | import java.io.IOException; |
| 13 | import java.io.InputStream; |
| 14 | import java.io.OutputStream; |
| 15 | import java.net.URL; |
| 16 | import java.util.logging.Logger; |
| 17 | |
| 18 | /** |
| 19 | * @author Dean Hiller |
| 20 | * |
| 21 | * TODO To change the template for this generated type comment go to |
| 22 | * Window - Preferences - Java - Code Style - Code Templates |
| 23 | */ |
| 24 | public class Util implements FileLocator { |
| 25 | |
| 26 | private final static Logger log = Logger.getLogger(Util.class.getName()); |
| 27 | |
| 28 | public static void copyInToOut(InputStream in, OutputStream out) throws IOException { |
| 29 | byte[] buffer = new byte[10000]; |
| 30 | int bytesRead = 0; |
| 31 | while(bytesRead >= 0) { |
| 32 | out.write(buffer, 0, bytesRead); |
| 33 | bytesRead = in.read(buffer); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public File getFile(URL url) { |
| 38 | String filePart = url.getFile(); |
| 39 | String nameWithClass = filePart.substring(5, filePart.length()); |
| 40 | int index = nameWithClass.indexOf("!"); |
| 41 | String fileName = nameWithClass.substring(0, index); |
| 42 | |
| 43 | File f = new File(fileName); |
| 44 | |
| 45 | if(!f.exists()) { |
| 46 | log.warning("Problem with buildtemplate.jar, contact owner."); |
| 47 | log.warning("Could not find file="+fileName); |
| 48 | log.warning("This message comes from a class in buildtemplate.jar"); |
| 49 | // System.exit(1); |
| 50 | } |
| 51 | |
| 52 | return f; |
| 53 | } |
| 54 | |
| 55 | public static void copyFile(File src, File dest) throws IOException { |
| 56 | dest.createNewFile(); |
| 57 | |
| 58 | FileInputStream in = new FileInputStream(src); |
| 59 | FileOutputStream out = new FileOutputStream(dest); |
| 60 | Util.copyInToOut(in, out); |
| 61 | in.close(); |
| 62 | out.close(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param in1 |
| 67 | * @param in2 |
| 68 | * @return |
| 69 | * @throws IOException |
| 70 | */ |
| 71 | public static boolean inputsAreTheSame(long size, InputStream in1, InputStream in2) throws IOException { |
| 72 | //lie here as we dont' want to compare |
| 73 | if(size > Integer.MAX_VALUE) |
| 74 | return true; |
| 75 | |
| 76 | byte[] buffer1 = new byte[(int)size]; |
| 77 | byte[] buffer2 = new byte[(int)size]; |
| 78 | |
| 79 | buffer1 = readInWholeFile(buffer1, in1); |
| 80 | buffer2 = readInWholeFile(buffer2, in2); |
| 81 | if(buffer1 == null || buffer2 == null) { |
| 82 | log.finer("can't compare the two files, replacing it"); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return arraysEqual(buffer1, buffer2); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the byte buffer array unless it is the incorrect size. |
| 91 | * |
| 92 | * @param b |
| 93 | * @param in |
| 94 | * @return |
| 95 | * @throws IOException |
| 96 | */ |
| 97 | private static byte[] readInWholeFile(byte[] b, InputStream in) throws IOException { |
| 98 | int read = 0; |
| 99 | int totalRead = 0; |
| 100 | while(totalRead < b.length && read >= 0) { |
| 101 | read = in.read(b, totalRead, b.length-totalRead); |
| 102 | totalRead += read; |
| 103 | } |
| 104 | if(read >= 0) //if we got end of stream, we read one too many bytes. |
| 105 | return b; |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | private static boolean arraysEqual(byte[] b1, byte[] b2) { |
| 110 | if(b1.length != b2.length) |
| 111 | return false; |
| 112 | |
| 113 | for(int i = 0; i < b1.length; i++) { |
| 114 | if(b1[i] != b2[i]) |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | } |