Example usage for java.io BufferedReader close

List of usage examples for java.io BufferedReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:org.wso2.carbon.ml.MLTestUtils.java

public static String getJsonArrayAsString(CloseableHttpResponse response) throws IOException, JSONException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent()));
    JSONArray responseJson = new JSONArray(bufferedReader.readLine());
    bufferedReader.close();//from  ww w.j  av a  2 s.co  m
    response.close();

    return responseJson.toString();
}

From source file:Main.java

public static String getString(InputStream inputStream) {
    BufferedReader bufferedReader;
    StringBuffer strBuffer = new StringBuffer();
    try {//from  w w  w  . j a  v  a 2 s .  c om
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            strBuffer.append(line);
            strBuffer.append("\n");
        }
        bufferedReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return strBuffer.toString();
}

From source file:com.ct855.util.HttpsClientUtil.java

private static String print_content(HttpsURLConnection con) {
    if (con != null) {

        try {//from w w  w  . j  av  a2  s  . c  o m

            System.out.println("****** Content of the URL ********");
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String input;

            while ((input = br.readLine()) != null) {
                System.out.println(input);
            }
            br.close();
            return input;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return "";
}

From source file:com.l2jfree.gameserver.util.DatabaseBackupManager.java

public static void makeBackup() {
    File f = new File(Config.DATAPACK_ROOT, Config.DATABASE_BACKUP_SAVE_PATH);
    if (!f.mkdirs() && !f.exists()) {
        _log.warn("Could not create folder " + f.getAbsolutePath());
        return;//from  w  w  w .  j a va  2s .c  o m
    }

    _log.info("DatabaseBackupManager: backing up `" + Config.DATABASE_BACKUP_DATABASE_NAME + "`...");

    Process run = null;
    try {
        run = Runtime.getRuntime().exec("mysqldump" + " --user=" + Config.DATABASE_LOGIN + " --password="
                + Config.DATABASE_PASSWORD
                + " --compact --complete-insert --default-character-set=utf8 --extended-insert --lock-tables --quick --skip-triggers "
                + Config.DATABASE_BACKUP_DATABASE_NAME, null, new File(Config.DATABASE_BACKUP_MYSQLDUMP_PATH));
    } catch (Exception e) {
    } finally {
        if (run == null) {
            _log.warn("Could not execute mysqldump!");
            return;
        }
    }

    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        Date time = new Date();

        File bf = new File(f, sdf.format(time) + (Config.DATABASE_BACKUP_COMPRESSION ? ".zip" : ".sql"));
        if (!bf.createNewFile())
            throw new IOException("Cannot create backup file: " + bf.getCanonicalPath());
        InputStream input = run.getInputStream();
        OutputStream out = new FileOutputStream(bf);
        if (Config.DATABASE_BACKUP_COMPRESSION) {
            ZipOutputStream dflt = new ZipOutputStream(out);
            dflt.setMethod(ZipOutputStream.DEFLATED);
            dflt.setLevel(Deflater.BEST_COMPRESSION);
            dflt.setComment("L2JFree Schema Backup Utility\r\n\r\nBackup date: "
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(time));
            dflt.putNextEntry(new ZipEntry(Config.DATABASE_BACKUP_DATABASE_NAME + ".sql"));
            out = dflt;
        }

        byte[] buf = new byte[4096];
        int written = 0;
        for (int read; (read = input.read(buf)) != -1;) {
            out.write(buf, 0, read);

            written += read;
        }
        input.close();
        out.close();

        if (written == 0) {
            bf.delete();
            BufferedReader br = new BufferedReader(new InputStreamReader(run.getErrorStream()));
            String line;
            while ((line = br.readLine()) != null)
                _log.warn("DatabaseBackupManager: " + line);
            br.close();
        } else
            _log.info("DatabaseBackupManager: Schema `" + Config.DATABASE_BACKUP_DATABASE_NAME
                    + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000
                    + " s.");

        run.waitFor();
    } catch (Exception e) {
        _log.warn("DatabaseBackupManager: Could not make backup: ", e);
    }
}

From source file:Main.java

public static List<String> readFileToList(String filePath, String charsetName) {
    File file = new File(filePath);
    List<String> fileContent = new ArrayList<String>();
    if (!file.isFile()) {
        return null;
    }/*from w  w w.  j a  va2 s.c o m*/

    BufferedReader reader = null;
    try {
        InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
        reader = new BufferedReader(is);
        String line = null;
        while ((line = reader.readLine()) != null) {
            fileContent.add(line);
        }
        reader.close();
        return fileContent;
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}

From source file:org.androidnerds.reader.util.api.Authentication.java

/**
 * This method generates a quick token to send with API requests that require
 * editing content. This method is called as the API request is being built so
 * that it doesn't expire prior to the actual execution.
 *
 * @param sid - the user's authentication token from ClientLogin
 * @return token - the edit token generated by the server.
 *
 *///from  w w  w  .j a  v  a  2  s  .  co  m
public static String generateFastToken(String sid) {
    try {
        BasicClientCookie cookie = Authentication.buildCookie(sid);
        DefaultHttpClient client = new DefaultHttpClient();

        client.getCookieStore().addCookie(cookie);

        HttpGet get = new HttpGet(TOKEN_URL);
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();

        Log.d(TAG, "Server Response: " + response.getStatusLine());

        InputStream in = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;

        while ((line = reader.readLine()) != null) {
            Log.d(TAG, "Response Content: " + line);
        }

        reader.close();
        client.getConnectionManager().shutdown();

        return line;
    } catch (Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:de.dakror.scpuller.SCPuller.java

public static void loadDownloadedSongs() {
    File f = new File(System.getProperty("user.home") + "/.dakror/SCPuller/downloaded.txt");
    if (!f.exists())
        return;/*from w w  w .j av  a 2  s .  com*/
    else {
        try {
            downloadedSongs.clear();
            BufferedReader br = new BufferedReader(new FileReader(f));
            String line = "";
            while ((line = br.readLine()) != null)
                downloadedSongs.add(Integer.parseInt(line));

            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:uk.ac.ebi.eva.test.utils.JobTestUtils.java

/**
 * counts non-comment lines in an InputStream
 */// w  ww.  j a v  a  2  s . c o m
public static long getLines(InputStream in) throws IOException {
    BufferedReader file = new BufferedReader(new InputStreamReader(in));
    long lines = 0;
    String line;
    while ((line = file.readLine()) != null) {
        if (line.charAt(0) != '#') {
            lines++;
        }
    }
    file.close();
    return lines;
}

From source file:com.cisco.dbds.utils.configfilehandler.ConfigFileHandler.java

/**
 * Load jar cong file.//from www.  ja  v a 2 s.c  o m
 *
 * @param Utilclass the utilclass
 */
public static void loadJarCongFile(Class Utilclass) {
    try {
        String path = Utilclass.getResource("").getPath();
        path = path.substring(6, path.length() - 1);
        path = path.split("!")[0];
        System.out.println(path);
        JarFile jarFile = new JarFile(path);

        final Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (entry.getName().contains(".properties")) {
                System.out.println("Jar File Property File: " + entry.getName());
                JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
                InputStream input = jarFile.getInputStream(fileEntry);
                setSystemvariable(input);
                InputStreamReader isr = new InputStreamReader(input);
                BufferedReader reader = new BufferedReader(isr);
                String line;

                while ((line = reader.readLine()) != null) {
                    System.out.println("Jar file" + line);
                }
                reader.close();
            }
        }
        jarFile.close();
    } catch (Exception e) {
        System.out.println("Jar file reading Error");
    }
}

From source file:com.taobao.android.builder.tools.ideaplugin.ApDownloader.java

public static File downloadAP(String mtlConfigUrl, File root) throws Exception {

    Pattern p = Pattern.compile("buildConfigId=(\\d+)");
    Matcher m = p.matcher(mtlConfigUrl);

    String configId = "";

    if (m.find()) {
        configId = m.group(1);//from w ww  .j  ava  2  s. com
    }

    String apiUrl = "http://" + AtlasBuildContext.sBuilderAdapter.tpatchHistoryUrl
            + "/rpc/androidPlugin/getAp.json?buildConfigId=" + configId;

    URL api = new URL(apiUrl);
    BufferedReader in = new BufferedReader(new InputStreamReader(api.openStream()));

    String inputLine = in.readLine();
    in.close();

    String downloadUrl = inputLine.trim().replace("\"", "").replace("\\", "");

    File file = new File(root, MD5Util.getMD5(downloadUrl) + ".ap");
    if (file.exists()) {
        return file;
    }

    URL downloadApi = new URL(downloadUrl);
    System.out.println("start to download ap from " + downloadUrl);

    File tmpFile = new File(file.getParentFile(), String.valueOf(System.currentTimeMillis()));

    FileUtils.copyURLToFile(downloadApi, file);
    return file;

}