List of usage examples for java.io InputStreamReader close
public void close() throws IOException
From source file:cuanto.api.CuantoConnector.java
/** * This is here to substitute for HttpMethod.getResponseBodyAsString(), which logs an annoying error message each time * it's called./*from w ww . jav a 2s . com*/ * * @param method The method for which to get the response. * @return The full response body as a String. * @throws IOException If something bad happened. */ private String getResponseBodyAsString(HttpMethod method) throws IOException { InputStreamReader reader = new InputStreamReader(method.getResponseBodyAsStream()); StringWriter writer = new StringWriter(); int in; while ((in = reader.read()) != -1) { writer.write(in); } reader.close(); return writer.toString(); }
From source file:de.teamgrit.grit.checking.compile.JavaCompileChecker.java
/** * Runs a command specified by a compiler invocation. * * @param compilerInvocation/* ww w.j a va2 s . c om*/ * specifies what program with which flags is being executed * @param pathToSourceFolder * the path to the source folder of the submission * @return CompilerOutput with fields initialized according to the outcome * of the process * @throws BadFlagException * if a flag is not known to javac */ private CompilerOutput runJavacProcess(List<String> compilerInvocation, Path pathToSourceFolder, boolean junit) throws BadFlagException { Process compilerProcess = null; try { ProcessBuilder compilerProcessBuilder = new ProcessBuilder(compilerInvocation); // make sure the compiler stays in its directory. if (Files.isDirectory(pathToSourceFolder, LinkOption.NOFOLLOW_LINKS)) { compilerProcessBuilder.directory(pathToSourceFolder.toFile()); } else { compilerProcessBuilder.directory(pathToSourceFolder.getParent().toFile()); } compilerProcess = compilerProcessBuilder.start(); } catch (IOException e) { // If we cannot call the compiler we return a CompilerOutput // which is // initialized with false, false, indicating // that the compiler wasn't invoked properly and that there was no // clean Compile. CompilerOutput compilerInvokeError = new CompilerOutput(); compilerInvokeError.setClean(false); compilerInvokeError.setCompilerInvoked(false); return compilerInvokeError; } // Now we read compiler output. If everything is ok javac reports // nothing at all. InputStream compilerOutputStream = compilerProcess.getErrorStream(); InputStreamReader compilerStreamReader = new InputStreamReader(compilerOutputStream); BufferedReader compilerOutputBuffer = new BufferedReader(compilerStreamReader); String line; CompilerOutput compilerOutput = new CompilerOutput(); compilerOutput.setCompilerInvoked(true); List<String> compilerOutputLines = new LinkedList<>(); try { while ((line = compilerOutputBuffer.readLine()) != null) { compilerOutputLines.add(line); } compilerOutputStream.close(); compilerStreamReader.close(); compilerOutputBuffer.close(); compilerProcess.destroy(); } catch (IOException e) { // Reading might go wrong here if javac should unexpectedly // terminate LOGGER.severe("Could not read compiler ourput from its output stream." + " Aborting compile of: " + pathToSourceFolder.toString() + " Got message: " + e.getMessage()); compilerOutput.setClean(false); compilerOutput.setCompileStreamBroken(true); return compilerOutput; } splitCompilerOutput(compilerOutputLines, compilerOutput); if (compilerOutputLines.size() == 0) { compilerOutput.setClean(true); } return compilerOutput; }
From source file:com.sds.acube.jstor.XNDiscXNApi.java
/** * XNDisc XNApi ? ?/*ww w.j av a2 s . c o m*/ */ private static void readVersionFromFile() { XNDiscXNApi_PublishingVersion = "<unknown>"; XNDiscXNApi_PublishingDate = "<unknown>"; InputStreamReader isr = null; LineNumberReader lnr = null; try { isr = new InputStreamReader(XNDiscXNApi.class.getResourceAsStream("/com/sds/acube/jstor/version.txt")); if (isr != null) { lnr = new LineNumberReader(isr); String line = null; do { line = lnr.readLine(); if (line != null) { if (line.startsWith("Publishing-Version=")) { XNDiscXNApi_PublishingVersion = line .substring("Publishing-Version=".length(), line.length()).trim(); } else if (line.startsWith("Publishing-Date=")) { XNDiscXNApi_PublishingDate = line.substring("Publishing-Date=".length(), line.length()) .trim(); } } } while (line != null); lnr.close(); } } catch (IOException ioe) { XNDiscXNApi_PublishingVersion = "<unknown>"; XNDiscXNApi_PublishingDate = "<unknown>"; } finally { try { if (lnr != null) { lnr.close(); } if (isr != null) { isr.close(); } } catch (IOException ioe) { } } }
From source file:org.alloy.metal.xml.merge.MergeContext.java
public String serialize(InputStream in) { InputStreamReader reader = null; int temp;/* ww w. j a v a 2 s.co m*/ StringBuilder item = new StringBuilder(); boolean eof = false; try { reader = new InputStreamReader(in); while (!eof) { temp = reader.read(); if (temp == -1) { eof = true; } else { item.append((char) temp); } } } catch (IOException e) { LOG.error("Unable to merge source and patch locations", e); } finally { if (reader != null) { try { reader.close(); } catch (Throwable e) { LOG.error("Unable to merge source and patch locations", e); } } } return item.toString(); }
From source file:com.esri.gpt.control.arcims.ServletConnectorProxy.java
/** * Fully reads the characters from an InputStream. * //from ww w .j a v a2 s . c o m * @param strm * the InputStream * @return the characters read * @throws IOException * if an exception occurs */ private String readInputCharacters(InputStream inputStream) throws IOException { StringBuffer sb = new StringBuffer(); InputStreamReader ir = null; BufferedReader br = null; try { char cbuf[] = new char[2048]; int n = 0; int nLen = cbuf.length; String sEncoding = "UTF-8"; ir = new InputStreamReader(inputStream, sEncoding); br = new BufferedReader(ir); while ((n = br.read(cbuf, 0, nLen)) > 0) { sb.append(cbuf, 0, n); } } finally { try { if (br != null) br.close(); } catch (Exception ef) { } try { if (ir != null) ir.close(); } catch (Exception ef) { } } return sb.toString(); }
From source file:org.apache.struts2.dispatcher.PlainTextResult.java
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { // verify charset Charset charset = null;//w ww . j ava 2s . c o m if (charSet != null) { if (Charset.isSupported(charSet)) { charset = Charset.forName(charSet); } else { _log.warn("charset [" + charSet + "] is not recognized "); charset = null; } } HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE); ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT); if (charset != null) { response.setContentType("text/plain; charset=" + charSet); } else { response.setContentType("text/plain"); } response.setHeader("Content-Disposition", "inline"); PrintWriter writer = response.getWriter(); InputStreamReader reader = null; try { if (charset != null) { reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation), charset); } else { reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation)); } if (reader == null) { _log.warn("resource at location [" + finalLocation + "] cannot be obtained (return null) from ServletContext !!! "); } else { char[] buffer = new char[BUFFER_SIZE]; int charRead = 0; while ((charRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charRead); } } } finally { if (reader != null) reader.close(); if (writer != null) { writer.flush(); writer.close(); } } }
From source file:ai.eve.volley.Request.java
/** * By default, everyone Request is http-base request, if you wants to load * local file or perform others, also wants to use Cache, you can override * this method to implement non-http request. *//*from w ww . java 2s .c om*/ public NetworkResponse perform() { if (EApplication.DEBUG) { StringBuilder resultStr = new StringBuilder(); try { InputStreamReader inputReader = new InputStreamReader(EApplication.mContext.getAssets().open(mUrl)); BufferedReader bufReader = new BufferedReader(inputReader); String line = ""; while ((line = bufReader.readLine()) != null) { resultStr.append(line); } inputReader.close(); bufReader.close(); return new NetworkResponse(200, resultStr.toString().getBytes(), "UTF-8"); } catch (Exception e) { ELog.E(e.getMessage()); } } return null; }
From source file:net.spinetrak.rpitft.data.streams.logger.NmeaFileLogger.java
private boolean isValid(final InputStream is_) throws IOException { final InputStreamReader isr = new InputStreamReader(is_); final BufferedReader buf = new BufferedReader(isr); boolean valid = false; // try each port few times before giving up for (int i = 0; i < 5; i++) { try {/*from w w w . j a va 2s . c o m*/ final String data = buf.readLine(); if (SentenceValidator.isValid(data)) { LOGGER.info("NMEA data found!"); valid = true; break; } } catch (final Exception ex_) { final String msg = ex_.getMessage(); final Network network = new Network(msg); Dispatcher.getInstance().dispatch(network); ex_.printStackTrace(); LOGGER.error(msg); } } isr.close(); buf.close(); return valid; }
From source file:org.craftercms.cstudio.loadtesting.actions.WriteContent.java
/** * convert InputStream to string/*w w w. j a v a2 s . c o m*/ * @param internalName * * @param is * @return string */ public String getFileContent(String baseFileName, String fileName, String internalName) throws Exception { InputStream is = null; InputStreamReader isReader = null; StringWriter sw = null; XMLWriter writer = null; try { is = this.getClass().getResourceAsStream("/" + baseFileName); isReader = new InputStreamReader(is, "UTF-8"); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(isReader); Element root = document.getRootElement(); Node node = root.selectSingleNode("file-name"); node.setText(fileName); Node node2 = root.selectSingleNode("internal-name"); node2.setText(internalName); sw = new StringWriter(); writer = new XMLWriter(sw); writer.write(document); writer.flush(); return sw.toString(); } finally { if (is != null) { is.close(); } if (isReader != null) { isReader.close(); } if (sw != null) { sw.close(); } if (writer != null) { writer.close(); } } }
From source file:com.ficeto.esp.EspExceptionDecoder.java
private int listenOnProcess(String[] arguments) { try {/* w w w. j a va 2 s. c o m*/ final Process p = ProcessUtils.exec(arguments); Thread thread = new Thread() { public void run() { try { InputStreamReader reader = new InputStreamReader(p.getInputStream()); int c; String line = ""; while ((c = reader.read()) != -1) { if ((char) c == '\r') continue; if ((char) c == '\n') { printLine(line); line = ""; } else { line += (char) c; } } printLine(line); reader.close(); reader = new InputStreamReader(p.getErrorStream()); while ((c = reader.read()) != -1) System.err.print((char) c); reader.close(); } catch (Exception e) { } } }; thread.start(); int res = p.waitFor(); thread.join(); return res; } catch (Exception e) { } return -1; }