public Properties loadUpdateProperties() { File propertiesDirectory; Properties updateProperties = new Properties(); // First we look at local configuration directory propertiesDirectory = new File(System.getProperty("user.home"), PROPERTIES_DIRECTORY_NAME); if (!propertiesDirectory.exists()) { // Second we look at tangara binary path propertiesDirectory = getTangaraPath().getParentFile(); } BufferedInputStream input = null; try { input = new BufferedInputStream( new FileInputStream( new File(propertiesDirectory, getProperty("checkUpdate.fileName")))); updateProperties.load(input); input.close(); return updateProperties; } catch (IOException e) { LOG.warn("Error trying to load update properties"); } finally { IOUtils.closeQuietly(input); } return null; }
private Icon makeIcon(final String gifFile) throws IOException { /* Copy resource into a byte array. This is * necessary because several browsers consider * Class.getResource a security risk because it * can be used to load additional classes. * Class.getResourceAsStream just returns raw * bytes, which we can convert to an image. */ InputStream resource = MyImageView.class.getResourceAsStream(gifFile); if (resource == null) { System.err.println(MyImageView.class.getName() + "/" + gifFile + " not found!!."); return null; } BufferedInputStream in = new BufferedInputStream(resource); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int n; while ((n = in.read(buffer)) > 0) { out.write(buffer, 0, n); } in.close(); out.flush(); buffer = out.toByteArray(); if (buffer.length == 0) { System.err.println("warning: " + gifFile + " is zero-length"); return null; } return new ImageIcon(buffer); }
/** * Load UTF8withBOM or any ansi text file. * * @param filename * @return * @throws java.io.IOException */ public static String loadFileAsString(String filename) throws java.io.IOException { final int BUFLEN = 1024; BufferedInputStream is = new BufferedInputStream(new FileInputStream(filename), BUFLEN); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFLEN); byte[] bytes = new byte[BUFLEN]; boolean isUTF8 = false; int read, count = 0; while ((read = is.read(bytes)) != -1) { if (count == 0 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) { isUTF8 = true; baos.write(bytes, 3, read - 3); // drop UTF8 bom marker } else { baos.write(bytes, 0, read); } count += read; } return isUTF8 ? new String(baos.toByteArray(), "UTF-8") : new String(baos.toByteArray()); } finally { try { is.close(); } catch (Exception ex) { } } }
public void run() { URL url; Base64Encoder base64 = new Base64Encoder(); try { url = new URL(urlString); } catch (MalformedURLException e) { System.err.println("Invalid URL"); return; } try { conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Authorization", "Basic " + base64.encode(user + ":" + pass)); httpIn = new BufferedInputStream(conn.getInputStream(), 8192); } catch (IOException e) { System.err.println("Unable to connect: " + e.getMessage()); return; } int prev = 0; int cur = 0; try { while (keepAlive && (cur = httpIn.read()) >= 0) { if (prev == 0xFF && cur == 0xD8) { jpgOut = new ByteArrayOutputStream(8192); jpgOut.write((byte) prev); } if (jpgOut != null) { jpgOut.write((byte) cur); } if (prev == 0xFF && cur == 0xD9) { synchronized (curFrame) { curFrame = jpgOut.toByteArray(); } frameAvailable = true; jpgOut.close(); } prev = cur; } } catch (IOException e) { System.err.println("I/O Error: " + e.getMessage()); } try { jpgOut.close(); httpIn.close(); } catch (IOException e) { System.err.println("Error closing streams: " + e.getMessage()); } conn.disconnect(); }
/** * Copies a file or directory * * @param src the file or directory to copy * @param dest where copy * @throws IOException */ public static void copyFile(File src, File dest) throws IOException { if (!src.exists()) throw new IOException("File not found '" + src.getAbsolutePath() + "'"); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); byte[] read = new byte[4096]; int len; while ((len = in.read(read)) > 0) out.write(read, 0, len); out.flush(); out.close(); in.close(); }
// Take a tree of files starting in a directory in a zip file // and copy them to a disk directory, recreating the tree. private int unpackZipFile( File inZipFile, String directory, String parent, boolean suppressFirstPathElement) { int count = 0; if (!inZipFile.exists()) return count; parent = parent.trim(); if (!parent.endsWith(File.separator)) parent += File.separator; if (!directory.endsWith(File.separator)) directory += File.separator; File outFile = null; try { ZipFile zipFile = new ZipFile(inZipFile); Enumeration zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipEntries.nextElement(); String name = entry.getName().replace('/', File.separatorChar); if (name.startsWith(directory)) { if (suppressFirstPathElement) name = name.substring(directory.length()); outFile = new File(parent + name); // Create the directory, just in case if (name.indexOf(File.separatorChar) >= 0) { String p = name.substring(0, name.lastIndexOf(File.separatorChar) + 1); File dirFile = new File(parent + p); dirFile.mkdirs(); } if (!entry.isDirectory()) { System.out.println("Installing " + outFile); // Copy the file BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); BufferedInputStream in = new BufferedInputStream(zipFile.getInputStream(entry)); int size = 1024; int n = 0; byte[] b = new byte[size]; while ((n = in.read(b, 0, size)) != -1) out.write(b, 0, n); in.close(); out.flush(); out.close(); // Count the file count++; } } } zipFile.close(); } catch (Exception e) { System.err.println("...an error occured while installing " + outFile); e.printStackTrace(); System.err.println("Error copying " + outFile.getName() + "\n" + e.getMessage()); return -count; } System.out.println(count + " files were installed."); return count; }
private byte[] readFileForResponse(HtmlRequest htmlRequest) throws IOException { if (htmlRequest.requestedFile.equals("/") || htmlRequest.requestedFile.equals("/" + defaultPage.getName())) { fullPathForFile = rootDirectory.getCanonicalPath() + "\\" + defaultPage.getName(); return prepareDefaultPage(null); } else { fullPathForFile = rootDirectory.getCanonicalPath() + htmlRequest.requestedFile; } File file = new File(fullPathForFile); byte[] buffer = new byte[(int) file.length()]; FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); bis.read(buffer, 0, buffer.length); bis.close(); return buffer; }
/** * Reads GIF image from stream * * @param BufferedInputStream containing GIF file. * @return read status code (0 = no errors) */ public int read(BufferedInputStream is) { init(); if (is != null) { in = is; readHeader(); if (!err()) { readContents(); if (frameCount < 0) { status = STATUS_FORMAT_ERROR; } } } else { status = STATUS_OPEN_ERROR; } try { is.close(); } catch (IOException e) { } return status; }
public Image getImage(String sImage) { Image imReturn = null; try { if (jar == null) { imReturn = this.toolkit.createImage(this.getClass().getClassLoader().getResource(sImage)); } else { // BufferedInputStream bis = new BufferedInputStream(jar.getInputStream(jar.getEntry(sImage))); ByteArrayOutputStream buffer = new ByteArrayOutputStream(4096); int b; while ((b = bis.read()) != -1) { buffer.write(b); } byte[] imageBuffer = buffer.toByteArray(); imReturn = this.toolkit.createImage(imageBuffer); bis.close(); buffer.close(); } } catch (IOException ex) { } return imReturn; }
@Override public void run() { String output = ""; // Get launcher jar File Launcher = new File(mcopy.getMinecraftPath() + "minecrafterr.jar"); jTextArea1.setText( "Checking for Minecraft launcher (minecrafterr.jar) in " + Launcher.getAbsolutePath() + "\n"); if (!Launcher.exists()) { jTextArea1.setText(jTextArea1.getText() + "Error: Could not find launcher!\n"); jTextArea1.setText(jTextArea1.getText() + "Downloading from Minecraft.net...\n"); try { BufferedInputStream in = new BufferedInputStream( new URL("https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft.jar") .openStream()); FileOutputStream fos = new FileOutputStream(mcopy.getMinecraftPath() + "minecrafterr.jar"); BufferedOutputStream bout = new BufferedOutputStream(fos, 1024); byte data[] = new byte[1024]; int x = 0; while ((x = in.read(data, 0, 1024)) >= 0) { bout.write(data, 0, x); } bout.close(); in.close(); } catch (IOException e) { jTextArea1.setText(jTextArea1.getText() + "Download failed..." + "\n"); } jTextArea1.setText(jTextArea1.getText() + "Download successful!" + "\n"); } // Got launcher. jTextArea1.setText(jTextArea1.getText() + "Minecraft launcher found!" + "\n"); jTextArea1.setText(jTextArea1.getText() + "Starting launcher..." + "\n"); try { System.out.println(System.getProperty("os.name")); // Run launcher in new process Process pr = Runtime.getRuntime() .exec( System.getProperty("java.home") + "/bin/java -Ddebug=full -cp " + mcopy.getMinecraftPath() + "minecrafterr.jar net.minecraft.LauncherFrame"); // Grab output BufferedReader out = new BufferedReader(new InputStreamReader(pr.getInputStream())); BufferedReader outERR = new BufferedReader(new InputStreamReader(pr.getErrorStream())); String line = ""; while ((line = out.readLine()) != null || (line = outERR.readLine()) != null) { if (!line.startsWith( "Setting user: "******"\n"; jTextArea1.setText(jTextArea1.getText() + line + "\n"); jTextArea1.setCaretPosition(jTextArea1.getText().length() - 1); } } } catch (IOException e) { jTextArea1.setText(jTextArea1.getText() + e.getMessage() + "\n"); jTextArea1.setCaretPosition(jTextArea1.getText().length() - 1); } // set output Main.Output = output; Main.SPAMDETECT = false; jTextArea1.setText(jTextArea1.getText() + "Error report complete."); jTextArea1.setCaretPosition(jTextArea1.getText().length() - 1); mcopy.analyze(); // Auto-analyze }
public static void main(String[] args) throws IOException { int servPort = Integer.parseInt(args[0]); String ksName = "keystore.jks"; char ksPass[] = "password".toCharArray(); char ctPass[] = "password".toCharArray(); try { KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(ksName), ksPass); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, ctPass); SSLContext sc = SSLContext.getInstance("SSL"); sc.init(kmf.getKeyManagers(), null, null); SSLServerSocketFactory ssf = sc.getServerSocketFactory(); SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(servPort); while (true) { // Listen for a TCP connection request. SSLSocket c = (SSLSocket) s.accept(); BufferedOutputStream out = new BufferedOutputStream(c.getOutputStream(), 1024); BufferedInputStream in = new BufferedInputStream(c.getInputStream(), 1024); byte[] byteBuffer = new byte[1024]; int count = 0; while ((byteBuffer[count] = (byte) in.read()) != -2) { count++; } String newFile = new String(byteBuffer, 0, count, "US-ASCII"); FileOutputStream writer = new FileOutputStream(newFile.trim()); int buffSize = 0; while ((buffSize = in.read(byteBuffer, 0, 1024)) != -1) { int index = 0; if ((index = (new String(byteBuffer, 0, buffSize, "US-ASCII")) .indexOf("------MagicStringCSE283Miami")) == -1) { writer.write(byteBuffer, 0, buffSize); } else { writer.write(byteBuffer, 0, index); break; } } writer.flush(); writer.close(); ZipOutputStream outZip = new ZipOutputStream(new BufferedOutputStream(out)); FileInputStream fin = new FileInputStream(newFile.trim()); BufferedInputStream origin = new BufferedInputStream(fin, 1024); ZipEntry entry = new ZipEntry(newFile.trim()); outZip.putNextEntry(entry); byteBuffer = new byte[1024]; int bytes = 0; while ((bytes = origin.read(byteBuffer, 0, 1024)) != -1) { outZip.write(byteBuffer, 0, bytes); } origin.close(); outZip.flush(); outZip.close(); out.flush(); out.close(); } } catch (Exception e) { System.err.println(e.toString()); } }
private void readData() { if (couldNotFetch) return; String test = VoxelUpdate.url; test = test.toLowerCase(); if (!test.endsWith(".xml")) { return; } try { File xml = new File("plugins/VoxelUpdate/temp.xml"); if (!xml.exists()) { xml.createNewFile(); } BufferedInputStream bi = new BufferedInputStream(new URL(VoxelUpdate.url).openStream()); FileOutputStream fo = new FileOutputStream(xml); BufferedOutputStream bo = new BufferedOutputStream(fo, 1024); byte[] b = new byte[1024]; int i = 0; while ((i = bi.read(b, 0, 1024)) >= 0) { bo.write(b, 0, i); } bo.close(); bi.close(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xml); doc.getDocumentElement().normalize(); Element base = doc.getDocumentElement(); NodeList pluginList = doc.getElementsByTagName("plugin"); xml.delete(); for (i = 0; i < pluginList.getLength(); i++) { Node n = pluginList.item(i); String name = null; HashMap<String, String> _map = new HashMap<String, String>(); if (n.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) n; String version = ""; String url = ""; String authors = ""; String description = ""; try { if (getTagValue("name", e) != null) { name = getTagValue("name", e); } else { return; } if (getTagValue("version", e) != null) { version = getTagValue("version", e); } if (getTagValue("url", e) != null) { url = getTagValue("url", e); } if (getTagValue("authors", e) != null) { authors = getTagValue("authors", e); } if (getTagValue("description", e) != null) { description = getTagValue("description", e); } } catch (NullPointerException ex) { continue; } if (!"".equals(version)) { _map.put("version", version); } if (!"".equals(url)) { _map.put("url", url); } if (!"".equals(authors)) { _map.put("authors", authors); } if (!"".equals(description)) { _map.put("description", description); } map.put(name, _map); } } lastDataFetch = System.currentTimeMillis(); } catch (MalformedURLException e) { VoxelUpdate.log.severe("[VoxelUpdate] Incorrectly formatted URL to data file in preferences"); } catch (IOException e) { VoxelUpdate.log.severe("[VoxelUpdate] Could not assign data to BIS"); VoxelUpdate.log.warning( "[VoxelUpdate] Probably because the link is broken or the server host is down"); VoxelUpdate.log.warning( "[VoxelUpdate] Also, check the properties file... It might be empty. If so, grab the default configuration (from TVB's wiki) after you stop your server, paste it into the .properties, and restart"); VoxelUpdate.log.warning("[VoxelUpdate] ... Turning off data search until a reload..."); couldNotFetch = true; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } }
public boolean doDownload(String plugin) { boolean downloaded = false; try { File dl = new File("plugins/VoxelUpdate/Downloads/" + plugin + ".jar"); if (!dl.getParentFile().isDirectory()) { dl.getParentFile().mkdirs(); } if (!dl.exists()) { dl.createNewFile(); } if (get(plugin, "url") == null) { return false; } BufferedInputStream bi = new BufferedInputStream(new URL(get(plugin, "url")).openStream()); FileOutputStream fo = new FileOutputStream(dl); BufferedOutputStream bo = new BufferedOutputStream(fo, 1024); byte[] b = new byte[1024]; int i = 0; while ((i = bi.read(b, 0, 1024)) >= 0) { bo.write(b, 0, i); } bo.close(); bi.close(); if (VoxelUpdate.autoUpdate) { File dupe = new File("plugins/" + dl.getName()); FileChannel ic = new FileInputStream(dl).getChannel(); FileChannel oc = new FileOutputStream(dupe).getChannel(); ic.transferTo(0, ic.size(), oc); ic.close(); oc.close(); VoxelUpdate.s .getPluginManager() .disablePlugin(VoxelUpdate.s.getPluginManager().getPlugin(plugin)); try { VoxelUpdate.s .getPluginManager() .enablePlugin(VoxelUpdate.s.getPluginManager().loadPlugin(dl)); } catch (Exception e) { VoxelUpdate.log.severe("[VoxelUpdate] Could not reload plugin \"" + plugin + "\""); } } downloaded = true; } catch (MalformedURLException e) { VoxelUpdate.log.severe( "[VoxelUpdate] Incorrectly formatted URL for download of \"" + plugin + "\""); return downloaded; } catch (FileNotFoundException e) { VoxelUpdate.log.severe("[VoxelUpdate] Could not save data to VoxelUpdate/Downloads"); e.printStackTrace(); return downloaded; } catch (IOException e) { VoxelUpdate.log.severe("[VoxelUpdate] Could not assign data to BIS"); e.printStackTrace(); return downloaded; } return downloaded; }