Example usage for java.io OutputStream close

List of usage examples for java.io OutputStream close

Introduction

In this page you can find the example usage for java.io OutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:com.teleca.jamendo.util.download.DownloadTask.java

private static void downloadCover(DownloadJob job) {

    PlaylistEntry mPlaylistEntry = job.getPlaylistEntry();
    String mDestination = job.getDestination();
    String path = DownloadHelper.getAbsolutePath(mPlaylistEntry, mDestination);
    File file = new File(path + "/" + "cover.jpg");
    // check if cover already exists
    if (file.exists()) {
        Log.v(JamendoApplication.TAG, "File exists - nothing to do");
        return;/* ww  w  . j a va  2 s. c  om*/
    }

    String albumUrl = mPlaylistEntry.getAlbum().getImage();
    if (albumUrl == null) {
        Log.w(JamendoApplication.TAG, "album Url = null. This should not happened");
        return;
    }
    albumUrl = albumUrl.replace("1.100", "1.500");

    InputStream stream = null;
    URL imageUrl;
    Bitmap bmp = null;

    // download cover
    try {
        imageUrl = new URL(albumUrl);

        try {
            stream = imageUrl.openStream();
            bmp = BitmapFactory.decodeStream(stream);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.v(JamendoApplication.TAG, "download Cover IOException");
            e.printStackTrace();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        Log.v(JamendoApplication.TAG, "download CoverMalformedURLException");
        e.printStackTrace();
    }

    // save cover to album directory
    if (bmp != null) {

        try {
            file.createNewFile();
            OutputStream outStream = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();

            Log.v(JamendoApplication.TAG, "Album cover saved to sd");

        } catch (FileNotFoundException e) {
            Log.w(JamendoApplication.TAG, "FileNotFoundException");

        } catch (IOException e) {
            Log.w(JamendoApplication.TAG, "IOException");
        }

    }
}

From source file:com.ariatemplates.attester.maven.ArtifactExtractor.java

public static void unzip(File zipFile, File outputFolder) throws IOException {
    ZipInputStream zipInputStream = null;
    try {//from w  ww  .  j ava 2  s  .  c o  m
        ZipEntry entry = null;
        zipInputStream = new ZipInputStream(FileUtils.openInputStream(zipFile));
        while ((entry = zipInputStream.getNextEntry()) != null) {
            File outputFile = new File(outputFolder, entry.getName());

            if (entry.isDirectory()) {
                outputFile.mkdirs();
                continue;
            }

            OutputStream outputStream = null;
            try {
                outputStream = FileUtils.openOutputStream(outputFile);
                IOUtils.copy(zipInputStream, outputStream);
                outputStream.close();
            } catch (IOException exception) {
                outputFile.delete();
                throw new IOException(exception);
            } finally {
                IOUtils.closeQuietly(outputStream);
            }
        }
        zipInputStream.close();
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}

From source file:edu.dartmouth.cs.dartcard.HttpUtilities.java

private static HttpURLConnection makeGetConnection(URL url, byte[] bytes) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);/*from www.j  a va  2s . c  o  m*/
    conn.setUseCaches(false);
    conn.setFixedLengthStreamingMode(bytes.length);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

    OutputStream out = conn.getOutputStream();
    out.write(bytes);
    out.close();

    return conn;
}

From source file:hudson.remoting.PipeTest.java

private static void write(Pipe pipe) throws IOException {
    OutputStream os = pipe.getOut();
    byte[] buf = new byte[384];
    for (int i = 0; i < 256; i++) {
        Arrays.fill(buf, (byte) i);
        os.write(buf, 0, 256);/* w  w w. jav a  2  s.c  o  m*/
    }
    os.close();
}

From source file:org.dataconservancy.dcs.integration.main.ManualDepositIT.java

private static void initFiles() throws IOException {
    final String fileName = "file.1.0.png";
    baseDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    File file = new File(baseDir, fileName);
    sampleFilePath = file.getAbsolutePath();

    OutputStream out = FileUtils.openOutputStream(file);
    InputStream in = FileRoundTripIT.class.getResourceAsStream("/" + fileName);
    IOUtils.copy(in, out);//from ww  w  .j  av a2 s .  c  o  m
    out.close();
}

From source file:be.i8c.sag.util.FileUtils.java

/**
 * Copy a directory and it's content// w  w w  .  ja v a2 s . c  o m
 *
 * @param from The directory to copy
 * @param to Where to copy it to
 * @param deleteOnExit If true, delete the files once the application has closed.
 * @throws IOException When failed to write to a file
 * @throws FileNotFoundException If a file could not be found
 */
public static void copyDirectory(File from, File to, boolean deleteOnExit) throws IOException {
    File[] children = from.listFiles();
    if (children != null) {
        for (File child : children) {
            // Extract the file
            InputStream in = new FileInputStream(child);
            File outputFile = createFile(new File(to, child.getName()));
            if (deleteOnExit)
                outputFile.deleteOnExit();

            OutputStream out = new FileOutputStream(outputFile);
            try {
                IOUtils.copy(in, out);
            } finally {
                out.flush();
                in.close();
                out.close();
            }
        }
    }
}

