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);
}
}
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);
}
}
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();
}
}
}
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);
}
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);
}
}
}
}
Aggregations