Search in sources :

Example 96 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 97 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 98 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 99 with BufferedOutputStream

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

the class IgnoredNamesTest method testEncodeDecode.

/**
     * Make sure that encoding and decoding IgnoredNames object is 1:1 operation.
     * @throws FileNotFoundException
     * @throws IOException 
     */
@Test
public void testEncodeDecode() throws FileNotFoundException, IOException {
    IgnoredNames in = new IgnoredNames();
    // Add file and directory to list of ignored items.
    in.add("f:foo.txt");
    in.add("d:bar");
    // Create an exception listener to detect errors while encoding and decoding
    final LinkedList<Exception> exceptions = new LinkedList<Exception>();
    ExceptionListener listener = new ExceptionListener() {

        @Override
        public void exceptionThrown(Exception e) {
            exceptions.addLast(e);
        }
    };
    // Actually create the file and directory for much better test coverage.
    File tmpdir = FileUtilities.createTemporaryDirectory("ignoredNames");
    File foo = new File(tmpdir, "foo.txt");
    foo.createNewFile();
    assertTrue(foo.isFile());
    File bar = new File(tmpdir, "bar");
    bar.mkdir();
    assertTrue(bar.isDirectory());
    // Store the IgnoredNames object as XML file.
    File testXML = new File(tmpdir, "Test.xml");
    XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(testXML)));
    e.setExceptionListener(listener);
    e.writeObject(in);
    e.close();
    // Restore the IgnoredNames object from XML file.
    XMLDecoder d = new XMLDecoder(new FileInputStream(testXML));
    IgnoredNames in2 = (IgnoredNames) d.readObject();
    d.close();
    // Verify that the XML encoding/decoding did not fail.
    if (!exceptions.isEmpty()) {
        AssertionFailedError afe = new AssertionFailedError("Got " + exceptions.size() + " exception(s)");
        // Can only chain one of the exceptions. Take the first one.
        afe.initCause(exceptions.getFirst());
        throw afe;
    }
    // Make sure the complete list of items is equal after decoding.
    // This will is a simple casual test that cannot verify that sub-classes
    // are intact. For that there are the following tests.
    assertTrue(in.getItems().containsAll(in2.getItems()));
    // Use the restored object to test the matching of file and directory.
    assertTrue(in2.ignore("foo.txt"));
    assertTrue(in2.ignore("bar"));
    assertTrue(in2.ignore(foo));
    assertTrue(in2.ignore(bar));
    // Cleanup.
    FileUtilities.removeDirs(tmpdir);
}
Also used : XMLEncoder(java.beans.XMLEncoder) FileOutputStream(java.io.FileOutputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) AssertionFailedError(junit.framework.AssertionFailedError) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) LinkedList(java.util.LinkedList) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) CAnalyzerFactoryTest(org.opensolaris.opengrok.analysis.c.CAnalyzerFactoryTest) Test(org.junit.Test)

Example 100 with BufferedOutputStream

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

the class LocalStorageImpl method unzipImportArchive.

public void unzipImportArchive(UpdateStatus update) throws LocalStorageException {
    // at this point, we've verified that:
    //   1) we have enough space on the device
    //   2) the archive downloaded is what was expected
    ZipInputStream zis = null;
    String newPrefix = "com.openmeap.storage." + update.getUpdateHeader().getHash().getValue();
    File hashHolder = null;
    String hashRootAbsolutePath = "";
    try {
        hashHolder = new File(activity.getFilesDir(), newPrefix);
        hashHolder.mkdir();
        hashRootAbsolutePath = hashHolder.getAbsolutePath();
    } catch (Exception e) {
        System.out.println("Exception thrown while creating hash folder.");
        System.out.println(e);
    }
    try {
        zis = new ZipInputStream(getImportArchiveInputStream());
        ZipEntry ze;
        while ((ze = zis.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                //		    		continue;
                try {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Writing directory structure in phone memory.");
                    File directoryStructure = new File(hashRootAbsolutePath, ze.getName());
                    directoryStructure.mkdirs();
                } catch (Exception e) {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Exception thrown while writing directory structure.");
                    System.out.println(e);
                }
            } else {
                try {
                    String osSeperator = System.getProperty("file.separator");
                    int seperatorLastIndex = ze.getName().lastIndexOf(osSeperator);
                    String fileName = ze.getName().substring(seperatorLastIndex + 1, ze.getName().length());
                    String fileNameParentDirectoryPrefix = "";
                    String absolutePathFromPrefix = "";
                    if (seperatorLastIndex != -1 && seperatorLastIndex != 0) {
                        fileNameParentDirectoryPrefix = ze.getName().substring(0, seperatorLastIndex);
                        absolutePathFromPrefix = hashRootAbsolutePath + osSeperator + fileNameParentDirectoryPrefix;
                    } else {
                        absolutePathFromPrefix = hashRootAbsolutePath + osSeperator;
                    }
                    URI osResourePathForThisFile = URI.create(absolutePathFromPrefix);
                    File writableFileReference = new File(osResourePathForThisFile.getPath(), fileName);
                    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(writableFileReference.getAbsolutePath(), true), 1024);
                    try {
                        byte[] buffer = new byte[1024];
                        int count;
                        while ((count = zis.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, count);
                        }
                    } catch (Exception e) {
                        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                        System.out.println("Exception while writing file contents.");
                        System.out.println(e);
                    } finally {
                        outputStream.close();
                    }
                } catch (Exception e) {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Unknown exception.");
                    System.out.println(e);
                }
            }
        //		    	Commenting following code to make use of file:/// alternate to content://
        //		        OutputStream baos = openFileOutputStream(newPrefix,ze.getName());		        
        //		        try {
        //		        	byte[] buffer = new byte[1024];
        //		        	int count;
        //		        	while ((count = zis.read(buffer)) != -1) {
        //		        		baos.write(buffer, 0, count);
        //		        	}
        //		        }
        //		        catch( Exception e ) {
        //		        	;// TODO: something, for the love of god.
        //		        }
        //		        finally {
        //		        	baos.close();
        //		        }
        }
    } catch (Exception e) {
        throw new LocalStorageException(e);
    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                throw new GenericRuntimeException(e);
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) URI(java.net.URI) LocalStorageException(com.openmeap.thinclient.LocalStorageException) LocalStorageException(com.openmeap.thinclient.LocalStorageException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UpdateException(com.openmeap.thinclient.update.UpdateException) ZipInputStream(java.util.zip.ZipInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)1074 FileOutputStream (java.io.FileOutputStream)754 IOException (java.io.IOException)549 File (java.io.File)432 OutputStream (java.io.OutputStream)312 BufferedInputStream (java.io.BufferedInputStream)207 InputStream (java.io.InputStream)151 DataOutputStream (java.io.DataOutputStream)145 FileInputStream (java.io.FileInputStream)125 ZipOutputStream (java.util.zip.ZipOutputStream)102 FileNotFoundException (java.io.FileNotFoundException)100 ZipEntry (java.util.zip.ZipEntry)91 ByteArrayOutputStream (java.io.ByteArrayOutputStream)86 ZipFile (java.util.zip.ZipFile)57 XmlSerializer (org.xmlpull.v1.XmlSerializer)57 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)56 URL (java.net.URL)50 ObjectOutputStream (java.io.ObjectOutputStream)48 ByteArrayInputStream (java.io.ByteArrayInputStream)39 PrintStream (java.io.PrintStream)38