List of usage examples for java.io OutputStreamWriter write
public void write(String str, int off, int len) throws IOException
From source file:com.duroty.utils.mail.MessageUtilities.java
/** * DOCUMENT ME!//from w w w .j av a 2s.c om * * @param multipart DOCUMENT ME! * @param vcard DOCUMENT ME! * @param charset DOCUMENT ME! * * @throws MessagingException DOCUMENT ME! */ public static void attach(MimeMultipart multipart, vCard vcard, String charset) throws MessagingException { String xpath = "/tmp"; String xcontent = vcard.toString(); try { // write the vCard to a temporary file in UTF-8 format File xfile = new File(xpath); FileOutputStream xfos = new FileOutputStream(xfile); OutputStreamWriter xosw = new OutputStreamWriter(xfos, Charset.defaultCharset().displayName()); xosw.write(xcontent, 0, xcontent.length()); xosw.flush(); xosw.close(); xfos.close(); // attach the temporary file to the message attach(multipart, xfile, Charset.defaultCharset().displayName()); } catch (Exception xex) { System.out.println("vCard attachment failed: " + xex.toString()); } }
From source file:github.madmarty.madsonic.service.RESTMusicService.java
@Override public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setSoTimeout(params, SOCKET_READ_TIMEOUT_GET_PLAYLIST); Reader reader = getReader(context, progressListener, "getPlaylist", params, "id", id); OutputStreamWriter out = null; try {// w w w .j a va 2 s.com out = new OutputStreamWriter(new FileOutputStream(FileUtil.getPlaylistFile(name))); char[] buff = new char[256]; int n; while ((n = reader.read(buff)) >= 0) { out.write(buff, 0, n); } } finally { Util.close(out); Util.close(reader); } try { reader = new FileReader(FileUtil.getPlaylistFile(name)); return new PlaylistParser(context).parse(reader, progressListener); } finally { Util.close(reader); } }
From source file:org.fcrepo.server.access.FedoraAccessServlet.java
public void getObjectProfile(Context context, String PID, Date asOfDateTime, boolean xml, HttpServletRequest request, HttpServletResponse response) throws ServerException { OutputStreamWriter out = null; Date versDateTime = asOfDateTime; ObjectProfile objProfile = null;/*from ww w .j a v a 2 s. c o m*/ PipedWriter pw = null; PipedReader pr = null; try { pw = new PipedWriter(); pr = new PipedReader(pw); objProfile = m_access.getObjectProfile(context, PID, asOfDateTime); if (objProfile != null) { // Object Profile found. // Serialize the ObjectProfile object into XML new ProfileSerializerThread(context, PID, objProfile, versDateTime, pw).start(); if (xml) { // Return results as raw XML response.setContentType(CONTENT_TYPE_XML); // Insures stream read from PipedReader correctly translates // utf-8 // encoded characters to OutputStreamWriter. out = new OutputStreamWriter(response.getOutputStream(), "UTF-8"); char[] buf = new char[BUF]; int len = 0; while ((len = pr.read(buf, 0, BUF)) != -1) { out.write(buf, 0, len); } out.flush(); } else { // Transform results into an html table response.setContentType(CONTENT_TYPE_HTML); out = new OutputStreamWriter(response.getOutputStream(), "UTF-8"); File xslFile = new File(m_server.getHomeDir(), "access/viewObjectProfile.xslt"); Templates template = XmlTransformUtility.getTemplates(xslFile); Transformer transformer = template.newTransformer(); transformer.setParameter("fedora", context.getEnvironmentValue(FEDORA_APP_CONTEXT_NAME)); transformer.transform(new StreamSource(pr), new StreamResult(out)); } out.flush(); } else { throw new GeneralException("No object profile returned"); } } catch (ServerException e) { throw e; } catch (Throwable th) { String message = "Error getting object profile"; logger.error(message, th); throw new GeneralException(message, th); } finally { try { if (pr != null) { pr.close(); } if (out != null) { out.close(); } } catch (Throwable th) { String message = "Error closing output"; logger.error(message, th); throw new StreamIOException(message); } } }
From source file:com.jtschohl.androidfirewall.MainActivity.java
/** * Get iptables information/*w w w.j a v a2s .c o m*/ */ private void getIptablesInfo() { final Context ctx = getApplicationContext(); File sdCard = Environment.getExternalStorageDirectory(); File dir = new File(sdCard.getAbsolutePath() + "/af_error_reports/"); String filename = "iptables.txt"; File file = new File(dir, filename); FileOutputStream fout = null; OutputStreamWriter output = null; String iptables = Api.showIptablesRules(ctx); try { for (String str : iptables.split("\r\n")) { fout = new FileOutputStream(file); output = new OutputStreamWriter(fout); output.write(str, 0, str.length()); } } catch (IOException e) { Log.e(TAG, "File write failed: " + e.toString()); } finally { try { if (output != null) { output.close(); Log.d(TAG, "OUTPUT Closed"); } if (fout != null) { fout.close(); Log.d(TAG, "FOUT Closed"); getLogcatInfo(); } } catch (IOException e) { Log.e(TAG, String.format("File close failed: %s", e.toString())); } } }
From source file:com.izforge.izpack.compiler.CompilerConfig.java
/** * Adds the resources./*from w ww .j a va2s.c om*/ * * @param data The XML data. * @throws CompilerException Description of the Exception */ protected void addResources(IXMLElement data) throws CompilerException { notifyCompilerListener("addResources", CompilerListener.BEGIN, data); IXMLElement root = data.getFirstChildNamed("resources"); if (root == null) { return; } // We process each res markup for (IXMLElement resNode : root.getChildrenNamed("res")) { String id = xmlCompilerHelper.requireAttribute(resNode, "id"); String src = xmlCompilerHelper.requireAttribute(resNode, "src"); // the parse attribute causes substitution to occur boolean substitute = xmlCompilerHelper.validateYesNoAttribute(resNode, "parse", NO); // the parsexml attribute causes the xml document to be parsed boolean parsexml = xmlCompilerHelper.validateYesNoAttribute(resNode, "parsexml", NO); String encoding = resNode.getAttribute("encoding"); if (encoding == null) { encoding = ""; } // basedir is not prepended if src is already an absolute path URL originalUrl = resourceFinder.findProjectResource(src, "Resource", resNode); URL url = originalUrl; InputStream is = null; OutputStream os = null; try { if (parsexml || (!"".equals(encoding)) || (substitute && !packager.getVariables().isEmpty())) { // make the substitutions into a temp file File parsedFile = FileUtils.createTempFile("izpp", null); parsedFile.deleteOnExit(); FileOutputStream outFile = new FileOutputStream(parsedFile); os = new BufferedOutputStream(outFile); // and specify the substituted file to be added to the // packager url = parsedFile.toURI().toURL(); } if (!"".equals(encoding)) { File recodedFile = FileUtils.createTempFile("izenc", null); recodedFile.deleteOnExit(); InputStreamReader reader = new InputStreamReader(originalUrl.openStream(), encoding); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(recodedFile), "UTF-8"); char[] buffer = new char[1024]; int read; while ((read = reader.read(buffer)) != -1) { writer.write(buffer, 0, read); } reader.close(); writer.close(); if (parsexml) { originalUrl = recodedFile.toURI().toURL(); } else { url = recodedFile.toURI().toURL(); } } if (parsexml) { IXMLParser parser = new XMLParser(); // this constructor will open the specified url (this is // why the InputStream is not handled in a similar manner // to the OutputStream) IXMLElement xml = parser.parse(originalUrl); IXMLWriter writer = new XMLWriter(); if (substitute && !packager.getVariables().isEmpty()) { // if we are also performing substitutions on the file // then create an in-memory copy to pass to the // substitutor ByteArrayOutputStream baos = new ByteArrayOutputStream(); writer.setOutput(baos); is = new ByteArrayInputStream(baos.toByteArray()); } else { // otherwise write direct to the temp file writer.setOutput(os); } writer.write(xml); } // substitute variable values in the resource if parsed if (substitute) { if (packager.getVariables().isEmpty()) { // reset url to original. url = originalUrl; assertionHelper.parseWarn(resNode, "No variables defined. " + url.getPath() + " not parsed."); } else { SubstitutionType type = SubstitutionType.lookup(resNode.getAttribute("type")); // if the xml parser did not open the url // ('parsexml' was not enabled) if (null == is) { is = new BufferedInputStream(originalUrl.openStream()); } // VariableSubstitutor vs = new // VariableSubstitutorImpl(compiler.getVariables()); variableSubstitutor.substitute(is, os, type, "UTF-8"); } } } catch (Exception e) { assertionHelper.parseError(resNode, e.getMessage(), e); } finally { if (null != os) { try { os.close(); } catch (IOException e) { // ignore as there is nothing we can realistically do // so lets at least try to close the input stream } } if (null != is) { try { is.close(); } catch (IOException e) { // ignore as there is nothing we can realistically do } } } packager.addResource(id, url); // remembering references to all added packsLang.xml files if (id.startsWith("packsLang.xml")) { List<URL> packsLangURLs; if (packsLangUrlMap.containsKey(id)) { packsLangURLs = packsLangUrlMap.get(id); } else { packsLangURLs = new ArrayList<URL>(); packsLangUrlMap.put(id, packsLangURLs); } packsLangURLs.add(url); } else if (id.startsWith(UserInputPanel.SPEC_FILE_NAME)) { // Check user input panel definitions IXMLElement xml = new XMLParser().parse(url); for (IXMLElement userPanelDef : xml.getChildrenNamed(UserInputPanel.NODE_ID)) { String userPanelId = xmlCompilerHelper.requireAttribute(userPanelDef, "id"); if (userInputPanelIds == null) { userInputPanelIds = new HashSet<String>(); } if (!userInputPanelIds.add(userPanelId)) { assertionHelper.parseError(xml, "Resource " + UserInputPanel.SPEC_FILE_NAME + ": Duplicate user input panel identifier '" + userPanelId + "'"); } } } } notifyCompilerListener("addResources", CompilerListener.END, data); }