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:com.termmed.statistics.runner.Runner.java

/**
 * Gets the old date.//from  ww  w.j a v  a2s .co  m
 *
 * @param fileName the file name
 * @return the old date
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static String getOldDate(String fileName) throws IOException {

    if (!dataFolder.exists()) {
        dataFolder.mkdirs();
    }
    File file = new File(dataFolder, fileName + ".dat");
    if (file.exists()) {
        FileInputStream rfis = new FileInputStream(file);
        InputStreamReader risr = new InputStreamReader(rfis, "UTF-8");
        BufferedReader rbr = new BufferedReader(risr);
        String ret = rbr.readLine();
        rbr.close();
        rbr = null;
        return ret;
    }
    return "";
}

From source file:com.grarak.romswitcher.Utils.Utils.java

public static String readLine(String filename) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
    try {//from  w  w w.j  a va2  s  . co m
        return reader.readLine();
    } finally {
        reader.close();
    }
}

From source file:com.quietlycoding.android.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.
 * //  www.  ja  va2  s  .  c  o  m
 * @param sid
 *            - the user's authentication token from ClientLogin
 * @return token - the edit token generated by the server.
 * 
 */
public static String generateFastToken(String sid) {
    try {
        final BasicClientCookie cookie = Authentication.buildCookie(sid);
        final DefaultHttpClient client = new DefaultHttpClient();

        client.getCookieStore().addCookie(cookie);

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

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

        final InputStream in = entity.getContent();
        final 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 (final Exception e) {
        Log.d(TAG, "Exception caught:: " + e.toString());
        return null;
    }
}

From source file:de.forsthaus.backend.util.IpLocator.java

/**
 * Gets the content for a given HttpURLConnection.
 * /*w w  w  . j  a  va2  s .c  o  m*/
 * @param connection
 *            Http URL Connection.
 * @return HTML response
 * @exception IOException
 */

private static List<String> getContent(HttpURLConnection connection) throws IOException {
    final InputStream in = connection.getInputStream();
    try {
        final List<String> result = new ArrayList<String>(5);
        final InputStreamReader isr = new InputStreamReader(in);
        final BufferedReader bufRead = new BufferedReader(isr);

        String aLine = null;
        while (null != (aLine = bufRead.readLine())) {
            result.add(aLine.trim());
        }

        bufRead.close();
        return result;
    } finally {
        try {
            in.close();
        } catch (final IOException e) {
        }
    }
}

From source file:ro.fortsoft.matilda.util.RecaptchaUtils.java

public static boolean verify(String recaptchaResponse, String secret) {
    if (StringUtils.isNullOrEmpty(recaptchaResponse)) {
        return false;
    }//from   w ww.  j  ava2s  .  c o  m

    boolean result = false;
    try {
        URL url = new URL("https://www.google.com/recaptcha/api/siteverify");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        // add request header
        connection.setRequestMethod("POST");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String postParams = "secret=" + secret + "&response=" + recaptchaResponse;
        //            log.debug("Post parameters '{}'", postParams);

        // send post request
        connection.setDoOutput(true);
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(postParams);
        outputStream.flush();
        outputStream.close();

        int responseCode = connection.getResponseCode();
        log.debug("Response code '{}'", responseCode);

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();

        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // print result
        log.debug("Response '{}'", response.toString());

        // parse JSON response and return 'success' value
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(response.toString());
        JsonNode nameNode = rootNode.path("success");

        result = nameNode.asBoolean();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return result;
}

From source file:com.pkrete.xrd4j.rest.util.ClientUtil.java

/**
 * Extracts the response string from the given HttpEntity.
 * @param entity HttpEntity that contains the response
 * @return response String/*w  ww  . java2  s  .  c  om*/
 */
public static String getResponseString(HttpEntity entity) {
    StringBuilder builder = new StringBuilder();
    if (entity != null) {
        String inputLine;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(entity.getContent()));
            while ((inputLine = in.readLine()) != null) {
                builder.append(inputLine);
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }
    return builder.toString();
}

From source file:de.akquinet.android.androlog.reporter.PostReporter.java

/**
 * Reads an input stream and returns the result as a String.
 *
 * @param in/*from   w  ww.  j  av a  2s . c o  m*/
 *            the input stream
 * @return the read String
 * @throws IOException
 *             if the stream cannot be read.
 */
public static String read(InputStream in) throws IOException {
    InputStreamReader isReader = new InputStreamReader(in, "UTF-8");
    BufferedReader reader = new BufferedReader(isReader);
    StringBuffer out = new StringBuffer();
    char[] c = new char[4096];
    for (int n; (n = reader.read(c)) != -1;) {
        out.append(new String(c, 0, n));
    }
    reader.close();

    return out.toString();
}

From source file:cc.gospy.core.util.StringHelper.java

public static String getMyExternalIp() {
    if (myExternalIp == null) {
        try {/*w  w  w  . ja v  a2s  . co m*/
            logger.info("Querying external ip...");
            FutureTask futureTask = new FutureTask<>(() -> {
                URL ipEcho = new URL("http://ipecho.net/plain");
                BufferedReader in = new BufferedReader(new InputStreamReader(ipEcho.openStream()));
                String resultIp = in.readLine();
                in.close();
                return resultIp;
            });
            futureTask.run();
            myExternalIp = (String) futureTask.get(3, TimeUnit.SECONDS);
            logger.info("My external ip: {}", myExternalIp);
        } catch (TimeoutException e) {
            myExternalIp = "unknown ip";
            logger.error("Get external ip failure, cause: Timeout (3 seconds)");
        } catch (Exception e) {
            myExternalIp = "unknown ip";
            logger.error("Get external ip failure, cause: {}", e.getMessage());
        }
    }
    return myExternalIp;
}

From source file:Main.java

public static List<String> getFileLines(final File file) throws IOException {
    final InputStream inputStream = new FileInputStream(file);
    final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    final List<String> lines = new ArrayList<String>();

    boolean done = false;

    while (!done) {
        final String line = reader.readLine();
        done = (line == null);/*from  w w  w  .  j  a va  2  s .  c  om*/

        if (line != null) {
            lines.add(line);
        }
    }

    reader.close();
    inputStream.close();

    return lines;
}

From source file:com.bmwcarit.barefoot.roadmap.Loader.java

/**
 * Reads road type configuration from file.
 *
 * @param path Path of the road type configuration file.
 * @return Mapping of road class identifiers to priority factor and default maximum speed.
 * @throws JSONException thrown on JSON extraction or parsing error.
 * @throws IOException thrown on file reading error.
 *///from   www . j  av a 2s  .  c  o  m
public static Map<Short, Tuple<Double, Integer>> read(String path) throws JSONException, IOException {
    BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

    String line = null, json = new String();
    while ((line = file.readLine()) != null) {
        json += line;
    }
    file.close();

    return roadtypes(new JSONObject(json));
}