Search in sources :

Example 41 with BufferedOutputStream

use of java.io.BufferedOutputStream in project MinecraftForge by MinecraftForge.

the class AccessTransformer method processJar.

private static void processJar(File inFile, File outFile, AccessTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;
    ZipOutputStream outJar = null;
    try {
        try {
            inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open input file: " + e.getMessage());
        }
        try {
            outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open output file: " + e.getMessage());
        }
        ZipEntry entry;
        while ((entry = inJar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                outJar.putNextEntry(entry);
                continue;
            }
            byte[] data = new byte[4096];
            ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
            int len;
            do {
                len = inJar.read(data);
                if (len > 0) {
                    entryBuffer.write(data, 0, len);
                }
            } while (len != -1);
            byte[] entryData = entryBuffer.toByteArray();
            String entryName = entry.getName();
            if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
                ClassNode cls = new ClassNode();
                ClassReader rdr = new ClassReader(entryData);
                rdr.accept(cls, 0);
                String name = cls.name.replace('/', '.').replace('\\', '.');
                for (AccessTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }
            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        IOUtils.closeQuietly(outJar);
        IOUtils.closeQuietly(inJar);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) BufferedOutputStream(java.io.BufferedOutputStream)

Example 42 with BufferedOutputStream

use of java.io.BufferedOutputStream in project MinecraftForge by MinecraftForge.

the class MarkerTransformer method processJar.

private static void processJar(File inFile, File outFile, MarkerTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;
    ZipOutputStream outJar = null;
    try {
        try {
            inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open input file: " + e.getMessage());
        }
        try {
            outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open output file: " + e.getMessage());
        }
        ZipEntry entry;
        while ((entry = inJar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                outJar.putNextEntry(entry);
                continue;
            }
            byte[] data = new byte[4096];
            ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
            int len;
            do {
                len = inJar.read(data);
                if (len > 0) {
                    entryBuffer.write(data, 0, len);
                }
            } while (len != -1);
            byte[] entryData = entryBuffer.toByteArray();
            String entryName = entry.getName();
            if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
                ClassNode cls = new ClassNode();
                ClassReader rdr = new ClassReader(entryData);
                rdr.accept(cls, 0);
                String name = cls.name.replace('/', '.').replace('\\', '.');
                for (MarkerTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }
            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        IOUtils.closeQuietly(outJar);
        IOUtils.closeQuietly(inJar);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) BufferedOutputStream(java.io.BufferedOutputStream)

Example 43 with BufferedOutputStream

use of java.io.BufferedOutputStream in project archaius by Netflix.

the class DynamicURLConfigurationTestWithFileURL method populateFile.

private void populateFile(File temporary, String prop1, String prop2) throws IOException {
    String s = prop1 + "\n" + prop2 + "\n";
    byte[] data = s.getBytes("UTF-8");
    OutputStream out = null;
    try {
        out = new BufferedOutputStream(new FileOutputStream(temporary, true), 8 * 1024);
        out.write(data, 0, data.length);
    } finally {
        if (null != out) {
            out.flush();
            out.close();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 44 with BufferedOutputStream

use of java.io.BufferedOutputStream in project atlas by alibaba.

the class ApBuildTask method createAP.

private File createAP(File apkFile, BaseVariantOutputData variantOutputData, AppVariantContext appVariantContext) throws IOException {
    File jarshrinkLog = new File(variantOutputData.getOutputFile().getParentFile().getParentFile(), "jar-shrink.log");
    File proguardOut = new File(String.valueOf(variantOutputData.getScope().getGlobalScope().getBuildDir()) + "/outputs/mapping/" + variantOutputData.getScope().getVariantScope().getVariantConfiguration().getDirName());
    // 生成build.ap
    String path = apkFile.getAbsolutePath();
    int index = path.lastIndexOf(".apk");
    File APFile = new File(path.substring(0, index) + ".ap");
    FileOutputStream fileOutputStream = new FileOutputStream(APFile);
    JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(fileOutputStream));
    addFile(jos, variantOutputData.manifestProcessorTask.getManifestOutputFile(), "AndroidManifest.xml");
    addFile(jos, apkFile, "android.apk");
    addFile(jos, new File(variantOutputData.processResourcesTask.getTextSymbolOutputDir(), "R.txt"), "R.txt");
    addFile(jos, appBuildInfo.getPackageIdFile());
    addFile(jos, appBuildInfo.getDependenciesFile());
    addFile(jos, appBuildInfo.getBuildInfoFile());
    addFile(jos, appBuildInfo.getVersionPropertiesFile());
    for (File file : appBuildInfo.getOtherFiles()) {
        addFile(jos, file);
    }
    for (String filePath : appBuildInfo.getOtherFilesMap().keySet()) {
        addFile(jos, filePath, appBuildInfo.getOtherFilesMap().get(filePath));
    }
    addFile(jos, jarshrinkLog, jarshrinkLog.getName());
    addFile(jos, getApkFiles(APFile.getParentFile().getParentFile(), appVariantContext), "apk-files.txt");
    // 如果存在着proguard文件,加入结果信息
    if (null != proguardOut && proguardOut.exists() && (new File(proguardOut, "mapping.txt").exists() || new File(proguardOut, "full-mapping.txt").exists())) {
        File usageFile = new File(proguardOut, "usage.txt");
        File mappingFile = new File(proguardOut, "mapping.txt");
        addFile(jos, usageFile, "usage.txt");
        addFile(jos, mappingFile, "mapping.txt");
        addFile(jos, new File(proguardOut, "full-mapping.txt"), "full-mapping.txt");
        addFile(jos, new File(proguardOut, "mapping.data"), "mapping.data");
    } else if (null != appVariantContext.apContext.getApExploredFolder() && appVariantContext.apContext.getApExploredFolder().exists()) {
        File lastApDir = appVariantContext.apContext.getApExploredFolder();
        File usageFile = new File(lastApDir, "usage.txt");
        File mappingFile = new File(lastApDir, "mapping.txt");
        addFile(jos, usageFile, "usage.txt");
        addFile(jos, mappingFile, "mapping.txt");
        addFile(jos, new File(lastApDir, "full-mapping.txt"), "full-mapping.txt");
        addFile(jos, new File(lastApDir, "mapping.data"), "mapping.data");
    }
    IOUtils.closeQuietly(jos);
    if (null != fileOutputStream) {
        IOUtils.closeQuietly(fileOutputStream);
    }
    return APFile;
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) BufferedOutputStream(java.io.BufferedOutputStream)

Example 45 with BufferedOutputStream

use of java.io.BufferedOutputStream in project atlas by alibaba.

the class TPatchTool method zipBundle.

/**
     * 将一个文件夹转换为so
     *
     * @param toZipFolder
     * @param soOutputFile
     */
private void zipBundle(File toZipFolder, File soOutputFile) throws IOException {
    FileOutputStream fileOutputStream = null;
    JarOutputStream jos = null;
    try {
        // 生成so文件
        Manifest manifest = createManifest();
        fileOutputStream = new FileOutputStream(soOutputFile);
        jos = new JarOutputStream(new BufferedOutputStream(fileOutputStream), manifest);
        jos.setLevel(9);
        //            jos.setComment(baseApkVersion+"@"+newApkVersion);
        // Add ZIP entry to output stream.
        File[] files = toZipFolder.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                addDirectory(jos, file, file.getName());
            } else {
                addFile(jos, file);
            }
        }
    } finally {
        IOUtils.closeQuietly(jos);
        if (null != fileOutputStream) {
            IOUtils.closeQuietly(fileOutputStream);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)2418 FileOutputStream (java.io.FileOutputStream)1659 IOException (java.io.IOException)1239 File (java.io.File)1060 OutputStream (java.io.OutputStream)791 BufferedInputStream (java.io.BufferedInputStream)527 InputStream (java.io.InputStream)374 FileInputStream (java.io.FileInputStream)332 DataOutputStream (java.io.DataOutputStream)242 ByteArrayOutputStream (java.io.ByteArrayOutputStream)228 ZipEntry (java.util.zip.ZipEntry)212 ZipOutputStream (java.util.zip.ZipOutputStream)209 FileNotFoundException (java.io.FileNotFoundException)191 ZipFile (java.util.zip.ZipFile)115 ArrayList (java.util.ArrayList)107 URL (java.net.URL)101 ObjectOutputStream (java.io.ObjectOutputStream)99 PrintStream (java.io.PrintStream)98 Test (org.junit.Test)94 ByteArrayInputStream (java.io.ByteArrayInputStream)89