Example usage for java.io InputStreamReader close

List of usage examples for java.io InputStreamReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:cc.aileron.commons.resource.ResourceImpl.java

@Override
public String toString() {
    try {//from  w  w w .  ja v  a  2s  . c om
        final InputStreamReader ir = new InputStreamReader(content.getInputStream());
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(new BufferedWriter(sw));
        final BufferedReader br = new BufferedReader(ir);

        String line = br.readLine();
        while (line != null) {
            pw.println(line);
            line = br.readLine();
        }
        br.close();
        ir.close();
        pw.flush();
        pw.close();
        return sw.toString();
    } catch (final Exception e) {
        return "";
    }
}

From source file:com.aliyun.oss.common.comm.HttpFactoryTest.java

private String readSting(InputStream input) {
    InputStreamReader reader = new InputStreamReader(input);
    BufferedReader br = new BufferedReader(reader);
    StringBuilder sb = new StringBuilder();
    String line = null;//  w  w w .  ja  va  2  s .c o  m
    try {
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    } catch (IOException e) {
        return null;
    } finally {
        try {
            br.close();
            reader.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.sds.acube.ndisc.xnapi.XNApiUtils.java

private static void readVersionFromFile() {
    XNApi_PublishingVersion = "<unknown>";
    XNApi_PublishingDate = "<unknown>";
    InputStreamReader isr = null;
    LineNumberReader lnr = null;/*from  w ww. ja  v a2s . co m*/
    try {
        isr = new InputStreamReader(
                XNApiUtils.class.getResourceAsStream("/com/sds/acube/ndisc/xnapi/version.txt"));
        if (isr != null) {
            lnr = new LineNumberReader(isr);
            String line = null;
            do {
                line = lnr.readLine();
                if (line != null) {
                    if (line.startsWith("Publishing-Version=")) {
                        XNApi_PublishingVersion = line.substring("Publishing-Version=".length(), line.length())
                                .trim();
                    } else if (line.startsWith("Publishing-Date=")) {
                        XNApi_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                .trim();
                    }
                }
            } while (line != null);
            lnr.close();
        }
    } catch (IOException ioe) {
        XNApi_PublishingVersion = "<unknown>";
        XNApi_PublishingDate = "<unknown>";
    } finally {
        try {
            if (lnr != null) {
                lnr.close();
            }
            if (isr != null) {
                isr.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:org.craftercms.cstudio.loadtesting.actions.AlfrescoWriteContent.java

/**
 * convert InputStream to string/*from   w  w  w .  jav a  2s.c  o  m*/
 * @param internalName 
 * 
 * @param is
 * @return string
 */
public String getFileContent(String baseFileName) throws Exception {
    InputStream is = null;
    BufferedReader bufferedReader = null;
    InputStreamReader inputReader = null;
    try {
        is = this.getClass().getResourceAsStream("/" + baseFileName);
        StringBuilder sb = new StringBuilder();
        String line = null;
        inputReader = new InputStreamReader(is);
        bufferedReader = new BufferedReader(inputReader);
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line + "\n");
        }
        return sb.toString();
    } finally {
        is.close();
        inputReader.close();
        bufferedReader.close();
    }
}

From source file:org.apache.solr.cloud.TestConfigSetsAPI.java

private NamedList getConfigSetPropertiesFromZk(SolrZkClient zkClient, String path) throws Exception {
    byte[] oldPropsData = null;
    try {//from w  ww.jav  a2  s .c  o m
        oldPropsData = zkClient.getData(path, null, null, true);
    } catch (KeeperException.NoNodeException e) {
        // okay, properties just don't exist
    }

    if (oldPropsData != null) {
        InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(oldPropsData),
                StandardCharsets.UTF_8);
        try {
            return ConfigSetProperties.readFromInputStream(reader);
        } finally {
            reader.close();
        }
    }
    return null;
}

From source file:org.apache.camel.dataformat.csv.CsvIteratorTest.java

@Test
public void closeIfError(@Injectable final InputStreamReader reader, @Injectable final CSVParser parser)
        throws IOException {
    new Expectations() {
        {/*www.  ja  v a2  s .  c o m*/
            parser.getLine();
            result = new String[] { "1" };

            parser.getLine();
            result = new String[] { "2" };

            parser.getLine();
            result = new IOException(HDD_CRASH);

            // The reader will be closed when there is nothing left
            reader.close();
        }
    };

    @SuppressWarnings("resource")
    CsvIterator<List<String>> iterator = new CsvIterator<List<String>>(parser, reader,
            CsvLineConverters.getListConverter());
    Assert.assertTrue(iterator.hasNext());
    Assert.assertEquals(Arrays.asList("1"), iterator.next());
    Assert.assertTrue(iterator.hasNext());

    try {
        iterator.next();
        Assert.fail("exception expected");
    } catch (IllegalStateException e) {
        Assert.assertEquals(HDD_CRASH, e.getCause().getMessage());
    }

    Assert.assertFalse(iterator.hasNext());

    try {
        iterator.next();
        Assert.fail("exception expected");
    } catch (NoSuchElementException e) {
        // okay
    }
}

From source file:com.omertron.thetvdbapi.tools.WebBrowser.java

/**
 * Request the web page at the specified URL
 *
 * @param url//ww  w  .  j  ava2  s  .  c om
 * @throws IOException
 */
public static String request(URL url) throws IOException {
    StringBuilder content = new StringBuilder();

    BufferedReader in = null;
    URLConnection cnx = null;
    InputStreamReader isr = null;
    GZIPInputStream zis = null;

    try {
        cnx = openProxiedConnection(url);

        sendHeader(cnx);
        readHeader(cnx);

        // Check the content encoding of the connection. Null content encoding is standard HTTP
        if (cnx.getContentEncoding() == null) {
            //in = new BufferedReader(new InputStreamReader(cnx.getInputStream(), getCharset(cnx)));
            isr = new InputStreamReader(cnx.getInputStream(), "UTF-8");
        } else if (cnx.getContentEncoding().equalsIgnoreCase("gzip")) {
            zis = new GZIPInputStream(cnx.getInputStream());
            isr = new InputStreamReader(zis, "UTF-8");
        } else {
            return "";
        }

        in = new BufferedReader(isr);

        String line;
        while ((line = in.readLine()) != null) {
            content.append(line);
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (zis != null) {
            try {
                zis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (cnx instanceof HttpURLConnection) {
            ((HttpURLConnection) cnx).disconnect();
        }
    }
    return content.toString();
}

From source file:ca.ualberta.cmput301f13t13.storyhoard.serverClasses.ESRetrieval.java

/**
 * Retrieves the content of the given HttpResponse and returns it as
 * a string. In this application the response content will be a story 
 * object in JSON string format. These strings are later to be converted 
 * to real story objects using JSON. </br></br>
 * /*from  w w  w  . j a v  a 2 s  .com*/
 * This method is only meant to be used by the searchById() and retrieve()
 * which are also defined in this class. It is a helper function for them.
 * </br></br>
 * 
 * In addition to returning JSON string, this method also prints out the 
 * JSON string to the logcat. 
 * 
 * @return string of the response
 */
private String getEntityContent(HttpResponse response) throws IOException {
    HttpEntity entity = response.getEntity();
    InputStreamReader is = new InputStreamReader(entity.getContent());
    BufferedReader br = new BufferedReader(is);
    String output;
    System.err.println("Output from ESUpdates -> ");
    String json = "";
    while ((output = br.readLine()) != null) {
        System.err.println(output);
        json += output;
    }
    System.err.println("JSON:" + json);
    entity.consumeContent();
    is.close();
    br.close();
    return json;
}

From source file:com.github.akinaru.hcidebugger.activity.BaseActivity.java

/**
 * Retrieve btsnoop file absolute path from bt_stack.conf file
 *
 * @return btsnoop file absolute path/*  w  w w.j  a  v  a 2 s. c o m*/
 */
protected String getHciLogFilePath() {

    try {
        FileInputStream fis = new FileInputStream(getResources().getString(R.string.bluetooth_config));
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bufferedReader = new BufferedReader(isr);
        String line;
        while ((line = bufferedReader.readLine()) != null) {

            if (line.contains(getResources().getString(R.string.bt_config_file_name_filter))) {
                if (line.indexOf("=") != -1) {
                    fis.close();
                    isr.close();
                    bufferedReader.close();
                    return line.substring(line.indexOf("=") + 1);
                }
            }
        }

        fis.close();
        isr.close();
        bufferedReader.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:com.mingsoft.util.proxy.Result.java

/**
 * ?gzip?/* w ww. ja v  a  2s. c om*/
 * 
 * @param charSet
 *            ?
 * @return InputStreamReade;
 */
public String getContentForGzip(String charset) {
    if (httpEntity.getContentEncoding().getValue().indexOf("gzip") > -1) {
        try {
            GZIPInputStream gzipis = new GZIPInputStream(httpEntity.getContent());

            InputStreamReader isr = new InputStreamReader(gzipis, charset); // ?????
            java.io.BufferedReader br = new java.io.BufferedReader(isr);
            String tempbf;
            StringBuffer sb = new StringBuffer();
            while ((tempbf = br.readLine()) != null) {
                sb.append(tempbf);
                sb.append("\r\n");
            }
            gzipis.close();
            isr.close();
            return sb.toString();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}