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

public static String readFile(File f) {
    try {// ww  w. j av  a 2 s . c om
        FileInputStream fstream = new FileInputStream(f);
        InputStreamReader in = new InputStreamReader(fstream);
        BufferedReader br = new BufferedReader(in);
        String strLine;
        StringBuilder sb = new StringBuilder();
        while ((strLine = br.readLine()) != null) {
            sb.append(strLine);
            sb.append('\n');
        }
        br.close();
        in.close();
        fstream.close();
        return sb.toString();
    } catch (Exception ex) {
        return "";
    }
}

From source file:Main.java

public static String readFileToString(final Class contextClass, final String streamIdentifier)
        throws IOException {

    InputStreamReader inputStreamReader = null;
    try {//w  w w .  j a  va 2  s . c  om
        inputStreamReader = new InputStreamReader(contextClass.getResourceAsStream(streamIdentifier));
        return CharStreams.toString(inputStreamReader);
    } catch (IOException e) {
        throw new IOException();
    } finally {
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
    }
}

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

private static int getNextSignedPreKeyId(Context context) {
    try {// ww  w  .j a  v  a  2s . c o m
        File nextFile = new File(getSignedPreKeysDirectory(context), SignedPreKeyIndex.FILE_NAME);

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

From source file:org.openqa.selenium.remote.server.SessionLogsTest.java

private static JSONObject getValueForPostRequest(URL serverUrl) throws Exception {
    String postRequest = serverUrl + "/logs";
    HttpClient client = new DefaultHttpClient();
    HttpPost postCmd = new HttpPost(postRequest);
    HttpResponse response = client.execute(postCmd);
    HttpEntity entity = response.getEntity();
    InputStreamReader reader = new InputStreamReader(entity.getContent(), Charsets.UTF_8);
    try {/*from w  w  w.  j a  v a 2s.c om*/
        String str = CharStreams.toString(reader);
        return new JSONObject(str).getJSONObject("value");
    } finally {
        EntityUtils.consume(entity);
        reader.close();
    }
}

From source file:Main.java

static String validGet(URL url) throws IOException {
    String html = "";
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    // TODO ensure HttpsURLConnection.setDefaultSSLSocketFactory isn't
    // required to be reset like hostnames are
    HttpURLConnection conn = null;
    try {//from   w  ww .  ja v a  2s.c  om
        conn = (HttpURLConnection) url.openConnection();
        conn.setUseCaches(false);
        conn.setRequestMethod("GET");
        setBasicAuthentication(conn, url);
        int status = conn.getResponseCode();
        if (status != 200) {
            Logd(TAG, "Failed to get from server, returned code: " + status);
            throw new IOException("Get failed with error code " + status);
        } else {
            InputStreamReader in = new InputStreamReader(conn.getInputStream());
            BufferedReader br = new BufferedReader(in);
            String decodedString;
            while ((decodedString = br.readLine()) != null) {
                html += decodedString;
            }
            in.close();
        }
    } catch (IOException e) {
        Logd(TAG, "Failed to get from server: " + e.getMessage());
    }
    if (conn != null) {
        conn.disconnect();
    }
    return html;
}

From source file:Main.java

public static String readStringFromInputStreamAndCloseStream(final InputStream inputStream,
        final int bufferSize) throws IOException {
    if (bufferSize <= 0) {
        // Safe close: it's more important to alert the programmer of
        // their error than to let them catch and continue on their way.
        throw new IllegalArgumentException("Expected buffer size larger than 0. Got: " + bufferSize);
    }//from  w  ww  .ja  v  a 2 s.  co m

    final StringBuilder stringBuilder = new StringBuilder(bufferSize);
    final InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
    try {
        int charsRead;
        final char[] buffer = new char[bufferSize];
        while ((charsRead = reader.read(buffer, 0, bufferSize)) != -1) {
            stringBuilder.append(buffer, 0, charsRead);
        }
    } finally {
        reader.close();
    }
    return stringBuilder.toString();
}

From source file:Main.java

public static String readFromFile(final File file) throws Exception {
    final FileInputStream filestream = new FileInputStream(file);
    final InputStreamReader reader = new InputStreamReader(filestream);
    try {/*from ww w. jav a 2s .  c  om*/
        final StringBuilder buffer = new StringBuilder();
        final char[] tmp = new char[2048];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toString();
    } finally {
        reader.close();
    }
}

From source file:it.eng.spagobi.studio.core.util.JSONReader.java

public static JSONObject createJSONObject(IFile objSel) {
    logger.debug("IN");
    JSONObject o = null;/*from  w ww.j  a v a 2 s .  co m*/
    try {
        InputStream inputStream = objSel.getContents();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputStreamReader);

        String line = null;
        StringBuffer stringBuffer = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        }

        inputStreamReader.close();
        reader.close();
        inputStream.close();

        String contentString = stringBuffer.toString();

        o = new JSONObject(contentString);

    } catch (Exception e) {
        logger.error("error in reading JSON object", e);
        return null;
    } catch (Throwable e) {
        logger.error("error in reading JSON object", e);
        return null;
    }

    logger.debug("OUT");
    return o;
}

