Example usage for java.io BufferedOutputStream close

List of usage examples for java.io BufferedOutputStream close

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

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

Usage

From source file:CB_Core.Api.PocketQuery.java

/**
 * @param AccessToken/*from ww w  .j  a  v  a2 s .co  m*/
 *            Config.GetAccessToken(true)
 * @param pocketQueryConfig
 *            Config.settings.PocketQueryFolder.getValue()
 * @param PqFolder
 * @return
 */
public static int DownloadSinglePocketQuery(PQ pocketQuery, String PqFolder) {
    HttpGet httpGet = new HttpGet(
            GroundspeakAPI.GS_LIVE_URL + "GetPocketQueryZippedFile?format=json&AccessToken="
                    + GroundspeakAPI.GetAccessToken(true) + "&PocketQueryGuid=" + pocketQuery.GUID);

    try {
        // String result = GroundspeakAPI.Execute(httpGet);
        httpGet.setHeader("Accept", "application/json");
        httpGet.setHeader("Content-type", "application/json");

        // Execute HTTP Post Request
        String result = "";
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httpGet);

        int buffLen = 32 * 1024;
        byte[] buff = new byte[buffLen];
        InputStream inputStream = response.getEntity().getContent();
        int buffCount = inputStream.read(buff, 0, buffLen);
        int buffPos = 0;
        result = ""; // now read from the response until the ZIP Informations are beginning or to the end of stream
        for (int i = 0; i < buffCount; i++) {
            byte c = buff[i];
            result += (char) c;

            if (result.contains("\"ZippedFile\":\"")) { // The stream position represents the beginning of the ZIP block // to have a correct JSON Array we must add a "}} to the
                // result
                result += "\"}}";
                buffPos = i; // Position im Buffer, an der die ZIP-Infos beginnen
                break;
            }
        }

        //
        try
        // Parse JSON Result
        {
            JSONTokener tokener = new JSONTokener(result);
            JSONObject json = (JSONObject) tokener.nextValue();
            JSONObject status = json.getJSONObject("Status");
            if (status.getInt("StatusCode") == 0) {
                GroundspeakAPI.LastAPIError = "";
                SimpleDateFormat postFormater = new SimpleDateFormat("yyyyMMddHHmmss");
                String dateString = postFormater.format(pocketQuery.DateLastGenerated);
                String local = PqFolder + "/" + pocketQuery.Name + "_" + dateString + ".zip";

                // String test = json.getString("ZippedFile");

                FileOutputStream fs;
                fs = new FileOutputStream(local);
                BufferedOutputStream bfs = new BufferedOutputStream(fs);

                try {
                    // int firstZipPos = result.indexOf("\"ZippedFile\":\"") + 14;
                    // int lastZipPos = result.indexOf("\"", firstZipPos + 1) - 1;
                    CB_Utils.Converter.Base64.decodeStreamToStream(inputStream, buff, buffLen, buffCount,
                            buffPos, bfs);
                } catch (Exception ex) {
                }

                // fs.write(resultByte);
                bfs.flush();
                bfs.close();
                fs.close();

                result = null;
                System.gc();

                return 0;
            } else {
                GroundspeakAPI.LastAPIError = "";
                GroundspeakAPI.LastAPIError = "StatusCode = " + status.getInt("StatusCode") + "\n";
                GroundspeakAPI.LastAPIError += status.getString("StatusMessage") + "\n";
                GroundspeakAPI.LastAPIError += status.getString("ExceptionDetails");

                return (-1);
            }

        } catch (JSONException e) {

            e.printStackTrace();
        }
    } catch (ClientProtocolException e) {
        System.out.println(e.getMessage());
        return (-1);
    } catch (IOException e) {
        System.out.println(e.getMessage());
        return (-1);
    }

    return 0;

}

From source file:com.zlfun.framework.excel.ExcelUtils.java

