List of usage examples for java.io InputStreamReader close
public void close() throws IOException
From source file:com.mp3bot.App.java
public String readFromURL(final String url) { String ret = ""; URL my = null;/*from www. j ava 2s . co m*/ contentBuf.clear(); try { my = new URL(url); } catch (MalformedURLException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); return ret; } InputStreamReader reader = null; BufferedReader breader = null; try { reader = new InputStreamReader(my.openStream()); breader = new BufferedReader(reader); } catch (IOException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); return ret; } try { if (reader != null && breader != null) { int rlen = 0; while ((rlen = breader.read(contentBuf)) > 0) { //System.out.println("read len= " + rlen + " bytes"); } reader.close(); } } catch (IOException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); return ret; } reader = null; my = null; contentBuf.flip(); ret = contentBuf.toString().replaceAll("[\n\r]", ""); return ret; }
From source file:cn.org.eshow.framwork.util.AbFileUtil.java
/** * ???Assets./*w w w . j a v a 2 s . co m*/ * * @param context the context * @param name the name * @param encoding the encoding * @return the string */ public static String readAssetsByName(Context context, String name, String encoding) { String text = null; InputStreamReader inputReader = null; BufferedReader bufReader = null; try { inputReader = new InputStreamReader(context.getAssets().open(name)); bufReader = new BufferedReader(inputReader); String line = null; StringBuffer buffer = new StringBuffer(); while ((line = bufReader.readLine()) != null) { buffer.append(line); } text = new String(buffer.toString().getBytes(), encoding); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bufReader != null) { bufReader.close(); } if (inputReader != null) { inputReader.close(); } } catch (Exception e) { e.printStackTrace(); } } return text; }
From source file:com.github.dpsm.android.print.gson.GsonResultOperator.java
@Override public Subscriber<? super Response> call(final Subscriber<? super T> subscriber) { return new Subscriber<Response>() { @Override//from w w w .ja va 2s . c om public void onCompleted() { subscriber.onCompleted(); } @Override public void onError(final Throwable e) { subscriber.onError(e); } @Override public void onNext(final Response response) { switch (response.getStatus()) { case HttpURLConnection.HTTP_OK: InputStreamReader reader = null; try { reader = new InputStreamReader(response.getBody().in()); final JsonElement jsonElement = mGSON.fromJson(new JsonReader(reader), JsonObject.class); if (jsonElement != null && jsonElement.isJsonObject()) { final T result = mConstructor.newInstance(jsonElement.getAsJsonObject()); subscriber.onNext(result); } } catch (Exception e) { subscriber.onError(e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // Nothing to do here } } } break; default: subscriber.onError(new IOException("Http Response Failed: " + response.getStatus())); break; } } }; }
From source file:cn.org.eshow.framwork.util.AbFileUtil.java
/** * ???Raw.// w ww . j a v a 2 s.c o m * * @param context the context * @param id the id * @param encoding the encoding * @return the string */ public static String readRawByName(Context context, int id, String encoding) { String text = null; InputStreamReader inputReader = null; BufferedReader bufReader = null; try { inputReader = new InputStreamReader(context.getResources().openRawResource(id)); bufReader = new BufferedReader(inputReader); String line = null; StringBuffer buffer = new StringBuffer(); while ((line = bufReader.readLine()) != null) { buffer.append(line); } text = new String(buffer.toString().getBytes(), encoding); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bufReader != null) { bufReader.close(); } if (inputReader != null) { inputReader.close(); } } catch (Exception e) { e.printStackTrace(); } } return text; }
From source file:jp.co.cyberagent.jenkins.plugins.DeployStrategy.java
public String getVersion() { File versionFile = new File(mBuild.getWorkspace().getRemote() + "/VERSION"); if (versionFile.exists()) { FileInputStream stream = null; DataInputStream in = null; InputStreamReader reader = null; BufferedReader br = null; try {/*from w w w . j av a 2s . c o m*/ stream = new FileInputStream(versionFile); in = new DataInputStream(stream); reader = new InputStreamReader(in); br = new BufferedReader(reader); return br.readLine(); } catch (Exception e) { getLogger().println(TAG + "Error: " + e.getMessage()); } finally { try { if (stream != null) stream.close(); if (br != null) br.close(); if (reader != null) reader.close(); if (in != null) in.close(); } catch (IOException e) { getLogger().println(TAG + "Error: " + e.getMessage()); } } } return null; }
From source file:Installer.java
public static String readAsciiFile(File file) throws IOException { FileInputStream fin = new FileInputStream(file); InputStreamReader inr = new InputStreamReader(fin, "ASCII"); BufferedReader br = new BufferedReader(inr); StringBuffer sb = new StringBuffer(); for (;;) {/*w ww . j av a2 s . c o m*/ String line = br.readLine(); if (line == null) break; sb.append(line); sb.append("\n"); } br.close(); inr.close(); fin.close(); return sb.toString(); }
From source file:org.apache.hama.manager.util.UITemplate.java
/** * read template file contents//from w w w. jav a 2 s . c o m * * @param template fileName */ protected String loadTemplateResource(String fileName) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); } InputStream is = classLoader.getResourceAsStream(fileName); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); String line; try { while ((line = br.readLine()) != null) { sb.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (isr != null) { try { isr.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); }
From source file:com.omertron.pushoverapi.PushoverApi.java
/** * Sends a raw bit of text via POST to PushoverApi. * * @param message/* w ww .j a v a 2s .c o m*/ * @return JSON reply from PushoverApi. * @throws IOException */ private String sendToPushover(String message) throws IOException { URL pushoverUrl = new URL(PUSHOVER_URL); if (isDebug) System.out.println("Pushing with URL: " + message); HttpsURLConnection connection = (HttpsURLConnection) pushoverUrl.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); OutputStream outputStream = null; try { outputStream = connection.getOutputStream(); outputStream.write(message.getBytes(Charset.forName("UTF-8"))); } finally { if (outputStream != null) { outputStream.close(); } } InputStreamReader isr = null; BufferedReader br = null; StringBuilder output = new StringBuilder(); try { isr = new InputStreamReader(connection.getInputStream()); br = new BufferedReader(isr); connection.disconnect(); String outputCache; while ((outputCache = br.readLine()) != null) { output.append(outputCache); } } finally { if (isr != null) { isr.close(); } if (br != null) { br.close(); } } return output.toString(); }
From source file:org.pentaho.di.baserver.utils.web.HttpConnectionHelper.java
protected String getContent(HttpMethod method, String encoding) throws IOException { String body;// w ww .j av a2 s .com InputStreamReader inputStreamReader; if (!Const.isEmpty(encoding)) { inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream(), encoding); } else { inputStreamReader = new InputStreamReader(method.getResponseBodyAsStream()); } StringBuilder bodyBuffer = new StringBuilder(); int c; while ((c = inputStreamReader.read()) != -1) { bodyBuffer.append((char) c); } inputStreamReader.close(); body = bodyBuffer.toString(); return body; }
From source file:org.andrewberman.sync.PDFDownloader.java
/** * Uploads and downloads the bibtex library file, to synchronize library * changes.// www .j a va 2 s . co m */ void syncBibTex() throws Exception { String base = baseDir.getCanonicalPath() + sep; itemMax = -1; final File bibFile = new File(base + username + ".bib"); // final File md5File = new File(base + ".md5s.txt"); final File dateFile = new File(base + "sync_info.txt"); // Check the MD5s, if the .md5s.txt file exists. long bibModified = bibFile.lastModified(); long lastDownload = 0; boolean download = true; boolean upload = true; if (dateFile.exists()) { String fileS = readFile(dateFile); String[] props = fileS.split("\n"); for (String prop : props) { String[] keyval = prop.split("="); if (keyval[0].equals("date")) { lastDownload = Long.valueOf(keyval[1]); } } } if (lastDownload >= bibModified) { upload = false; } boolean uploadSuccess = false; if (bibFile.exists() && uploadNewer && upload) { BufferedReader br = null; try { status("Uploading BibTex file..."); FilePartSource fsrc = new FilePartSource(bibFile); FilePart fp = new FilePart("file", fsrc); br = new BufferedReader(new FileReader(bibFile)); StringBuffer sb = new StringBuffer(); String s; while ((s = br.readLine()) != null) { sb.append(s + "\n"); } String str = sb.toString(); final Part[] parts = new Part[] { new StringPart("btn_bibtex", "Import BibTeX file..."), new StringPart("to_read", "2"), new StringPart("tag", ""), new StringPart("private", "t"), new StringPart("update_allowed", "t"), new StringPart("update_id", "cul-id"), new StringPart("replace_notes", "t"), new StringPart("replace_tags", "t"), fp }; waitOrExit(); PostMethod filePost = new PostMethod(BASE_URL + "/profile/" + username + "/import_do"); try { filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); httpclient.executeMethod(filePost); String response = filePost.getResponseBodyAsString(); // System.out.println(response); uploadSuccess = true; System.out.println("Bibtex upload success!"); } finally { filePost.releaseConnection(); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) br.close(); } } if (download || upload) { status("Downloading BibTeX file..."); String pageURL = BASE_URL + "bibtex/user/" + username + ""; FileWriter fw = new FileWriter(bibFile); try { GetMethod get = new GetMethod(pageURL); httpclient.executeMethod(get); InputStreamReader read = new InputStreamReader(get.getResponseBodyAsStream()); int c; while ((c = read.read()) != -1) { waitOrExit(); fw.write(c); } read.close(); } finally { fw.close(); } } // Store the checksums. if (uploadSuccess) { // if (fileChecksum == null) // { // fileChecksum = getMd5(bibFile); // remoteChecksum = get(BASE_URL + "bibtex/user/" + username + "?md5=true"); // } // String md5S = fileChecksum + "\n" + remoteChecksum; // writeFile(md5File, md5S); String dateS = "date=" + Calendar.getInstance().getTimeInMillis(); writeFile(dateFile, dateS); } }