List of usage examples for java.io OutputStreamWriter close
public void close() throws IOException
From source file:com.xpn.xwiki.plugin.packaging.AbstractPackageTest.java
private byte[] getEncodedByteArray(String content, String charset) throws IOException { StringReader rdr = new StringReader(content); BufferedReader bfr = new BufferedReader(rdr); ByteArrayOutputStream ostr = new ByteArrayOutputStream(); OutputStreamWriter os = new OutputStreamWriter(ostr, charset); // Voluntarily ignore the first line... as it's the xml declaration String line = bfr.readLine(); os.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\n"); line = bfr.readLine();/* w w w .ja va2 s .c o m*/ while (null != line) { os.append(line); os.append("\n"); line = bfr.readLine(); } os.flush(); os.close(); return ostr.toByteArray(); }
From source file:com.doomy.padlock.MainFragment.java
public void superUserReboot(String mCommand) { Runtime mRuntime = Runtime.getRuntime(); Process mProcess = null;//w w w . j a va2 s. c om OutputStreamWriter mWrite = null; try { mProcess = mRuntime.exec("su"); mWrite = new OutputStreamWriter(mProcess.getOutputStream()); mWrite.write(mCommand + "\n"); mWrite.flush(); mWrite.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:nl.welteninstituut.tel.oauth.OauthWorker.java
protected String postToURL(URL url, String data) throws IOException { try {/*from w ww . j av a 2 s .c o m*/ URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String result = ""; String line; while ((line = rd.readLine()) != null) { result += line; } wr.close(); rd.close(); return result; } catch (Exception e) { } return "{}"; }
From source file:net.sf.jasperreports.AbstractTest.java
protected String errExportDigest(Throwable t) throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException { File outputFile = createXmlOutputFile(); log.debug("Error stack trace at " + outputFile.getAbsolutePath()); MessageDigest digest = MessageDigest.getInstance("SHA-1"); FileOutputStream output = new FileOutputStream(outputFile); OutputStreamWriter osw = null; try {/*from w ww . j ava 2 s. com*/ DigestOutputStream out = new DigestOutputStream(output, digest); //PrintStream ps = new PrintStream(out); //t.printStackTrace(ps); osw = new OutputStreamWriter(out, "UTF-8"); osw.write(String.valueOf(t.getMessage())); } finally { osw.close(); output.close(); } return toDigestString(digest); }
From source file:com.cookbook.cq.dao.util.GsonHttpMessageConverter.java
@Override protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), getCharset(outputMessage.getHeaders())); try {/* w ww .j a va 2s.c o m*/ if (this.prefixJson) { writer.append("{} && "); } Type typeOfSrc = getType(); if (typeOfSrc != null) { this.gson.toJson(o, typeOfSrc, writer); } else { this.gson.toJson(o, writer); } writer.close(); } catch (JsonIOException ex) { throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); } }
From source file:com.hangum.tadpole.importdb.core.dialog.importdb.sql.SQLToDBImportDialog.java
private void saveLog() { try {/*from ww w . j a v a2 s. c om*/ if (bufferBatchResult == null || "".equals(bufferBatchResult.toString())) { //$NON-NLS-1$ MessageDialog.openError(null, Messages.CsvToRDBImportDialog_4, Messages.SQLToDBImportDialog_LogEmpty); return; } String filename = PublicTadpoleDefine.TEMP_DIR + userDB.getDisplay_name() + "_SQLImportResult.log"; //$NON-NLS-1$ FileOutputStream fos = new FileOutputStream(filename); OutputStreamWriter bw = new OutputStreamWriter(fos, "UTF-8"); //$NON-NLS-1$ bw.write(bufferBatchResult.toString()); bw.close(); String strImportResultLogContent = FileUtils.readFileToString(new File(filename)); downloadExtFile(userDB.getDisplay_name() + "_SQLImportResult.log", strImportResultLogContent);//sbExportData.toString()); //$NON-NLS-1$ } catch (Exception ee) { logger.error("log writer", ee); //$NON-NLS-1$ } }
From source file:com.hangum.tadpole.importexport.core.dialogs.SQLToDBImportDialog.java
private void saveLog() { try {//from w w w. j a va2 s . c o m if (bufferBatchResult == null || "".equals(bufferBatchResult.toString())) { //$NON-NLS-1$ MessageDialog.openWarning(null, Messages.get().Warning, Messages.get().SQLToDBImportDialog_LogEmpty); return; } String filename = PublicTadpoleDefine.TEMP_DIR + userDB.getDisplay_name() + "_SQLImportResult.log"; //$NON-NLS-1$ FileOutputStream fos = new FileOutputStream(filename); OutputStreamWriter bw = new OutputStreamWriter(fos, "UTF-8"); //$NON-NLS-1$ bw.write(bufferBatchResult.toString()); bw.close(); String strImportResultLogContent = FileUtils.readFileToString(new File(filename)); downloadExtFile(userDB.getDisplay_name() + "_SQLImportResult.log", strImportResultLogContent);//sbExportData.toString()); //$NON-NLS-1$ } catch (Exception ee) { logger.error("log writer", ee); //$NON-NLS-1$ } }
From source file:org.guanxi.sp.engine.service.saml2.WebBrowserSSOAuthConsumerService.java
private String processGuardConnection(String acsURL, String entityID, String keystoreFile, String keystorePassword, String truststoreFile, String truststorePassword, ResponseDocument responseDocument, String guardSession) throws GuanxiException, IOException { EntityConnection connection;/* ww w .j av a2 s .c om*/ Bag bag = getBag(responseDocument, guardSession); // Initialise the connection to the Guard's attribute consumer service connection = new EntityConnection(acsURL, entityID, keystoreFile, keystorePassword, truststoreFile, truststorePassword, EntityConnection.PROBING_OFF); connection.setDoOutput(true); connection.connect(); // Send the data to the Guard in an explicit POST variable String json = URLEncoder.encode(Guanxi.REQUEST_PARAMETER_SAML_ATTRIBUTES, "UTF-8") + "=" + URLEncoder.encode(bag.toJSON(), "UTF-8"); OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream()); wr.write(json); wr.flush(); wr.close(); //os.close(); // ...and read the response from the Guard return new String(Utils.read(connection.getInputStream())); }
From source file:ca.uhn.fhir.tinder.ant.TinderGeneratorTask.java
@Override public void execute() throws BuildException { validateAttributes();/*from w w w . j a v a 2s. c o m*/ try { if (baseResourceNames == null || baseResourceNames.isEmpty()) { baseResourceNames = new ArrayList<String>(); log("No resource names supplied, going to use all resources from version: " + fhirContext.getVersion().getVersion()); Properties p = new Properties(); try { p.load(fhirContext.getVersion().getFhirVersionPropertiesFile()); } catch (IOException e) { throw new BuildException("Failed to load version property file", e); } if (verbose) { log("Property file contains: " + p); } for (Object next : p.keySet()) { if (((String) next).startsWith("resource.")) { baseResourceNames.add(((String) next).substring("resource.".length()).toLowerCase()); } } } else { for (int i = 0; i < baseResourceNames.size(); i++) { baseResourceNames.set(i, baseResourceNames.get(i).toLowerCase()); } } if (excludeResourceNames != null) { for (int i = 0; i < excludeResourceNames.size(); i++) { baseResourceNames.remove(excludeResourceNames.get(i).toLowerCase()); } } log("Including the following resources: " + baseResourceNames); ResourceGeneratorUsingSpreadsheet gen = new ResourceGeneratorUsingSpreadsheet(version, projectHome); gen.setBaseResourceNames(baseResourceNames); try { gen.parse(); // gen.setFilenameSuffix("ResourceProvider"); // gen.setTemplate("/vm/jpa_daos.vm"); // gen.writeAll(packageDirectoryBase, null,packageBase); // gen.setFilenameSuffix("ResourceTable"); // gen.setTemplate("/vm/jpa_resource_table.vm"); // gen.writeAll(directoryBase, packageBase); } catch (Exception e) { throw new BuildException("Failed to parse FHIR metadata", e); } try { VelocityContext ctx = new VelocityContext(); ctx.put("resources", gen.getResources()); ctx.put("packageBase", packageBase); ctx.put("targetPackage", targetPackage); ctx.put("version", version); ctx.put("esc", new EscapeTool()); String capitalize = WordUtils.capitalize(version); if ("Dstu".equals(capitalize)) { capitalize = "Dstu1"; } ctx.put("versionCapitalized", capitalize); VelocityEngine v = new VelocityEngine(); v.setProperty("resource.loader", "cp"); v.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); v.setProperty("runtime.references.strict", Boolean.TRUE); targetDirectoryFile.mkdirs(); if (targetFile != null) { InputStream templateIs = null; if (templateFileFile != null) { templateIs = new FileInputStream(templateFileFile); } else { templateIs = ResourceGeneratorUsingSpreadsheet.class.getResourceAsStream(template); } InputStreamReader templateReader = new InputStreamReader(templateIs); File target = null; if (targetPackage != null) { target = new File(targetDir, targetPackage.replace('.', File.separatorChar)); } else { target = new File(targetDir); } target.mkdirs(); File f = new File(target, targetFile); OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(f, false), "UTF-8"); v.evaluate(ctx, w, "", templateReader); w.close(); } else { File packageDirectoryBase = new File(targetDir, packageBase.replace(".", File.separatorChar + "")); packageDirectoryBase.mkdirs(); gen.setFilenameSuffix(targetClassSuffix); gen.setTemplate(template); gen.setTemplateFile(templateFileFile); gen.writeAll(packageDirectoryBase, null, packageBase); } } catch (Exception e) { log("Caught exception: " + e.getClass().getName() + " [" + e.getMessage() + "]", 1); e.printStackTrace(); throw new BuildException("Failed to generate file(s)", e); } cleanup(); } catch (Exception e) { if (e instanceof BuildException) { throw (BuildException) e; } log("Caught exception: " + e.getClass().getName() + " [" + e.getMessage() + "]", 1); e.printStackTrace(); throw new BuildException("Error processing " + getTaskName() + " task.", e); } }
From source file:com.cubusmail.server.services.ShowMessageSourceServlet.java
@Override public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {/*from w w w .j av a2s.com*/ String messageId = request.getParameter("messageId"); if (messageId != null) { IMailbox mailbox = SessionManager.get().getMailbox(); Message msg = mailbox.getCurrentFolder().getMessageById(Long.parseLong(messageId)); ContentType contentType = new ContentType("text/plain"); response.setContentType(contentType.getBaseType()); response.setHeader("expires", "0"); String charset = null; if (msg.getContentType() != null) { try { charset = new ContentType(msg.getContentType()).getParameter("charset"); } catch (Throwable e) { // should never happen } } if (null == charset || charset.equalsIgnoreCase(CubusConstants.US_ASCII)) { charset = CubusConstants.DEFAULT_CHARSET; } OutputStream outputStream = response.getOutputStream(); // writing the header String header = generateHeader(msg); outputStream.write(header.getBytes(), 0, header.length()); BufferedInputStream bufInputStream = new BufferedInputStream(msg.getInputStream()); InputStreamReader reader = null; try { reader = new InputStreamReader(bufInputStream, charset); } catch (UnsupportedEncodingException e) { log.error(e.getMessage(), e); reader = new InputStreamReader(bufInputStream); } OutputStreamWriter writer = new OutputStreamWriter(outputStream); char[] inBuf = new char[1024]; int len = 0; try { while ((len = reader.read(inBuf)) > 0) { writer.write(inBuf, 0, len); } } catch (Throwable e) { log.warn("Download canceled!"); } writer.flush(); writer.close(); outputStream.flush(); outputStream.close(); } } catch (Exception e) { log.error(e.getMessage(), e); } }