public static <T> void save(String fileName, String sheetName, Class<T> clazz, List<T> list) {
    try {//  ww  w. j  av a 2 s.co  m
        //?
        BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(fileName));
        write(sheetName, clazz, list, os);
        os.flush();
        os.close();
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Main.java

public static void saveRgb2Bitmap(IntBuffer buf, String filePath, int width, int height) {
    final int[] pixelMirroredArray = new int[width * height];
    Log.d("TryOpenGL", "Creating " + filePath);
    BufferedOutputStream bos = null;
    try {//  w  w  w  .  ja v a2s. c  om
        int[] pixelArray = buf.array();
        // rotate 180 deg with x axis because y is reversed
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j];
            }
        }
        bos = new BufferedOutputStream(new FileOutputStream(filePath));
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bmp.copyPixelsFromBuffer(IntBuffer.wrap(pixelMirroredArray));
        bmp.compress(Bitmap.CompressFormat.JPEG, 90, bos);
        bmp.recycle();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static void copyDatabase2FileDir(Context context, String dbName) {
    InputStream stream = null;//  w ww  .  j  ava 2 s  .  co  m
    BufferedOutputStream outputStream = null;
    try {

        stream = context.getAssets().open(dbName);
        File file = new File(context.getFilesDir(), dbName);

        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = stream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
            outputStream.flush();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.nary.Debug.java

public static void saveClass(String _filename, Object _class, boolean _compress) {
    BufferedOutputStream FS = null;
    try {/*from  w ww  .  j a  v  a  2 s  .c  om*/
        FS = new BufferedOutputStream(
                cfEngine.thisPlatform.getFileIO().getFileOutputStream(new File(_filename)), 32000);
        saveClass(FS, _class, _compress);
    } catch (Throwable E) {
    } finally {
        try {
            if (FS != null)
                FS.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:marytts.util.io.FileUtils.java

private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;/*from  w  w  w  . j  av a  2s  .co  m*/
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }
}

From source file:com.headswilllol.basiclauncher.Launcher.java

public static void unzip(ZipFile zip, ZipEntry entry, File dest) { // convenience method for unzipping from archive
    if (dest.exists())
        dest.delete();//from  w  w  w  .j a  va 2  s  .c  o  m
    dest.getParentFile().mkdirs();
    try {
        BufferedInputStream bIs = new BufferedInputStream(zip.getInputStream(entry));
        int b;
        byte buffer[] = new byte[1024];
        FileOutputStream fOs = new FileOutputStream(dest);
        BufferedOutputStream bOs = new BufferedOutputStream(fOs, 1024);
        while ((b = bIs.read(buffer, 0, 1024)) != -1)
            bOs.write(buffer, 0, b);
        bOs.flush();
        bOs.close();
        bIs.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        createExceptionLog(ex);
        progress = "Failed to unzip " + entry.getName();
        fail = "Errors occurred; see log file for details";
        launcher.paintImmediately(0, 0, width, height);
    }
}

From source file:com.eviware.soapui.support.Tools.java

public static int copyFile(File source, File target, boolean overwrite) throws IOException {
    int bytes = 0;

    if (target.exists()) {
        if (overwrite) {
            target.delete();/*from   w  w  w  . j a  v a  2 s  . c  o  m*/
        } else {
            return -1;
        }
    } else {
        String path = target.getAbsolutePath();
        int ix = path.lastIndexOf(File.separatorChar);
        if (ix != -1) {
            path = path.substring(0, ix);
            File pathFile = new File(path);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
        }
    }

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target));

    int read = in.read(copyBuffer);
    while (read != -1) {
        if (read > 0) {
            out.write(copyBuffer, 0, read);
            bytes += read;
        }
        read = in.read(copyBuffer);
    }

    in.close();
    out.close();

    return bytes;
}

From source file:com.linkedin.pinot.common.utils.TarGzCompressionUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the
 * specified directory.//from   w w  w  .j  a  va2 s  . co m
 *
 * @param directoryPath
 *          The path to the directory to create an archive of
 * @param tarGzPath
 *          The path to the archive to create
 * @return tarGzPath
 * @throws IOException
 *           If anything goes wrong
 */
public static String createTarGzOfDirectory(String directoryPath, String tarGzPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    if (!tarGzPath.endsWith(TAR_GZ_FILE_EXTENTION)) {
        tarGzPath = tarGzPath + TAR_GZ_FILE_EXTENTION;
    }

    try {
        fOut = new FileOutputStream(new File(tarGzPath));
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        addFileToTarGz(tOut, directoryPath, "");
    } finally {
        tOut.finish();

        tOut.close();
        gzOut.close();
        bOut.close();
        fOut.close();
    }
    return tarGzPath;
}

From source file:fr.gael.dhus.server.http.TomcatServer.java

private static void expand(InputStream input, File file) throws IOException {
    BufferedOutputStream output = null;
    try {//from ww w . ja  v a 2 s  .  c  o  m
        output = new BufferedOutputStream(new FileOutputStream(file));
        byte buffer[] = new byte[2048];
        while (true) {
            int n = input.read(buffer);
            if (n <= 0) {
                break;
            }
            output.write(buffer, 0, n);
        }
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    }
}