/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.channels.FileLock; import java.util.HashMap; import buzzerproxy.db.view.DatabaseEntry; /** * * @author Enger */ public class IO { public IO() { } public boolean lockInstance() { try { final File file = new File(Constant.LOCKFILE); final RandomAccessFile randomAccessFile = new RandomAccessFile( file, "rw"); final FileLock fileLock = randomAccessFile.getChannel().tryLock(); if (fileLock != null) { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { fileLock.release(); randomAccessFile.close(); file.delete(); } catch (Exception e) { System.out.println("Unable to remove lock file: " + Constant.LOCKFILE); } } }); return true; } } catch (Exception e) { System.out.println("Unable to create and/or lock file: " + Constant.LOCKFILE); } return false; } public boolean writeLog(DatabaseEntry db) { try { // Create file FileWriter fstream = new FileWriter( Constant.DB_ALTERNATIVE_LOG_FILE); BufferedWriter out = new BufferedWriter(fstream); out.write("ID: " + db.getid() + " ### Time: " + db.getdatetime() + " ### Clientversion: " + db.getclientversion() + " ### Filetype: " + db.getfiletype() + " ### Search: " + db.getsearch() + " ### Type: " + db.gettype() + " ### IP: " + db.getip()); // Close the output stream out.close(); return true; } catch (Exception e) {// Catch exception if any System.err.println("Error: " + e.getMessage()); return false; } } @SuppressWarnings("unused") public String readFileAsString(String filePath) throws IOException { File f = new File(filePath); if (!f.exists() || !f.isFile() || !f.canRead() || filePath == null) throw new NullPointerException(); StringBuffer fileData = new StringBuffer(); BufferedReader reader = new BufferedReader(new FileReader(filePath)); char[] buf = new char[1024]; int numRead = 0; while ((numRead = reader.read(buf)) != -1) { String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); buf = new char[1024]; } reader.close(); return fileData.toString(); } public byte[] readStreamAsByteArray(String file) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); File f = new File(file); if (f.isFile() && f.exists() && f.canRead()) { FileInputStream is = new FileInputStream(f); byte[] data = new byte[4096]; int count = is.read(data); while (count != -1) { dos.write(data, 0, count); count = is.read(data); } } return baos.toByteArray(); } public boolean isIntNumber(String num) { try { Integer.parseInt(num); } catch (NumberFormatException nfe) { return false; } return true; } // public boolean isIntNumber(String num) { public String getFileExtension(String uri) { String extension = ""; String[] uriFileTyp = uri.split("\\."); extension = uriFileTyp[uriFileTyp.length - 1]; return extension; } // public String getFileExtension(String uri){ public boolean isValidFileTyp(String uri, String filetyp) { boolean is = false; boolean in = false; String[] content = filetyp.split("\\+"); String[] uriFileTyp = uri.split("\\."); for (int i = 0; i < content.length; i++) { // System.out.println("urifiletyp: "+uriFileTyp[uriFileTyp.length - // 1] + " und " + content[i]); if (uriFileTyp[uriFileTyp.length - 1].contains(content[i] .replaceFirst("\\.", ""))) { is = true; // System.out.println(uri + " und filetype: " + content[i] + // " sind gleich"); } else { // System.out.println(uri + " und filetype: " + content[i] + // " sind NICHT gleich"); }// if ( uri.endsWith(content[i])){ } // for ( int i=0;i l = new ArrayList(); // // String[] content = filetyp.split(""); // String fT = ""; // for (int i = 0; i < content.length; i++) { // if (in) { // fT += content[i]; // } // if (content[i].equals("\\.")) { // in = true; // } // if (content[i].equals("\\|")) { // in = false; // fT = ""; // l.add(fT); // } // } // for (int i = 0; i < content.length; i++) { // // String[] uriFileTyp = uri.split("\\."); // System.out.println("urifiletyp: "+uriFileTyp[uriFileTyp.length - 1]); // // for ( String ftyp : l){ // System.out.println("FileTyp: "+ftyp); // } // // for (int i = 0; i < content.length; i++) { // // // // if (uriFileTyp[uriFileTyp.length - 1].endsWith(content[i]) && // !content[i].isEmpty()) { // // is = true; // // System.out.println(uri + " und filetype: " + content[i] + // " sind gleich"); // // } else { // // System.out.println(uri + " und filetype: " + content[i] + // " sind NICHT gleich"); // // }// if ( uri.endsWith(content[i])){ // // } // for ( int i=0;i