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:org.bigbluebutton.impl.BBBProxyImpl.java

/** Make an API call */
public static Map<String, Object> doAPICall(String query) {
    Map<String, Object> response = new HashMap<String, Object>();

    StringBuilder urlStr = new StringBuilder(query);
    try {// www.  j a va 2  s. c  o  m
        // open connection
        log.debug("doAPICall.call: " + query);

        URL url = new URL(urlStr.toString());
        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.setUseCaches(false);
        httpConnection.setDoOutput(true);
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        int responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // read response
            InputStreamReader isr = null;
            BufferedReader reader = null;
            StringBuilder xml = new StringBuilder();
            try {
                isr = new InputStreamReader(httpConnection.getInputStream(), "UTF-8");
                reader = new BufferedReader(isr);
                String line = reader.readLine();
                while (line != null) {
                    if (!line.startsWith("<?xml version=\"1.0\"?>"))
                        xml.append(line.trim());
                    line = reader.readLine();
                }
            } finally {
                if (reader != null)
                    reader.close();
                if (isr != null)
                    isr.close();
            }
            httpConnection.disconnect();

            // parse response
            log.debug("doAPICall.responseXml: " + xml);
            //Patch to fix the NaN error
            String stringXml = xml.toString();
            stringXml = stringXml.replaceAll(">.\\s+?<", "><");

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder;
            try {
                docBuilder = docBuilderFactory.newDocumentBuilder();
                Document dom = null;
                dom = docBuilder.parse(new InputSource(new StringReader(stringXml)));

                response = getNodesAsMap(dom, "response");
                log.debug("doAPICall.responseMap: " + response);

                String returnCode = (String) response.get("returncode");
                if (BBBProxy.APIRESPONSE_FAILED.equals(returnCode)) {
                    log.debug("doAPICall." + (String) response.get("messageKey") + ": Message="
                            + (String) response.get("message"));
                }

            } catch (ParserConfigurationException e) {
                log.debug("Failed to initialise BaseProxy: " + e.getMessage());
            }

        } else {
            log.debug("doAPICall.HTTPERROR: Message=" + "BBB server responded with HTTP status code "
                    + responseCode);
        }

    } catch (IOException e) {
        log.debug("doAPICall.IOException: Message=" + e.getMessage());
    } catch (SAXException e) {
        log.debug("doAPICall.SAXException: Message=" + e.getMessage());
    } catch (IllegalArgumentException e) {
        log.debug("doAPICall.IllegalArgumentException: Message=" + e.getMessage());
    } catch (Exception e) {
        log.debug("doAPICall.Exception: Message=" + e.getMessage());
    }
    return response;
}

From source file:com.farmerbb.secondscreen.util.U.java

public static String getProfileTitle(Context context, String filename) throws IOException {
    // Open the file on disk
    FileInputStream input = context.openFileInput(filename);
    InputStreamReader reader = new InputStreamReader(input);
    BufferedReader buffer = new BufferedReader(reader);

    // Load the file
    String line = buffer.readLine();

    // Close file on disk
    reader.close();

    return (line);
}

From source file:com.cssweb.android.common.CssIniFile.java

public static String loadStockData(Context context, String filename) {
    FileInputStream fileIn = null;
    InputStreamReader in = null;
    BufferedReader buffer = null;
    StringBuffer text = new StringBuffer();
    String res = null;//from w  w  w . j av a  2  s  .c o m
    try {
        fileIn = context.openFileInput(filename + ".dat");
        in = new InputStreamReader(fileIn);
        buffer = new BufferedReader(in);
        while ((res = buffer.readLine()) != null) {
            text.append(res);
        }
        res = text.toString();
    } catch (FileNotFoundException e) {
        //e.printStackTrace();
        res = null;
    } catch (IOException e) {
        //e.printStackTrace();
        res = null;
    } finally {
        try {
            if (fileIn != null)
                fileIn.close();
        } catch (IOException e) {
        }
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
        }
        try {
            if (buffer != null)
                buffer.close();
        } catch (IOException e) {
        }
    }
    return res;
}

From source file:com.flipkart.polyguice.config.YamlConfiguration.java

public YamlConfiguration(URL url) throws IOException {
    InputStreamReader reader = new InputStreamReader(url.openStream());
    load(reader);//from w  ww. java  2 s  .c  o  m
    reader.close();
}

From source file:com.cubusmail.mail.text.MessageTextUtil.java