From source file:dk.netarkivet.common.utils.cdx.CDXUtils.java

/**
  * Applies createCDXRecord() to all ARC/WARC files in a directory, creating
  * one CDX file per ARC/WARC file.//from ww w  . j  a v  a 2s .co m
  * Note, any exceptions during index generation are logged at level FINE
  * but otherwise ignored.
  * Exceptions creating any cdx file are logged at level WARNING but
  * otherwise ignored.
  * CDX files are named as the ARC/WARC files except ".(w)arc" or
  * ".(w)arc.gz" is extended with ".cdx"
  *
  * @param archiveProfile archive profile including filters, patterns, etc.
  * @param archiveFileDirectory A directory with archive files to generate
  * index for
  * @param cdxFileDirectory A directory to generate CDX files in
  * @throws ArgumentNotValid if any of directories are null or is not an
  * existing directory, or if cdxFileDirectory is not writable.
  */
public static void generateCDX(ArchiveProfile archiveProfile, File archiveFileDirectory, File cdxFileDirectory)
        throws ArgumentNotValid {
    ArgumentNotValid.checkNotNull(archiveProfile, "ArchiveProfile archiveProfile");
    ArgumentNotValid.checkNotNull(archiveFileDirectory, "File archiveFileDirectory");
    ArgumentNotValid.checkNotNull(cdxFileDirectory, "File cdxFileDirectory");
    if (!archiveFileDirectory.isDirectory() || !archiveFileDirectory.canRead()) {
        throw new ArgumentNotValid(
                "The directory for arc files '" + archiveFileDirectory + "' is not a readable directory");
    }
    if (!cdxFileDirectory.isDirectory() || !cdxFileDirectory.canWrite()) {
        throw new ArgumentNotValid(
                "The directory for cdx files '" + archiveFileDirectory + "' is not a writable directory");
    }
    Map<File, Exception> exceptions = new HashMap<File, Exception>();
    File[] filesToProcess = archiveFileDirectory.listFiles(archiveProfile.filename_filter);
    if (filesToProcess.length == 0) {
        log.warn("Found no related arcfiles to process in the archive dir '"
                + archiveFileDirectory.getAbsolutePath() + "'.");
    } else {
        log.debug("Found " + filesToProcess.length + " related arcfiles to process in the archive dir '"
                + archiveFileDirectory.getAbsolutePath() + "'.");
    }
    for (File arcfile : filesToProcess) {
        File cdxfile = new File(cdxFileDirectory, arcfile.getName() + FileUtils.CDX_EXTENSION);
        try {
            OutputStream cdxstream = null;
            try {
                cdxstream = new FileOutputStream(cdxfile);
                writeCDXInfo(arcfile, cdxstream);
            } finally {
                if (cdxstream != null) {
                    cdxstream.close();
                }
            }
        } catch (Exception e) {
            exceptions.put(cdxfile, e);
        }
    }
    // Log any errors
    if (exceptions.size() > 0) {
        StringBuilder errorMsg = new StringBuilder("Exceptions during cdx file generation:\n");
        for (Map.Entry<File, Exception> fileException : exceptions.entrySet()) {
            errorMsg.append("Could not create cdxfile '");
            errorMsg.append(fileException.getKey().getAbsolutePath());
            errorMsg.append("':\n");
            errorMsg.append(ExceptionUtils.getStackTrace(fileException.getValue()));
            errorMsg.append('\n');
        }
        log.debug(errorMsg.toString());
    }
}

From source file:Main.java

/** Writes a <code>Properties</code> object to
 * a <code>File</code>.//from  w  ww .  ja  va 2  s.co  m
 */
public static void write(Properties p, File file) throws IOException {
    OutputStream out = null;
    try {
        out = new FileOutputStream(file);
        p.store(out, "");
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Throwable t) {
            }
        }
    }
}

From source file:Main.java

public static void saveBitmap2file(Bitmap bmp, File file) {

    OutputStream stream = null;
    try {/*w w  w  .  j  a va 2s.  c  o m*/
        stream = new FileOutputStream(file.getAbsolutePath());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);

    try {
        stream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:edu.dartmouth.cs.dartcard.HttpUtilities.java

private static HttpURLConnection makePostConnection(URL url, byte[] bytes) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);//  w w w.jav a2  s.  c  om
    conn.setUseCaches(false);
    conn.setFixedLengthStreamingMode(bytes.length);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

    // Add in API key
    String authKey = new String(Base64.encodeBase64((key + ":").getBytes()));
    conn.setRequestProperty("Authorization", "Basic " + authKey);

    OutputStream out = conn.getOutputStream();
    out.write(bytes);
    out.close();

    return conn;
}