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:com.servoy.extensions.plugins.http.HttpProvider.java

public static Pair<String, String> getPageDataOldImpl(URL url, int timeout) {
    StringBuffer sb = new StringBuffer();
    String charset = null;/*  w ww  . ja  v  a  2  s .c  o  m*/
    try {
        URLConnection connection = url.openConnection();
        if (timeout >= 0)
            connection.setConnectTimeout(timeout);
        InputStream is = connection.getInputStream();
        final String type = connection.getContentType();
        if (type != null) {
            final String[] parts = type.split(";");
            for (int i = 1; i < parts.length && charset == null; i++) {
                final String t = parts[i].trim();
                final int index = t.toLowerCase().indexOf("charset=");
                if (index != -1)
                    charset = t.substring(index + 8);
            }
        }
        InputStreamReader isr = null;
        if (charset != null)
            isr = new InputStreamReader(is, charset);
        else
            isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        int read = 0;
        while ((read = br.read()) != -1) {
            sb.append((char) read);
        }
        br.close();
        isr.close();
        is.close();
    } catch (Exception e) {
        Debug.error(e);
    }
    return new Pair<String, String>(sb.toString(), charset);
}

From source file:com.sun.faces.generate.RenderKitSpecificationGenerator.java

public static void appendResourceToStringBuffer(String resourceName, StringBuffer sb) throws Exception {
    InputStreamReader isr = null;
    URL url = null;//from w w w  .ja v a  2  s  .c o  m
    URLConnection conn = null;
    char[] chars = new char[1024];
    int len = 0;

    url = getCurrentLoader(sb).getResource(resourceName);
    conn = url.openConnection();
    conn.setUseCaches(false);
    isr = new InputStreamReader(conn.getInputStream());
    while (-1 != (len = isr.read(chars, 0, 1024))) {
        sb.append(chars, 0, len);
    }
    isr.close();
}

From source file:com.metawiring.load.generators.ExtractGenerator.java

