Search in sources :

Example 11 with FileOutputStream

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);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) URL(java.net.URL)

Example 12 with FileOutputStream

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 13 with FileOutputStream

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());
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipOutputStream(java.util.zip.ZipOutputStream) ImmutableList(com.google.common.collect.ImmutableList) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) Test(org.junit.Test)

Example 14 with FileOutputStream

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);
                }
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) File(java.io.File) URL(java.net.URL)

Example 15 with FileOutputStream

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);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) IOException(java.io.IOException)

Aggregations

FileOutputStream (java.io.FileOutputStream)13792 File (java.io.File)8295 IOException (java.io.IOException)6166 FileInputStream (java.io.FileInputStream)2644 OutputStream (java.io.OutputStream)2605 InputStream (java.io.InputStream)2077 BufferedOutputStream (java.io.BufferedOutputStream)1755 FileNotFoundException (java.io.FileNotFoundException)1531 OutputStreamWriter (java.io.OutputStreamWriter)1440 Test (org.junit.Test)1115 ZipEntry (java.util.zip.ZipEntry)734 BufferedWriter (java.io.BufferedWriter)668 ArrayList (java.util.ArrayList)654 ZipOutputStream (java.util.zip.ZipOutputStream)642 BufferedInputStream (java.io.BufferedInputStream)604 ByteArrayOutputStream (java.io.ByteArrayOutputStream)556 PrintWriter (java.io.PrintWriter)530 Properties (java.util.Properties)497 URL (java.net.URL)478 Writer (java.io.Writer)477