Java tutorial
/* * CHGManager Computer Edition * Copyright (C) 2013 Chunky Hosting LLC * Made by: ImThatPedoBear * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.chunkyhosting.Roe.computer.CHGManager.utilities; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.security.MessageDigest; import java.util.HashMap; import net.chunkyhosting.Roe.computer.CHGManager.CHGManager; import net.chunkyhosting.Roe.computer.CHGManager.gui.dialogs.DownloadNotice; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * @author ImThatPedoBear * */ public class Install { private HashMap<JSONObject, URL> downloads = new HashMap<JSONObject, URL>(); public Install() { switch (this.verifyInstall()) { case 0: //Bad URL. break; case 1: //The response returned null. break; case 2: new DownloadNotice(this.getDownloads()); break; case 3: System.out.println("Your CHGManager install should be ok!"); break; case 4: System.out.println("An IOError occoured!"); break; } } public int verifyInstall() { String json = "NULL"; try { json = CHGManager.getInstance().getNetwork() .downloadContentFromURL(new URL(CHGManager.getInstance().getUrl() + "/download.json")); } catch (MalformedURLException e) { return 0; } if (json == null) { return 1; } else if (json.equalsIgnoreCase("IOError")) { return 4; } JSONObject meh = new JSONObject(json); JSONArray array = meh.getJSONArray("Files"); System.out.println(meh.toString(4)); for (int i = 0; i < array.length(); i++) { JSONObject jsonObject = array.getJSONObject(i); File file = new File(CHGManager.getInstance().getSettings().getWorkingDirectory() + File.separator + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension")); if (!file.exists()) { System.err.println("File: " + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension") + ", is missing! Adding to download list."); try { this.getDownloads().put(jsonObject, new URL(CHGManager.getInstance().getUrl() + "/" + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension"))); } catch (JSONException e) { System.err.println("ERROR(" + e.getMessage() + "): An error occoured while adding \"" + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension") + "\" to the download list!"); } catch (MalformedURLException e) { System.err.println("ERROR(" + e.getMessage() + "): An error occoured while adding \"" + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension") + "\" to the download list!"); } } if (!getFileMD5(file.toString(), file.exists()).equalsIgnoreCase(jsonObject.getString("MD5"))) { if (!this.getDownloads().containsKey(jsonObject)) { file.delete(); try { this.getDownloads().put(jsonObject, new URL(CHGManager.getInstance().getUrl() + "/" + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension"))); } catch (JSONException e) { System.err.println("ERROR(" + e.getMessage() + "): An error occoured while adding \"" + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension") + "\" to the download list!"); } catch (MalformedURLException e) { System.err.println("ERROR(" + e.getMessage() + "): An error occoured while adding \"" + jsonObject.getString("Folder") + File.separator + jsonObject.getString("FileName") + "." + jsonObject.getString("Extension") + "\" to the download list!"); } } } System.out.println("File (" + file.toString() + ") MD5: " + getFileMD5(file.toString(), false) + ", " + jsonObject.getString("MD5")); } if (this.getDownloads().size() != 0) { return 2; } return 3; } public static String getFileMD5(String filename, boolean exist) { byte[] b = new byte[1]; try { InputStream fis = new FileInputStream(filename); byte[] buffer = new byte[1024]; MessageDigest complete = MessageDigest.getInstance("MD5"); int numRead; do { numRead = fis.read(buffer); if (numRead > 0) { complete.update(buffer, 0, numRead); } } while (numRead != -1); fis.close(); b = complete.digest(); } catch (Exception e) { if (exist) { e.printStackTrace(); } } String result = ""; for (int i = 0; i < b.length; i++) { result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1); } System.out.println("MD5'ed " + filename + ": " + result); return result; } public HashMap<JSONObject, URL> getDownloads() { return this.downloads; } public void setDownloads(HashMap<JSONObject, URL> downloads) { this.downloads = downloads; } }