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:software.uncharted.util.HTTPUtil.java

public static String get(String url) throws IOException {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;//from w w w.java  2  s  .  c om
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    return response.toString();
}

From source file:cc.vileda.sipgatesync.api.SipgateApi.java

@NonNull
private static String getUrl(final String apiUrl, final String token) throws IOException {
    final HttpURLConnection urlConnection = getConnection(apiUrl);
    urlConnection.setDoInput(true);//from w  ww.  jav  a  2 s. c  o  m
    urlConnection.setRequestMethod("GET");
    if (token != null) {
        urlConnection.setRequestProperty("Authorization", "Bearer " + token);
    }
    StringBuilder sb = new StringBuilder();
    int HttpResult = urlConnection.getResponseCode();
    if (HttpResult == HttpURLConnection.HTTP_OK) {
        BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line).append("\n");
        }
        br.close();

        return sb.toString();
    } else {
        System.out.println(urlConnection.getResponseMessage());
    }

    return "";
}

From source file:com.moss.posixfifosockets.PosixFifoSocket.java

private static String read(InputStream in) throws IOException {
    StringBuilder text = new StringBuilder();
    char[] b = new char[1024];

    BufferedReader r = new BufferedReader(new InputStreamReader(in));

    for (int x = r.read(b); x != -1; x = r.read(b)) {
        text.append(b, 0, x);//from w w w .j av  a2  s .  c  o m
    }

    r.close();
    return text.toString();
}

From source file:Main.java

public static String readXmlAsString(final File input) throws IOException {
    String xmlString = "";

    if (input == null) {
        throw new IOException("The input stream object is null.");
    }//from   www.  j a v  a2  s.  c o m

    final FileInputStream fileInputStream = new FileInputStream(input);
    final InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
    final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String line = bufferedReader.readLine();
    while (line != null) {
        xmlString += line + "\n";
        line = bufferedReader.readLine();
    }
    fileInputStream.close();
    fileInputStream.close();
    bufferedReader.close();

    return xmlString;
}

From source file:Main.java

public static String readFile(File file) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line = null;/*from   w  w  w .j  a  v a2  s  .  c om*/
    StringBuilder stringBuilder = new StringBuilder();
    String ls = System.getProperty("line.separator");

    while ((line = reader.readLine()) != null) {
        stringBuilder.append(line);
        stringBuilder.append(ls);
    }
    reader.close();
    return stringBuilder.toString();
}

From source file:Util.java

/**
 * Reads file contents, returning it as a String, joining lines with provided
 * separator./* w  w  w. j  ava 2  s . c  om*/
 */
public static String stringFromFile(File file, String joinWith) throws IOException {
    StringBuffer buf = new StringBuffer();
    BufferedReader in = new BufferedReader(new FileReader(file));

    try {
        String line = null;
        while ((line = in.readLine()) != null) {
            buf.append(line).append(joinWith);
        }
    } finally {
        in.close();
    }
    return buf.toString();
}

From source file:com.beginner.core.utils.SmsUtil.java

public static String SMS(String postData, String postUrl) {
    try {// ww w  .j a va 2s .  c o m
        //??POST
        URL url = new URL(postUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setUseCaches(false);
        conn.setDoOutput(true);

        conn.setRequestProperty("Content-Length", "" + postData.length());
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
        out.write(postData);
        out.flush();
        out.close();

        //???
        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            System.out.println("connect failed!");
            return "";
        }
        //??
        String line, result = "";
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        while ((line = in.readLine()) != null) {
            result += line + "\n";
        }
        in.close();
        return result;
    } catch (IOException e) {
        e.printStackTrace(System.out);
    }
    return "";
}

From source file:com.yahoo.storm.yarn.TestIntegration.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@BeforeClass//from  w w  w  .j a  va  2 s. co m
public static void setup() {
    try {
        zkServer = new EmbeddedZKServer();
        zkServer.start();

        LOG.info("Starting up MiniYARN cluster");
        if (yarnCluster == null) {
            yarnCluster = new MiniYARNCluster(TestIntegration.class.getName(), 2, 1, 1);
            Configuration conf = new YarnConfiguration();
            conf.setInt(YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_MB, 512);
            conf.setInt(YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, 2 * 1024);
            yarnCluster.init(conf);
            yarnCluster.start();
        }
        sleep(2000);

        Configuration miniyarn_conf = yarnCluster.getConfig();
        yarn_site_xml = testConf.createYarnSiteConfig(miniyarn_conf);

        storm_home = testConf.stormHomePath();
        LOG.info("Will be using storm found on PATH at " + storm_home);

        //create a storm configuration file with zkport 
        final Map storm_conf = Config.readStormConfig();
        storm_conf.put(backtype.storm.Config.STORM_ZOOKEEPER_PORT, zkServer.port());
        storm_conf_file = testConf.createConfigFile(storm_conf);

        List<String> cmd = java.util.Arrays.asList("bin/storm-yarn", "launch", storm_conf_file.toString(),
                "--stormZip", "lib/storm.zip", "--appname", "storm-on-yarn-test", "--output",
                "target/appId.txt");
        execute(cmd);

        //wait for Storm cluster to be fully luanched
        sleep(15000);

        BufferedReader reader = new BufferedReader(new FileReader("target/appId.txt"));
        appId = reader.readLine();
        reader.close();
        if (appId != null)
            appId = appId.trim();
        LOG.info("application ID:" + appId);
    } catch (Exception ex) {
        LOG.error("setup failure", ex);
        Assert.assertEquals(null, ex);
    }
}

From source file:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java

/**
 * Executes BSH scripts/*from   w w  w  .jav  a 2s  .c o  m*/
 *
 * @param context    the web context to use for finding the file resource
 * @param db         the database connection to use for executing the script against
 * @param scriptName The resource filename
 * @throws Exception Description of the Exception
 */
private static void upgradeBSH(ServletContext context, Connection db, String scriptName) throws Exception {
    LOG.info("Executing BeanShell script " + scriptName);
    // Prepare bean shell script, if needed
    Interpreter script = new Interpreter();
    script.set("db", db);
    // Add the ApplicationPrefs...
    ApplicationPrefs prefs = (ApplicationPrefs) context.getAttribute(Constants.APPLICATION_PREFS);
    script.set("prefs", prefs);
    // Read the script
    String pathString = scriptName.substring(0, 4);
    String setupPath = "/WEB-INF/database/common/" + pathString + "/";
    InputStream source = context.getResourceAsStream(setupPath + scriptName);
    BufferedReader in = new BufferedReader(new InputStreamReader(source));
    // Execute the script
    script.eval(in);
    in.close();
}

From source file:jp.gr.java_conf.schkit.utils.ApplicationInfo.java

public static String ResourceAsString(InputStream is) {

    StringBuilder sb = new StringBuilder();
    try {//w w  w  .  ja  va 2  s  .  c  o m
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String line;

        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

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