/**
 * Reads a string from given input stream using direct buffering
 * /*www . jav a2 s.co  m*/
 * @param inStream
 *            - the input stream
 * @param charset
 *            - the charset
 * @return the <code>String</code> read from input stream
 * @throws IOException
 *             - if an I/O error occurs
 */
public static String readStream(final InputStream inStream, final String charset) throws IOException {

    InputStreamReader isr = null;
    try {
        int count = 0;
        final char[] c = new char[BUFSIZE];
        isr = new InputStreamReader(inStream, charset);
        if ((count = isr.read(c)) > 0) {
            final StringBuilder sb = new StringBuilder(STRBLD_SIZE);
            do {
                sb.append(c, 0, count);
            } while ((count = isr.read(c)) > 0);
            return sb.toString();
        }
        return "";
    } catch (final UnsupportedEncodingException e) {
        log.error("Unsupported encoding in a message detected and monitored.", e);
        return "";
    } finally {
        if (null != isr) {
            try {
                isr.close();
            } catch (final IOException e) {
                log.error(e.getLocalizedMessage(), e);
            }
        }
    }
}

From source file:com.flipkart.polyguice.config.JsonConfiguration.java

public JsonConfiguration(URL url) throws IOException {
    InputStreamReader reader = new InputStreamReader(url.openStream());
    load(reader);//  www  .  j a  va2 s  .  co m
    reader.close();
}

From source file:de.root1.logiccollection.offLogicVocESP8266.java

@Override
public void init() {
    this.tt = new TimerTask() {

        @Override//  w w w  .  j a v a  2  s  .  c  o  m
        public void run() {
            try {
                Socket s = new Socket("nodemcu1", 44444);
                OutputStream out = s.getOutputStream();
                InputStream in = s.getInputStream();
                out.write("\n".getBytes());
                out.flush();

                InputStreamReader isr = new InputStreamReader(in);
                JSONObject data = (JSONObject) JSONValue.parse(isr);
                isr.close();
                out.close();

                int voc = Integer.parseInt(data.get("voc").toString());
                int tvoc = Integer.parseInt(data.get("voc").toString());
                int resistance = Integer.parseInt(data.get("resistance").toString());
                int status = Integer.parseInt(data.get("status").toString());

                log.info("voc={}, tvoc={} resistance={} status={}",
                        new Object[] { voc, tvoc, resistance, status });

                //                    write(ga, String.valueOf(voc));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    };
    setPA("1.1.203");
    t.schedule(tt, 5000, 60000);
    log.info("VOC ESP8266 reader is running.");
}

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

private void loadLines(String filename) {

    InputStream stream = LineExtractGenerator.class.getClassLoader().getResourceAsStream(filename);
    if (stream == null) {
        throw new RuntimeException(filename + " was missing.");
    }//from   w  w  w.j ava  2s.  co  m

    CharBuffer linesImage;
    try {
        InputStreamReader isr = new InputStreamReader(stream);
        linesImage = CharBuffer.allocate(1024 * 1024);
        isr.read(linesImage);
        isr.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
    linesImage.flip();
    Collections.addAll(lines, linesImage.toString().split("\n"));
}

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

private CharBuffer loadLoremIpsum() {
    InputStream stream = LoremExtractGenerator.class.getClassLoader()
            .getResourceAsStream("data/lorem_ipsum_full.txt");
    if (stream == null) {
        throw new RuntimeException("lorem_ipsum_full.txt was missing.");
    }//from  ww w.j a  v a  2s  . co  m

    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.log4ic.compressor.utils.HttpUtils.java

/**
 * //www . ja v  a 2s. co  m
 *
 * @param httpUrl
 * @return
 * @throws java.io.IOException
 */
public static String requestFile(String httpUrl) {
    URL url = null;
    try {
        url = new URL(httpUrl);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    if (url == null) {
        return null;
    }
    InputStream in = null;
    InputStreamReader streamReader = null;
    BufferedReader reader = null;
    try {
        in = url.openStream();
        streamReader = new InputStreamReader(in);
        reader = new BufferedReader(streamReader);
        String lineCode;
        StringBuilder pageCodeBuffer = new StringBuilder();
        while ((lineCode = reader.readLine()) != null) {
            pageCodeBuffer.append(lineCode);
            pageCodeBuffer.append("\n");
        }

        return pageCodeBuffer.toString();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (streamReader != null) {
            try {
                streamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}