List of usage examples for java.io BufferedReader close
public void close() throws IOException
From source file:com.breadwallet.tools.util.JsonParser.java
private static String callURL(String myURL) { // System.out.println("Requested URL_EA:" + myURL); StringBuilder sb = new StringBuilder(); HttpURLConnection urlConn = null; InputStreamReader in = null;/*w w w .j a v a 2 s . co m*/ try { URL url = new URL(myURL); urlConn = (HttpURLConnection) url.openConnection(); int versionNumber = 0; MainActivity app = MainActivity.app; if (app != null) { try { PackageInfo pInfo = null; pInfo = app.getPackageManager().getPackageInfo(app.getPackageName(), 0); versionNumber = pInfo.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } int stringId = 0; String appName = ""; if (app != null) { stringId = app.getApplicationInfo().labelRes; appName = app.getString(stringId); } String message = String.format(Locale.getDefault(), "%s/%d/%s", appName.isEmpty() ? "breadwallet" : appName, versionNumber, System.getProperty("http.agent")); urlConn.setRequestProperty("User-agent", message); urlConn.setReadTimeout(60 * 1000); String strDate = urlConn.getHeaderField("date"); if (strDate == null || app == null) { Log.e(TAG, "callURL: strDate == null!!!"); } else { @SuppressWarnings("deprecation") long date = Date.parse(strDate) / 1000; SharedPreferencesManager.putSecureTime(app, date); Assert.assertTrue(date != 0); } if (urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } assert in != null; in.close(); } catch (Exception e) { return null; } return sb.toString(); }
From source file:Main.java
public static String convertStreamToString(InputStream stream) { StringBuilder sb = new StringBuilder(); InputStreamReader isReader = new InputStreamReader(stream); BufferedReader reader = null; try {// w ww . j a va2 s . c om reader = new BufferedReader(isReader); String line = ""; while ((line = reader.readLine()) != null) { sb.append(line); } } catch (Exception ex) { ex.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); }
From source file:it.damore.solr.importexport.App.java
/** * @param sUrl//from w w w . j a v a 2s.c o m * @return * @throws MalformedURLException * @throws IOException */ private static String readUrl(String sUrl) throws MalformedURLException, IOException { StringBuilder sbJson = new StringBuilder(); URL url = new URL(sUrl); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) sbJson.append(inputLine); in.close(); return sbJson.toString(); }
From source file:gaffer.accumulo.utils.IngestUtils.java
/** * Read a Base64 encoded splits file and return the splits as Text objects * //from w w w .j a va2 s . c o m * @param fs The FileSystem which contains the splits file * @param splitsFile The Path to the splits file * @return * @throws IOException */ public static SortedSet<Text> getSplitsFromFile(FileSystem fs, Path splitsFile) throws IOException { FSDataInputStream fis = fs.open(splitsFile); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); SortedSet<Text> splits = new TreeSet<Text>(); String line = null; while ((line = reader.readLine()) != null) { splits.add(new Text(Base64.decodeBase64(line))); } reader.close(); return splits; }
From source file:Main.java
/** * readXMLFromFile method reads the template request xml file. * @param inputStream//from w w w . jav a 2 s .co m * @return * @throws Exception */ public static String readXMLFromFile(InputStream inputStream) throws Exception { BufferedReader in = null; StringBuffer strBuffer = new StringBuffer(); try { in = new BufferedReader(new InputStreamReader(inputStream)); String str; while ((str = in.readLine()) != null) { strBuffer.append(str + "\n"); } } finally { if (in != null) { in.close(); } } String returnString = strBuffer.toString(); return returnString; }
From source file:com.docudile.app.services.utils.FileHandler.java
public static Map<String, List<String>> readAllFiles(String folder) throws IOException { ArrayList<String> filenames = getFilePaths(folder); Map<String, List<String>> files = new HashMap<>(); for (String filename : filenames) { FileReader fr = new FileReader(folder + "/" + filename); BufferedReader br = new BufferedReader(fr); ArrayList<String> lines = new ArrayList<String>(); String temp;/*from w w w .java 2s . c o m*/ while ((temp = br.readLine()) != null) { if (StringUtils.isNotEmpty(temp)) { lines.add(temp); } } files.put(filename.split("\\.")[0], lines); br.close(); fr.close(); } return files; }
From source file:Main.java
/** * Trims down obj files, so that they may be parsed faster later on. * Remove uneccessary whitespaces, comments etc. * @param in stream to be trimmed/*w ww. ja va2 s .co m*/ * @param out the resulting trimmed stream */ public static void trim(BufferedReader in, BufferedWriter out) throws IOException { String line; out.write("#trimmed\n"); for (line = in.readLine(); line != null; line = in.readLine()) { line = getCanonicalLine(line); if (line.length() > 0) { out.write(line.trim()); out.write('\n'); } } in.close(); out.close(); }
From source file:Main.java
/** Returns a text where all line separator are \n * * @return the normalized text/*w w w. j a v a2s. com*/ */ public static String getNormalizedText(String text) { StringBuffer normalizedText = new StringBuffer(); BufferedReader reader = new BufferedReader(new StringReader(text)); try { String line; while ((line = reader.readLine()) != null) { normalizedText.append(line); normalizedText.append("\n"); } } catch (IOException e) { e.printStackTrace(); } try { reader.close(); } catch (IOException e) { e.printStackTrace(); } return normalizedText.toString(); }
From source file:cc.vileda.sipgatesync.api.SipgateApi.java
public static String getToken(final String username, final String password) { try {//www .ja va 2s . co m final HttpURLConnection urlConnection = getConnection("/authorization/token"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("POST"); JSONObject request = new JSONObject(); request.put("username", username); request.put("password", password); OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream()); Log.d("SipgateApi", request.getString("username")); wr.write(request.toString()); wr.flush(); StringBuilder sb = new StringBuilder(); int HttpResult = urlConnection.getResponseCode(); if (HttpResult == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), "utf-8")); String line; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } br.close(); Log.d("SipgateApi", "" + sb.toString()); final JSONObject response = new JSONObject(sb.toString()); return response.getString("token"); } else { System.out.println(urlConnection.getResponseMessage()); } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static BufferedReader readFile(Context context, String fileName) { BufferedReader br = null; StringBuffer stringBuffer = new StringBuffer(); try {/*ww w .ja v a2 s . c o m*/ br = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName))); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return br; }