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.jsonstore.util.JSONStoreUtil.java

public static void unpack(InputStream in, File targetDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(in);

    ZipEntry entry;// w  w w.ja v  a2  s  .c  om
    while ((entry = zin.getNextEntry()) != null) {
        String extractFilePath = entry.getName();
        if (extractFilePath.startsWith("/") || extractFilePath.startsWith("\\")) {
            extractFilePath = extractFilePath.substring(1);
        }
        File extractFile = new File(targetDir.getPath() + File.separator + extractFilePath);
        if (entry.isDirectory()) {
            if (!extractFile.exists()) {
                extractFile.mkdirs();
            }
            continue;
        } else {
            File parent = extractFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
        }

        // if not directory instead of the previous check and continue
        OutputStream os = new BufferedOutputStream(new FileOutputStream(extractFile));
        copyFile(zin, os);
        os.flush();
        os.close();
    }
}

From source file:com.wso2telco.util.FileUtil.java

/**
 * Copy.//from w  w  w .  j ava 2s .c o  m
 *
 * @param src the src
 * @param dst the dst
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void copy(String src, String dst) throws IOException {

    String fileName = src.substring(src.lastIndexOf("/") + 1);

    File fsrc = new File(src);
    File fdst = new File(dst + "/" + fileName);

    InputStream in = new FileInputStream(fsrc);
    OutputStream out = new FileOutputStream(fdst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

From source file:com.simiacryptus.util.io.IOUtil.java

/**
 * Write kryo.//  w ww .  j  a v a 2  s .  co m
 *
 * @param <T>  the type parameter
 * @param obj  the obj
 * @param file the file
 */
public static <T> void writeKryo(T obj, OutputStream file) {
    try {
        Output output = new Output(buffer.get());
        new KryoReflectionFactorySupport().writeClassAndObject(output, obj);
        output.close();
        IOUtils.write(CompressionUtil.encodeBZ(Arrays.copyOf(output.getBuffer(), output.position())), file);
        file.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.dian1.player.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(Dian1Application.TAG, "File exists - nothing to do");
        return;//from w  w w .j  av a2  s .c o m
    }

    String albumUrl = mPlaylistEntry.getAlbum().getImage();
    if (albumUrl == null) {
        Log.w(Dian1Application.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(Dian1Application.TAG, "download Cover IOException");
            e.printStackTrace();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        Log.v(Dian1Application.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(Dian1Application.TAG, "Album cover saved to sd");

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

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

    }
}

From source file:arena.utils.FileUtils.java

public static void writeArrayToFile(byte data[], File outFile) throws IOException {
    OutputStream out = null;
    try {/* ww w. j av  a  2s.  com*/
        outFile.getParentFile().mkdirs();
        out = new FileOutputStream(outFile);
        out.write(data);
        out.close();
    } finally {
        if (out != null) {
            out.close();
        }
    }
}

From source file:com.wisdombud.right.client.common.HttpKit.java

/**
 * Send POST request//from   ww w  . j ava 2s  .c  o  m
 */
public static String post(String url, Map<String, String> queryParas, String data,
        Map<String, String> headers) {
    HttpURLConnection conn = null;
    try {
        conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), POST, headers);
        conn.connect();

        final OutputStream out = conn.getOutputStream();
        out.write(data != null ? data.getBytes(CHARSET) : null);
        out.flush();
        out.close();

        return readResponseString(conn);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:eu.swiec.bearballin.common.io.FileIO.java

public static void copy(File input, File output) throws IOException {
    InputStream inStream;/*  w  w  w  .  j  a v a2s. c om*/

    inStream = new FileInputStream(input);

    OutputStream outStrem = new FileOutputStream(output);

    byte[] buf = new byte[1024];
    int len;

    while ((len = inStream.read(buf)) > 0) {
        outStrem.write(buf, 0, len);
    }

    inStream.close();
    outStrem.close();
}

From source file:Main.java

public static File copy1(Context context, String filename, String destfilename, ProgressDialog pd) {

    try {//from w ww  .ja v  a2  s. c om
        InputStream in = context.getAssets().open(filename);
        int max = in.available();
        if (pd != null) {
            pd.setMax(max);
        }

        File file = new File(destfilename);
        OutputStream out = new FileOutputStream(file);
        byte[] byt = new byte[1024];
        int len = 0;
        int total = 0;
        while ((len = in.read(byt)) != -1) {
            out.write(byt, 0, len);
            total += len;
            if (pd != null) {
                pd.setProgress(total);
            }
        }
        out.flush();
        out.close();
        in.close();

        return file;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.aoyetech.fee.commons.utils.FileUtils.java

public static void writeStringToFile(final File file, final String data, final Charset encoding,
        final boolean append) throws IOException {
    OutputStream out = null;
    try {//w w  w . j  a va2  s  .  co m
        out = openOutputStream(file, append);
        IOUtils.write(data, out, encoding);
        out.close(); // don't swallow close Exception if copy completes normally
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:maspack.fileutil.SafeFileUtils.java

public static boolean closeQuietly(OutputStream stream) {
    try {/*from www. j av  a 2 s  .com*/
        stream.close();
    } catch (Exception e) {
        return false;
    }
    return true;
}