use of java.io.FileOutputStream in project cw-omnibus by commonsguy.
the class Downloader method onHandleIntent.
@Override
public void onHandleIntent(Intent i) {
String filename = i.getData().getLastPathSegment();
startForeground(FOREGROUND_ID, buildForegroundNotification(filename));
try {
File output = new File(getFilesDir(), filename);
if (output.exists()) {
output.delete();
}
URL url = new URL(i.getData().toString());
HttpURLConnection c = (HttpURLConnection) url.openConnection();
FileOutputStream fos = new FileOutputStream(output.getPath());
BufferedOutputStream out = new BufferedOutputStream(fos);
try {
InputStream in = c.getInputStream();
byte[] buffer = new byte[8192];
int len = 0;
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
out.flush();
} finally {
fos.getFD().sync();
out.close();
c.disconnect();
}
raiseNotification(i, output, null);
} catch (IOException e2) {
raiseNotification(i, null, e2);
} finally {
stopForeground(true);
}
}
use of java.io.FileOutputStream in project vert.x by eclipse.
the class HttpTest method setupFile.
protected File setupFile(String fileName, String content) throws Exception {
File file = new File(testDir, fileName);
if (file.exists()) {
file.delete();
}
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
out.write(content);
out.close();
return file;
}
use of java.io.FileOutputStream in project buck by facebook.
the class ClassNodeListSupplierTest method testOneJar.
@Test
public void testOneJar() throws IOException {
File jar = new File(tmpDir.getRoot(), "primary.jar");
ZipOutputStream jarOut = new JarOutputStream(new FileOutputStream(jar));
jarOut.putNextEntry(new JarEntry("com/facebook/buck/android/ClassNodeListSupplierTest.class"));
writeClassBytes(ClassNodeListSupplierTest.class, jarOut);
jarOut.close();
Supplier<ImmutableList<ClassNode>> supplier = ClassNodeListSupplier.createMemoized(ImmutableList.of(jar.toPath()));
ImmutableList<ClassNode> classNodes = supplier.get();
assertEquals(1, classNodes.size());
assertEquals(Type.getType(ClassNodeListSupplierTest.class).getInternalName(), classNodes.get(0).name);
// Memoized should always return the same object
assertSame(classNodes, supplier.get());
}
use of java.io.FileOutputStream in project jetty.project by eclipse.
the class SelectiveJarResource method copyTo.
/**
* @see org.eclipse.jetty.util.resource.JarResource#copyTo(java.io.File)
*/
@Override
public void copyTo(File directory) throws IOException {
if (_includes == null)
_includes = DEFAULT_INCLUDES;
if (_excludes == null)
_excludes = DEFAULT_EXCLUDES;
//parts of the jar file are copied
if (!exists())
return;
String urlString = this.getURL().toExternalForm().trim();
int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
if (endOfJarUrl < 0)
throw new IOException("Not a valid jar url: " + urlString);
URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
try (InputStream is = jarFileURL.openConnection().getInputStream();
JarInputStream jin = new JarInputStream(is)) {
JarEntry entry;
while ((entry = jin.getNextJarEntry()) != null) {
String entryName = entry.getName();
LOG.debug("Looking at " + entryName);
String dotCheck = entryName.replace('\\', '/');
dotCheck = URIUtil.canonicalPath(dotCheck);
if (dotCheck == null) {
LOG.info("Invalid entry: " + entryName);
continue;
}
File file = new File(directory, entryName);
if (entry.isDirectory()) {
if (isIncluded(entryName)) {
if (!isExcluded(entryName)) {
// Make directory
if (!file.exists())
file.mkdirs();
} else
LOG.debug("{} dir is excluded", entryName);
} else
LOG.debug("{} dir is NOT included", entryName);
} else {
//entry is a file, is it included?
if (isIncluded(entryName)) {
if (!isExcluded(entryName)) {
// make directory (some jars don't list dirs)
File dir = new File(file.getParent());
if (!dir.exists())
dir.mkdirs();
// Make file
try (OutputStream fout = new FileOutputStream(file)) {
IO.copy(jin, fout);
}
// touch the file.
if (entry.getTime() >= 0)
file.setLastModified(entry.getTime());
} else
LOG.debug("{} file is excluded", entryName);
} else
LOG.debug("{} file is NOT included", entryName);
}
}
Manifest manifest = jin.getManifest();
if (manifest != null) {
if (isIncluded("META-INF") && !isExcluded("META-INF")) {
File metaInf = new File(directory, "META-INF");
metaInf.mkdir();
File f = new File(metaInf, "MANIFEST.MF");
try (OutputStream fout = new FileOutputStream(f)) {
manifest.write(fout);
}
}
}
}
}
use of java.io.FileOutputStream in project jetty.project by eclipse.
the class FileSessionDataStore method doStore.
/**
* @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long)
*/
@Override
public void doStore(String id, SessionData data, long lastSaveTime) throws Exception {
File file = null;
if (_storeDir != null) {
//remove any existing file for the session
file = getFile(_storeDir, id);
if (file != null && file.exists())
file.delete();
//make a fresh file using the latest session expiry
file = new File(_storeDir, getFileNameWithExpiry(data));
try (FileOutputStream fos = new FileOutputStream(file, false)) {
save(fos, id, data);
} catch (Exception e) {
e.printStackTrace();
if (file != null)
// No point keeping the file if we didn't save the whole session
file.delete();
throw new UnwriteableSessionDataException(id, _context, e);
}
}
}
Aggregations