From source file:de.fhg.igd.mapviewer.server.file.MapFileTileProvider.java

/**
 * Creates a {@link MapFileTileProvider}
 * //from   ww  w  .  j  a  v a2s .c  o  m
 * @param mapFile the map file
 * 
 * @return the {@link TileProvider} for the given map file
 * 
 * @throws IOException if the map file cannot be read
 * @throws MalformedURLException if an URI for a map file entry is invalid
 */
public static MapFileTileProvider createMapFileTileProvider(File mapFile)
        throws MalformedURLException, IOException {
    Properties mapProperties = new Properties();
    InputStreamReader mapReader = new InputStreamReader(
            getEntryURI(mapFile, FileTiler.MAP_PROPERTIES_FILE).toURL().openStream());
    try {
        mapProperties.load(mapReader);
    } finally {
        mapReader.close();
    }

    Properties converterProperties = new Properties();
    InputStreamReader converterReader = new InputStreamReader(
            getEntryURI(mapFile, FileTiler.CONVERTER_PROPERTIES_FILE).toURL().openStream());
    try {
        converterProperties.load(converterReader);
    } finally {
        converterReader.close();
    }

    // get informations from map properties
    int zoomLevels = Integer.parseInt(mapProperties.getProperty(FileTiler.PROP_ZOOM_LEVELS));

    int[] mapWidth = new int[zoomLevels];
    int[] mapHeight = new int[zoomLevels];

    for (int zoom = 0; zoom < zoomLevels; zoom++) {
        mapWidth[zoom] = Integer.parseInt(mapProperties.getProperty(FileTiler.PROP_MAP_WIDTH + zoom));
        mapHeight[zoom] = Integer.parseInt(mapProperties.getProperty(FileTiler.PROP_MAP_HEIGHT + zoom));
    }

    int tileWidth = Integer.parseInt(mapProperties.getProperty(FileTiler.PROP_TILE_WIDTH));
    int tileHeight = Integer.parseInt(mapProperties.getProperty(FileTiler.PROP_TILE_HEIGHT));

    return new MapFileTileProvider(mapFile, // map file
            converterProperties, // converter properties
            mapWidth, // map width array
            mapHeight, // map height array
            zoomLevels - 1, // default zoom
            0, // minimum zoom
            zoomLevels - 1, // maximum zoom
            zoomLevels - 1, // total map zoom
            tileWidth, // tile width
            tileHeight // tile height
    );
}

From source file:Main.java

public static StringBuffer readInputStream(InputStream inputStream) throws IOException {
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuffer requestXml = new StringBuffer();

    String line;// w  w w  . j a  v a2  s.co m
    while (null != (line = bufferedReader.readLine())) {
        requestXml.append(line).append(System.getProperty("line.separator"));
    }

    bufferedReader.close();
    inputStreamReader.close();

    return requestXml;
}