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:Main.java

/**
 * Writes the current app logcat to a file.
 *
 * @param filename The filename to save it as
 * @throws IOException//from   w  ww.jav  a  2 s  . c o m
 */
public static void writeLogcat(String filename) throws IOException {
    String[] args = { "logcat", "-v", "time", "-d" };

    Process process = Runtime.getRuntime().exec(args);
    InputStreamReader input = new InputStreamReader(process.getInputStream());
    OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(filename));
    BufferedReader br = new BufferedReader(input);
    BufferedWriter bw = new BufferedWriter(output);
    String line;

    while ((line = br.readLine()) != null) {
        bw.write(line);
        bw.newLine();
    }

    bw.close();
    output.close();
    br.close();
    input.close();
}

From source file:org.nuxeo.apidoc.introspection.ServerInfo.java

public static ServerInfo fromXML(File file) throws IOException {
    InputStreamReader reader = new FileReader(file);
    try {/*from w  w  w.  j a  va 2s.  c o m*/
        return fromXML(reader);
    } finally {
        reader.close();
    }
}

From source file:com.xerox.amazonws.ec2.EC2Utils.java

/**
 * This method makes a best effort to fetch a piece of instance metadata.
 *
 * @param key the name of the metadata to fetch
 * @return value of the metadata item//w w  w .  j  a v  a  2 s .c  o  m
 */
public static String getInstanceUserdata() throws IOException {
    int retries = 0;
    while (true) {
        try {
            URL url = new URL("http://169.254.169.254/latest/user-data/");
            InputStreamReader rdr = new InputStreamReader(url.openStream());
            StringWriter wtr = new StringWriter();
            char[] buf = new char[1024];
            int bytes;
            while ((bytes = rdr.read(buf)) > -1) {
                if (bytes > 0) {
                    wtr.write(buf, 0, bytes);
                }
            }
            rdr.close();
            return wtr.toString();
        } catch (IOException ex) {
            if (retries == 5) {
                logger.debug("Problem getting user data, retries exhausted...");
                return null;
            } else {
                logger.debug("Problem getting user data, retrying...");
                try {
                    Thread.sleep((int) Math.pow(2.0, retries) * 1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }
}

From source file:com.andrious.btc.data.jsonUtils.java

public static Object getObj(String url, TypeToken token) {

    Object obj;/*from  w w w  .  j a  va  2  s . c o m*/

    InputStreamReader stream = HttpConn.urlStream(url);

    try {

        obj = new Gson().fromJson(stream, token.getType());

    } catch (Exception ex) {

        obj = null;

    } finally {

        try {

            stream.close();

        } catch (Exception ex) {
        }
    }

    return obj;
}

From source file:com.andrious.btc.data.jsonUtils.java

public static List<?> getList(String url, TypeToken token) {

    List<?> list;/*from  ww  w .  j a  v a  2 s  .co  m*/

    InputStreamReader stream = HttpConn.urlStream(url);

    try {

        list = new Gson().fromJson(stream, token.getType());

    } catch (Exception ex) {

        list = null;

    } finally {

        try {

            stream.close();

        } catch (Exception ex) {
        }
    }

    return list;
}

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

/**
 * ?txt?//from   w ww . j  ava  2 s .  c  om
 * @param filepath    
 * @return String    
 * @since 1.0.0
 */
public static String readTxtFile(String path) {
    try {
        String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))
                + "../../"; //
        filePath = filePath.replaceAll("file:/", "");
        filePath = filePath.replaceAll("%20", " ");
        filePath = filePath.trim() + path.trim();
        if (filePath.indexOf(":") != 1) {
            filePath = File.separator + filePath;
        }
        String encoding = "utf-8";
        File file = new File(filePath);
        if (file.isFile() && file.exists()) { // ?
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding); // ??
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                return lineTxt;
            }
            read.close();
        } else {
            System.out.println("?,?:" + filePath);
        }
    } catch (Exception e) {
        System.out.println("?");
    }
    return "";
}

From source file:Main.java

public static String readInputStream(InputStreamReader in) {
    StringBuilder sb = new StringBuilder();
    try {//w ww.  j a va 2 s  .  c  o  m
        BufferedReader reader = new BufferedReader(in);
        char[] temp = new char[1024];
        int length;
        while ((length = reader.read(temp)) != -1) {
            if (Thread.currentThread().isInterrupted())
                break;
            sb.append(temp, 0, length);
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
        }
    }
    return (sb.toString());
}

From source file:org.jetbrains.webdemo.ResponseUtils.java

public static String readData(InputStream is) throws IOException {
    InputStreamReader reader = new InputStreamReader(is);
    StringBuilder response = new StringBuilder();
    BufferedReader bufferedReader = new BufferedReader(reader);

    String tmp;//  ww w. j av a 2s .com
    while ((tmp = bufferedReader.readLine()) != null) {
        response.append(tmp);
    }

    reader.close();
    return response.toString();
}

From source file:Main.java

/**
 * Read a file, used for reading weather file from assets
 * @param fileName/*from   www  .  ja v a  2 s  .  c om*/
 * @param context
 * @return
 */
public static String readFromAssetsFile(String fileName, Context context) {
    StringBuilder returnString = new StringBuilder();
    InputStream fIn = null;
    InputStreamReader isr = null;
    BufferedReader input = null;
    try {
        fIn = context.getResources().getAssets().open(fileName, Context.MODE_WORLD_READABLE);
        isr = new InputStreamReader(fIn);
        input = new BufferedReader(isr);
        String line = "";
        while ((line = input.readLine()) != null) {
            returnString.append(line);
        }
    } catch (Exception e) {
    } finally {
        try {
            if (isr != null) {
                isr.close();
            }
            if (fIn != null) {
                fIn.close();
            }
            if (input != null) {
                input.close();
            }
        } catch (Exception e2) {
        }
    }
    return returnString.toString();
}

From source file:it.uniroma2.sag.kelp.linearization.nystrom.NystromMethod.java

/**
 * Load a Nystrom-based projection function from a file
 * /*  ww  w .j a v a 2 s .c o  m*/
 * @param inputFilePath
 *            the input file
 * @return Nystrom-based projection function
 * @throws FileNotFoundException
 * @throws IOException
 */
public static NystromMethod load(String inputFilePath) throws FileNotFoundException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    InputStreamReader inS = new InputStreamReader(FileUtils.createInputStream(inputFilePath), "utf8");
    NystromMethod readValue = mapper.readValue(inS, NystromMethod.class);
    inS.close();
    return readValue;
}