/** Returns true if the two InputStreams differ. */ private static boolean differs(InputStream oldIS, InputStream newIS) throws IOException { int newSize = 0; int oldSize; int total = 0; boolean retVal = false; try { while (newSize != -1) { newSize = newIS.read(newBytes); oldSize = oldIS.read(oldBytes); if (newSize != oldSize) { if (_debug) { System.out.println( "\tread sizes differ: " + newSize + " " + oldSize + " total " + total); } retVal = true; break; } if (newSize > 0) { while (--newSize >= 0) { total++; if (newBytes[newSize] != oldBytes[newSize]) { if (_debug) { System.out.println("\tbytes differ at " + total); } retVal = true; break; } if (retVal) { // Jump out break; } newSize = 0; } } } } catch (IOException ioE) { throw ioE; } finally { try { oldIS.close(); } catch (IOException e) { // Ignore } try { newIS.close(); } catch (IOException e) { // Ignore } } return retVal; }
/** Returns true if the two InputStreams differ. */ private static boolean differs(InputStream oldIS, InputStream newIS) throws IOException { int newSize = 0; int oldSize; int total = 0; while (newSize != -1) { newSize = newIS.read(newBytes); oldSize = oldIS.read(oldBytes); if (newSize != oldSize) { if (_debug) { System.out.println( "\tread sizes differ: " + newSize + " " + oldSize + " total " + total); } return true; } if (newSize > 0) { while (--newSize >= 0) { total++; if (newBytes[newSize] != oldBytes[newSize]) { if (_debug) { System.out.println("\tbytes differ at " + total); } return true; } } newSize = 0; } } return false; }
private void digest(MessageDigest[] algorithms, Resource r) throws Exception { InputStream in = r.openInputStream(); byte[] data = new byte[BUFFER_SIZE]; int size = in.read(data); while (size > 0) { for (int a = 0; a < algorithms.length; a++) { if (algorithms[a] != null) { algorithms[a].update(data, 0, size); } } size = in.read(data); } }
public static int writeStreamTo( final InputStream input, final OutputStream output, int bufferSize) throws IOException { int available = Math.min(input.available(), 256 * KB); byte[] buffer = new byte[Math.max(bufferSize, available)]; int answer = 0; int count = input.read(buffer); while (count >= 0) { output.write(buffer, 0, count); answer += count; count = input.read(buffer); } return answer; }
private static void writeEntry(JarOutputStream jos, JarEntry entry, InputStream data) throws IOException { jos.putNextEntry(entry); // Read the entry int size = data.read(newBytes); while (size != -1) { jos.write(newBytes, 0, size); size = data.read(newBytes); } data.close(); }
/** * Puts the uninstaller. * * @exception Exception Description of the Exception */ private void putUninstaller() throws Exception { // Me make the .uninstaller directory String dest = translatePath("$INSTALL_PATH") + File.separator + "Uninstaller"; String jar = dest + File.separator + "uninstaller.jar"; File pathMaker = new File(dest); pathMaker.mkdirs(); // We log the uninstaller deletion information UninstallData udata = UninstallData.getInstance(); udata.setUninstallerJarFilename(jar); udata.setUninstallerPath(dest); // We open our final jar file FileOutputStream out = new FileOutputStream(jar); ZipOutputStream outJar = new ZipOutputStream(out); idata.uninstallOutJar = outJar; outJar.setLevel(9); udata.addFile(jar); // We copy the uninstaller InputStream in = getClass().getResourceAsStream("/res/IzPack.uninstaller"); ZipInputStream inRes = new ZipInputStream(in); ZipEntry zentry = inRes.getNextEntry(); while (zentry != null) { // Puts a new entry outJar.putNextEntry(new ZipEntry(zentry.getName())); // Byte to byte copy int unc = inRes.read(); while (unc != -1) { outJar.write(unc); unc = inRes.read(); } // Next one please inRes.closeEntry(); outJar.closeEntry(); zentry = inRes.getNextEntry(); } inRes.close(); // We put the langpack in = getClass().getResourceAsStream("/langpacks/" + idata.localeISO3 + ".xml"); outJar.putNextEntry(new ZipEntry("langpack.xml")); int read = in.read(); while (read != -1) { outJar.write(read); read = in.read(); } outJar.closeEntry(); }
private Class<?> getClassFromStream( final InputStream stream, final String classname, final File container) throws IOException, SecurityException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bytesRead = -1; final byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { baos.write(buffer, 0, bytesRead); } final byte[] classData = baos.toByteArray(); return this.defineClassFromData(container, classData, classname); }
void copy(InputStream in, File dest) throws IOException { dest.getParentFile().mkdirs(); OutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); try { byte[] data = new byte[8192]; int n; while ((n = in.read(data, 0, data.length)) > 0) out.write(data, 0, n); } finally { out.close(); in.close(); } }
private static void writeEntry(JarOutputStream jos, JarEntry entry, InputStream data) throws IOException { jos.putNextEntry(entry); try { // Read the entry int size = data.read(newBytes); while (size != -1) { jos.write(newBytes, 0, size); size = data.read(newBytes); } } catch (IOException ioE) { throw ioE; } finally { try { data.close(); } catch (IOException e) { // Ignore } } }
protected void addRuntimeClasses(JarOutputStream jar) throws IOException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl == null) { cl = getClass().getClassLoader(); } ArrayList<String> runtimeClasses = getRuntimeClasses(cl); for (String c : runtimeClasses) { jar.putNextEntry(new JarEntry(c)); InputStream in = cl.getResourceAsStream(c); int ch; while ((ch = in.read()) != -1) { jar.write((char) ch); } jar.closeEntry(); in.close(); } }
public byte[] getBytes(String className) { try { Tracer.mark(); String realName = className.replace(".", "/"); realName += ".class"; JarEntry je = jf.getJarEntry(realName); InputStream is = jf.getInputStream(je); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[4096]; while (is.available() > 0) { int read = is.read(buff); baos.write(buff, 0, read); } is.close(); return baos.toByteArray(); } catch (Exception e) { } finally { Tracer.unmark(); } return null; }
static String getResourceAsString(String path) { InputStream is = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream outPrint = new PrintStream(out); try { is = Util.class.getClassLoader().getResourceAsStream(path); int content; while ((content = is.read()) != -1) { // convert to char and display it outPrint.print((char) content); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); if (outPrint != null) outPrint.close(); } catch (IOException ex) { ex.printStackTrace(); } } return out.toString(); }
/** * Copies all bytes from the input stream to the output stream. Does not close or flush either * stream. * * @param from the input stream to read from * @param to the output stream to write to * @throws IOException if an I/O error occurs */ private void copy(InputStream from, OutputStream to) throws IOException { int n; while ((n = from.read(copyBuf)) != -1) to.write(copyBuf, 0, n); }