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.qpark.eip.core.spring.security.https.HttpsRequester.java

private static String read(final URL url, final String httpAuthBase64, final HostnameVerifier hostnameVerifier)
        throws IOException {
    StringBuffer sb = new StringBuffer(1024);
    HttpURLConnection connection = null;
    connection = (HttpURLConnection) url.openConnection();
    if (url.getProtocol().equalsIgnoreCase("https")) {
        connection = (HttpsURLConnection) url.openConnection();
        ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier);
    }//from  ww  w  .j  a va2  s  .co m
    if (httpAuthBase64 != null) {
        connection.setRequestProperty("Authorization", new StringBuffer(httpAuthBase64.length() + 6)
                .append("Basic ").append(httpAuthBase64).toString());
    }
    connection.setRequestProperty("Content-Type", "text/plain; charset=\"utf8\"");
    connection.setRequestMethod("GET");

    int returnCode = connection.getResponseCode();
    InputStream connectionIn = null;
    if (returnCode == 200) {
        connectionIn = connection.getInputStream();
    } else {
        connectionIn = connection.getErrorStream();
    }
    BufferedReader buffer = new BufferedReader(new InputStreamReader(connectionIn));
    String inputLine;
    while ((inputLine = buffer.readLine()) != null) {
        sb.append(inputLine);
    }
    buffer.close();

    return sb.toString();
}

From source file:eu.hansolo.tilesfx.weather.Sun.java

public static ZonedDateTime[] getSunriseSunsetAt(final double LATITUDE, final double LONGITUDE,
        final ZoneId ZONE_ID) {
    StringBuilder response = new StringBuilder();
    try {/*from   www  .  j  av a2s.  c o  m*/
        final String URL_STRING = new StringBuilder(SUNRISE_SUNSET_URL).append("lat=").append(LATITUDE)
                .append("&lng=").append(LONGITUDE).append("&date=today&formatted=0").toString();
        final HttpURLConnection CONNECTION = (HttpURLConnection) new URL(URL_STRING).openConnection();
        final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream()));
        String inputLine;
        while ((inputLine = IN.readLine()) != null) {
            response.append(inputLine).append("\n");
        }
        IN.close();
        return parseJsonData(response.toString(), ZONE_ID);
    } catch (IOException ex) {
        return null;
    }
}

From source file:com.calclab.emite.core.client.services.android.AndroidConnector.java

private static String readInputStream(InputStream stream) {
    try {/*w w w. j a  v a2 s . co  m*/
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line).append("\n");
        }

        br.close();
        return sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.codeskraps.lolo.misc.Utils.java

public static LOLO getLolo() throws UnsupportedEncodingException, ClientProtocolException, IOException,
        IllegalArgumentException, NullPointerException, JSONException {
    long startTime = System.currentTimeMillis();
    if (BuildConfig.DEBUG)
        Log.d(TAG, "download begining");
    if (BuildConfig.DEBUG)
        Log.d(TAG, "download url:" + Constants.LOLO_URL);

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);

    HttpClient client = new DefaultHttpClient(httpParameters);
    HttpGet request = new HttpGet(Constants.LOLO_URL);
    HttpResponse response = client.execute(request);

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    String json = reader.readLine();
    Log.d(TAG, "json: " + json);
    reader.close();

    JSONTokener tokener = new JSONTokener(json);

    JSONObject finalResult = new JSONObject(tokener);
    LOLO lolo = finalResult.getBoolean("open") ? LOLO.ON : LOLO.OFF;

    if (BuildConfig.DEBUG)
        Log.d(TAG, "lolo: " + lolo);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "download ready in " + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
    return lolo;//w  w  w  . j a v  a  2  s. c o m
}

From source file:Main.java

public static List<String> abreArquivo(String nomeArq) {

    List<String> linhas = new ArrayList<String>();
    String linha = "";
    try {// w  w w  .java2 s. c om

        BufferedReader txtBuffer = new BufferedReader(
                new InputStreamReader(new FileInputStream(nomeArq), codificacao));

        linha = txtBuffer.readLine();
        while (linha != null) {
            linhas.add(linha);
            linha = txtBuffer.readLine();
        }
        txtBuffer.close();
    } catch (Exception e) {
        System.out.println("[ERROR] Erro ao abrir arquivo: " + nomeArq);
        System.exit(-1);
    }
    return linhas;

}

From source file:fr.julienvermet.bugdroid.util.NetworkUtils.java

public static NetworkResult readJson(String url) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    int statusCode = 0;
    try {/* w w w  .j av  a2s  .  co m*/
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            reader.close();
        } else {
            Log.e(NetworkUtils.class.toString(), "Failed to download Json content");
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

    }

    return new NetworkResult(statusCode, builder.toString());
}

From source file:Main.java

public static void loadXPATHConf() {
    xpath_expression = new String[xpath_size];
    BufferedReader br;
    try {//  w w  w. ja  va 2  s.co m
        br = new BufferedReader(new FileReader(xpathconf_path));

        String line = "";
        int counter = 0;
        while (((line = br.readLine()) != null) && counter < xpath_size) {
            xpath_expression[counter] = line;
            counter++;
        }
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.spring.resource.FileSourceExample.java

public static String read(InputStream inputStream) {
    StringBuilder sb = new StringBuilder();
    try {/*w  w w .j  a  v a 2  s .c  o  m*/
        // ?
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
        try {
            String s;
            // ?
            while ((s = in.readLine()) != null) {
                sb.append(s);
                sb.append("\n");
            }
        } finally {
            in.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return sb.toString();
}

From source file:ota.otaupdates.utils.Utils.java

/**
 * Credit goes to Matthew Booth (http://www.github.com/MatthewBooth) for this function
 * @param propName The prop to be checked
 * @return String The value of the prop//  www . j  a v  a 2 s  . co m
 */
public static String getProp(String propName) {
    Process p = null;
    String result = "";
    try {
        p = new ProcessBuilder("/system/bin/getprop", propName).redirectErrorStream(true).start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while ((line = br.readLine()) != null) {
            result = line;
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.tmobile.TMobileParsing.java

protected static String getDataFromUrl(String url, String basicAuthToken) throws IOException {
    URL urlObj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) urlObj.openConnection();
    con.setRequestProperty("Authorization", "Basic " + basicAuthToken);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String userInfo = in.readLine(); //Successfull invocation of the web service will see JSON in the HTTP response body
    in.close();

    return userInfo;
}