private CharBuffer loadFileData() {
    InputStream stream = null;/* ww w.j av a2s  .  co  m*/
    File onFileSystem = new File("data" + File.separator + fileName);

    if (onFileSystem.exists()) {
        try {
            stream = new FileInputStream(onFileSystem);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(
                    "Unable to find file " + onFileSystem.getPath() + " after verifying that it exists.");
        }
        logger.debug("Loaded file data from " + onFileSystem.getPath());
    }

    if (stream == null) {
        stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("data/" + fileName);
        logger.debug("Loaded file data from classpath resource " + fileName);
    }

    if (stream == null) {
        throw new RuntimeException(fileName + " was missing.");
    }

    CharBuffer image;
    try {
        InputStreamReader isr = new InputStreamReader(stream);
        image = CharBuffer.allocate(1024 * 1024);
        isr.read(image);
        isr.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
    image.flip();

    return image.asReadOnlyBuffer();

}

From source file:com.kaylerrenslow.armaDialogCreator.updater.tasks.AdcVersionCheckTask.java

@NotNull
private ReleaseInfo getLatestRelease() throws Exception {
    setStatusText("Updater.checking_for_updates");

    JSONParser parser = new JSONParser();
    URLConnection connection = new URL(versionCheckUrl).openConnection();
    InputStreamReader reader = new InputStreamReader(connection.getInputStream());

    JSONObject object = (JSONObject) parser.parse(reader);

    reader.close();
    connection.getInputStream().close();

    return new ReleaseInfo(object);
}

From source file:com.vmware.bdd.cli.commands.CommandsUtils.java

public static String dataFromFile(String filePath) throws IOException, FileNotFoundException {
    StringBuilder dataStringBuffer = new StringBuilder();
    FileInputStream fis = null;//  w w w . j  a va  2 s  .c  o  m
    InputStreamReader inputStreamReader = null;
    BufferedReader bufferedReader = null;
    try {
        fis = new FileInputStream(filePath);
        inputStreamReader = new InputStreamReader(fis, "UTF-8");
        bufferedReader = new BufferedReader(inputStreamReader);
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            dataStringBuffer.append(line);
            dataStringBuffer.append("\n");
        }
    } finally {
        if (fis != null) {
            fis.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (bufferedReader != null) {
            bufferedReader.close();
        }
    }
    return dataStringBuffer.toString();
}

From source file:org.zols.datastore.validator.TV4.java

/**
 * This method will load Javascript from class path
 *
 * @param path - path of Javascript file
 *///from  ww  w.  j  a va 2 s .co m
private void loadJavaScriptFile(String path) throws ScriptException {
    InputStream stream = TV4.class.getResourceAsStream(path);
    InputStreamReader reader = new InputStreamReader(stream);
    engine.eval(reader);
    try {
        stream.close();
        reader.close();
    } catch (IOException ex) {
        Logger.getLogger(TV4.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:clientserver.ServerThread.java

@Override
public void run() {
    try {//from w  ww. j  a v  a 2s  . c  o  m
        InputStreamReader is = new InputStreamReader(socket.getInputStream());
        br = new BufferedReader(is);
        while (receive()) {
        }
        System.out.println("Reading socket closing");
        is.close();
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.googlecode.xmlzen.XmlBuilderStreamOutputTest.java

@Test
public void testBuild() throws Exception {
    File myXml = File.createTempFile("xmlzentest", ".xml");
    OutputStream myXmlStream = new FileOutputStream(myXml);

    String xml = XmlBuilder.newXml(new XmlBuilderStreamOutput(myXmlStream, "UTF-8"), "UTF-8", false)
            .openTag("xml").withAttribute("id", 1).openTag("thisishow").withValue("you can build").closeTag()
            .openTag("your").withAttribute("xml", "nicely").withAttributeIf(1 < 0, "shouldnot", "happen")
            .toString(true);// w  w w.  j a  va  2s. com
    log.debug(xml);
    myXmlStream.close();
    InputStreamReader reader = new InputStreamReader(new FileInputStream(myXml), "UTF-8");
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    int c = 0;
    while ((c = reader.read()) != -1) {
        bout.write(c);
    }
    reader.close();
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            + "<xml id=\"1\"><thisishow>you can build</thisishow>" + "<your xml=\"nicely\"/></xml>",
            bout.toString("UTF-8"));
}

From source file:br.cefetrj.sagitarii.nunki.comm.WebClient.java

public String doGet(String action, String parameter) throws Exception {
    String result = "NO_ANSWER";
    String mhpHost = gf.getHostURL();
    String url = mhpHost + "/" + action + "?" + parameter;

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    /*//from   w  w  w .j a v  a2 s. c om
    if ( gf.useProxy() ) {
       if ( gf.getProxyInfo() != null ) {
    HttpHost httpproxy = new HttpHost( gf.getProxyInfo().getHost(), gf.getProxyInfo().getPort() ); 
            
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope( gf.getProxyInfo().getHost(), gf.getProxyInfo().getPort() ),
            new UsernamePasswordCredentials( gf.getProxyInfo().getUser(), gf.getProxyInfo().getPassword() ));
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpproxy);
                  
       }
    }
    */

    HttpGet getRequest = new HttpGet(url);
    getRequest.addHeader("accept", "application/json");
    getRequest.addHeader("Content-Type", "plain/text; charset=utf-8");
    getRequest.setHeader("User-Agent", "TaskManager Node");

    HttpResponse response = httpClient.execute(getRequest);
    response.setHeader("Content-Type", "plain/text; charset=UTF-8");

    int stCode = response.getStatusLine().getStatusCode();

    if (stCode != 200) {
        // Error
    } else {
        HttpEntity entity = response.getEntity();
        InputStreamReader isr = new InputStreamReader(entity.getContent(), "UTF-8");
        result = convertStreamToString(isr);
        Charset.forName("UTF-8").encode(result);

        isr.close();
    }

    httpClient.close();

    return result;
}

From source file:com.tactfactory.harmony.utils.TactFileUtils.java

/** convert file content to a stringbuffer.
 *
 * @param file The File to open/*w ww  .  j a v a2s.co  m*/
 * @return the StringBuffer containing the file's content
 */
public static StringBuffer fileToStringBuffer(final File file) {
    final StringBuffer result = new StringBuffer();
    String tmp;
    final String lineSeparator = System.getProperty("line.separator");

    FileInputStream fis = null;
    InputStreamReader inStream = null;
    BufferedReader bReader = null;
    try {
        fis = new FileInputStream(file);
        inStream = new InputStreamReader(fis, TactFileUtils.DEFAULT_ENCODING);
        bReader = new BufferedReader(inStream);
        while (true) {
            tmp = bReader.readLine();
            if (tmp == null) {
                break;
            }
            result.append(tmp);
            result.append(lineSeparator);
        }

    } catch (final IOException e) {
        ConsoleUtils.displayError(e);
    } finally {
        try {
            if (bReader != null) {
                bReader.close();
            }
            if (inStream != null) {
                inStream.close();
            }
            if (fis != null) {
                fis.close();
            }
        } catch (final IOException e) {
            ConsoleUtils.displayError(e);
        }
    }

    return result;
}