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

/**
 * Decodes the contents of the provided {@link InputStream} into a
 * {@link String} using the charset denoted by the charsetName parameter.
 * //from ww w .j  a v a  2  s.  co  m
 * @param byteStream The {@link InputStream} to decode.
 * @param charsetName The charset used to decode the stream.
 * @return A {@link String} representation of the stream's contents.
 * @throws IOException
 */
public static String toString(InputStream byteStream, String charsetName) throws IOException {
    char[] buffer = new char[1024];

    StringBuilder builder = new StringBuilder();

    InputStreamReader reader = new InputStreamReader(byteStream, charsetName);

    for (int length = 0; (length = reader.read(buffer)) >= 0;) {
        builder.append(buffer, 0, length);
    }

    reader.close();

    return builder.toString();
}

From source file:com.rammelkast.anticheatreloaded.config.yaml.CommentedConfiguration.java

/**
 * Creates a new {@link CommentedConfiguration}, loading from the given stream.
 * <p/>//from  www  .j  ava 2  s . c o m
 * Any errors loading the Configuration will be logged and then ignored.
 * If the specified input is not a valid config, a blank config will be returned.
 *
 * @param stream Input stream
 * @return Resulting configuration
 * @throws IllegalArgumentException Thrown if stream is null
 */
public static CommentedConfiguration loadConfiguration(InputStream stream) {
    Validate.notNull(stream, "Stream cannot be null");

    CommentedConfiguration config = new CommentedConfiguration();

    try {
        InputStreamReader reader = new InputStreamReader(stream);
        config.load(reader);
        reader.close();
    } catch (IOException ex) {
        Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration from stream", ex);
    } catch (InvalidConfigurationException ex) {
        Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration from stream", ex);
    }

    return config;
}

From source file:Main.java

public static String streamToString(InputStream input) throws IOException {

    InputStreamReader isr = new InputStreamReader(input);
    BufferedReader reader = new BufferedReader(isr);

    String line;//from ww  w  .j a  v a  2 s.  com
    StringBuffer sb = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }
    reader.close();
    isr.close();
    return sb.toString();
}

From source file:com.indeed.imhotep.web.QueryMetadata.java

private static String streamToString(InputStream inputStream) {
    try {/*from   w w  w .  j av  a2s .  c o  m*/
        final InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charsets.UTF_8);
        final String stringValue = CharStreams.toString(inputStreamReader);
        inputStreamReader.close();
        return stringValue;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:de.innovationgate.utils.XStreamUtils.java

/**
 * Loads an object from a UTF-8 encoded input stream
 * @param xstream The XStream instance to use
 * @param in The input stream to read from
 * @param forceClose force close on the input stream
 * @return The loaded object/*w  w w.ja v a  2 s . co  m*/
 * @throws IOException
 */
public static Object loadUtf8FromInputStream(XStream xstream, InputStream in, boolean forceClose)
        throws IOException {

    BufferedInputStream bufIn = new BufferedInputStream(in);
    try {
        InputStreamReader reader = new InputStreamReader(bufIn, "UTF-8");
        Object obj = xstream.fromXML(reader);
        if (!(in instanceof ZipInputStream) || forceClose) {
            reader.close();
        }
        return obj;
    } catch (UnsupportedEncodingException e) {
        // Cannot happen since Java always supports UTF-8
        return null;
    }
}

From source file:Main.java

/**
 * Get the last 500 lines of the application logcat.
 *
 * @return the log string.//from w  ww .jav  a2 s  . c  om
 * @throws IOException
 */
public static String getLogcat() throws IOException {
    String[] args = { "logcat", "-v", "time", "-d", "-t", "500" };

    Process process = Runtime.getRuntime().exec(args);
    InputStreamReader input = new InputStreamReader(process.getInputStream());
    BufferedReader br = new BufferedReader(input);
    StringBuilder log = new StringBuilder();
    String line;

    while ((line = br.readLine()) != null)
        log.append(line + "\n");

    br.close();
    input.close();

    return log.toString();
}

From source file:Main.java

/** read a resource as a String
 * /*  ww  w  .ja  va  2  s. c  o m*/
 * @param clazz
 * @param name
 * @return
 * @throws IOException
 */
public static String readResourceAsString(Class clazz, String name) throws IOException {
    StringBuffer fileData = new StringBuffer(1000);
    InputStreamReader reader = new InputStreamReader(clazz.getResourceAsStream(name));
    char[] buf = new char[1024];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
        buf = new char[1024];
    }
    reader.close();
    return fileData.toString().replace("\r\n", "\n");
}

From source file:com.baomidou.framework.common.JarHelper.java

public static List<String> readLines(JarFile jarFile, String fileName) throws IOException {
    if (jarFile == null || StringUtils.isEmpty(fileName)) {
        return null;
    }//ww w.  j a va 2s  .c o m
    List<String> lines = new ArrayList<String>();
    JarEntry entry = jarFile.getJarEntry(fileName);
    InputStream inputStream = jarFile.getInputStream(entry);
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    }
    bufferedReader.close();
    inputStreamReader.close();
    return lines;
}

From source file:Main.java

public static String getMac() {
    String macAdress = null;/*from   w ww  . ja v a2  s  .  c o  m*/
    String str = "";
    try {
        Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        for (; null != str;) {
            str = input.readLine();
            if (str != null) {
                macAdress = str.trim();
                break;
            }
        }
        ir.close();
        input.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return macAdress;
}

From source file:com.tingtingapps.securesms.crypto.PreKeyUtil.java

private static int getNextPreKeyId(Context context) {
    try {/*from   w  w  w . jav a  2  s. co  m*/
        File nextFile = new File(getPreKeysDirectory(context), PreKeyIndex.FILE_NAME);

        if (!nextFile.exists()) {
            return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
        } else {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(nextFile));
            PreKeyIndex index = JsonUtils.fromJson(reader, PreKeyIndex.class);
            reader.close();
            return index.nextPreKeyId;
        }
    } catch (IOException e) {
        Log.w("PreKeyUtil", e);
        return Util.getSecureRandom().nextInt(Medium.MAX_VALUE);
    }
}