Example usage for java.io OutputStreamWriter flush

List of usage examples for java.io OutputStreamWriter flush

Introduction

In this page you can find the example usage for java.io OutputStreamWriter flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ?/*from   w w  w. jav a  2s  .  com*/
 *
 * @param filePath
 *            ?
 * @param fileContent
 *            ?
 * @return ?truefalse
 */
public static boolean writeFileByString(String filePath, String fileContent) {
    if (null == filePath || null == fileContent) {
        return false;
    }
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath));
        outputStreamWriter.write(fileContent, 0, fileContent.length());
        outputStreamWriter.flush();
        outputStreamWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
        XLog.d(CLASS_NAME, e.getMessage());
        return false;
    }
    return true;
}

From source file:com.ieasy.basic.util.file.FileUtils.java

public static void WriteJSON(String outPath, String perfix, Object obj) {
    try {/* w w  w .j a  v a 2s .  c o  m*/
        String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss");
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outPath), "UTF-8");
        out.write((null != perfix ? perfix : "") + json);
        out.flush();
        out.close();
    } catch (IOException e) {
        logger.error("?JSON" + e.getMessage());
    }
    logger.debug("?JSON-->" + outPath);
}

From source file:org.opendatakit.survey.android.provider.SubmissionProvider.java

/**
 * This method actually writes the JSON appName-relative manifest to disk.
 *
 * @param payload/*from  ww  w  .j av a2  s  .co m*/
 * @param path
 * @return
 */
private static boolean exportFile(String payload, File outputFilePath, WebLogger log) {
    // write xml file
    FileOutputStream os = null;
    OutputStreamWriter osw = null;
    try {
        os = new FileOutputStream(outputFilePath, false);
        osw = new OutputStreamWriter(os, CharEncoding.UTF_8);
        osw.write(payload);
        osw.flush();
        osw.close();
        return true;

    } catch (IOException e) {
        log.e(t, "Error writing file");
        e.printStackTrace();
        try {
            osw.close();
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return false;
    }
}

From source file:mitm.common.tools.SMIME.java

private static MimeMessage loadp7m(String filename, boolean binary) throws Exception {
    File p7mFile = new File(filename);

    p7mFile = p7mFile.getAbsoluteFile();

    FileInputStream fis = new FileInputStream(p7mFile);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    OutputStreamWriter sw = new OutputStreamWriter(bos);

    sw.append("Content-Type: application/pkcs7-mime; name=\"smime.p7m\"\r\n");
    sw.append("Content-Transfer-Encoding: base64\r\n");
    sw.append("\r\n\r\n");

    byte[] content = IOUtils.toByteArray(fis);

    if (binary) {
        content = Base64.encodeBase64Chunked(content);
    }//from   w ww. j  a va2 s .  c o  m

    String base64Content = MiscStringUtils.toAsciiString(content);

    sw.append(base64Content);

    sw.flush();

    MimeMessage message = MailUtils.byteArrayToMessage(bos.toByteArray());

    return message;
}

From source file:com.noshufou.android.su.util.Util.java

public static boolean writeDefaultStoreFile(Context context) {
    File storedDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + "stored");
    storedDir.mkdirs();/*from w w w  . ja  v a  2  s  .  c  o m*/
    File defFile = new File(storedDir.getAbsolutePath() + File.separator + "default");
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String action = prefs.getString(Preferences.AUTOMATIC_ACTION, "prompt");
    try {
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(defFile.getAbsolutePath()));
        out.write(action);
        out.write("\n");
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        Log.w(TAG, "Default file not written", e);
        return false;
    } catch (IOException e) {
        Log.w(TAG, "Default file not written", e);
        return false;
    }
    return true;
}

From source file:com.noshufou.android.su.util.Util.java

public static boolean writeOptionsFile(Context context) {
    File storedDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + "stored");
    storedDir.mkdirs();// w w w  .ja  v a  2  s .  co  m
    File optFile = new File(storedDir.getAbsolutePath() + File.separator + "options");
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String ownerMode = prefs.getString(Preferences.USER_MODE, "owner_only");
    try {
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(optFile.getAbsolutePath()));
        out.write(ownerMode);
        out.write("\n");
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        Log.w(TAG, "Options file not written", e);
        return false;
    } catch (IOException e) {
        Log.w(TAG, "Options file not written", e);
        return false;
    }
    return true;
}

From source file:org.csware.ee.utils.Tools.java

public static String GetDataByPost(String httpUrl, String parMap) {
    try {/*from  w w  w  . j ava 2  s.c  om*/
        URL url = new URL(httpUrl);// 
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestMethod("POST"); // ?
        connection.setRequestProperty("Accept", "application/json"); // ??
        connection.setRequestProperty("Content-Type", "application/json"); // ????
        connection.connect();
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8?
        out.append(parMap);
        out.flush();
        out.close();
        // ??
        int length = (int) connection.getContentLength();// ?
        InputStream is = connection.getInputStream();
        if (length != -1) {
            byte[] data = new byte[length];
            byte[] temp = new byte[1024];
            int readLen = 0;
            int destPos = 0;
            while ((readLen = is.read(temp)) > 0) {
                System.arraycopy(temp, 0, data, destPos, readLen);
                destPos += readLen;
            }
            String result = new String(data, "UTF-8"); // utf-8?
            return result;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("aa:  " + e.getMessage());
    }
    return "error"; // ?
}

From source file:org.apache.cocoon.util.NetUtils.java

/**
 * Encode a path as required by the URL specification (<a href="http://www.ietf.org/rfc/rfc1738.txt">
 * RFC 1738</a>). This differs from <code>java.net.URLEncoder.encode()</code> which encodes according
 * to the <code>x-www-form-urlencoded</code> MIME format.
 *
 * @param path the path to encode/*from  w w  w .  j  a va2s. co  m*/
 * @return the encoded path
 */
public static String encodePath(String path) {
    // stolen from org.apache.catalina.servlets.DefaultServlet ;)

    /**
     * Note: This code portion is very similar to URLEncoder.encode.
     * Unfortunately, there is no way to specify to the URLEncoder which
     * characters should be encoded. Here, ' ' should be encoded as "%20"
     * and '/' shouldn't be encoded.
     */

    int maxBytesPerChar = 10;
    StringBuffer rewrittenPath = new StringBuffer(path.length());
    ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
    OutputStreamWriter writer = null;
    try {
        writer = new OutputStreamWriter(buf, "UTF8");
    } catch (Exception e) {
        e.printStackTrace();
        writer = new OutputStreamWriter(buf);
    }

    for (int i = 0; i < path.length(); i++) {
        int c = path.charAt(i);
        if (safeCharacters.get(c)) {
            rewrittenPath.append((char) c);
        } else {
            // convert to external encoding before hex conversion
            try {
                writer.write(c);
                writer.flush();
            } catch (IOException e) {
                buf.reset();
                continue;
            }
            byte[] ba = buf.toByteArray();
            for (int j = 0; j < ba.length; j++) {
                // Converting each byte in the buffer
                byte toEncode = ba[j];
                rewrittenPath.append('%');
                int low = (toEncode & 0x0f);
                int high = ((toEncode & 0xf0) >> 4);
                rewrittenPath.append(hexadecimal[high]);
                rewrittenPath.append(hexadecimal[low]);
            }
            buf.reset();
        }
    }
    return rewrittenPath.toString();
}

From source file:cd.education.data.collector.android.utilities.EncryptionUtils.java

private static void writeSubmissionManifest(EncryptedFormInformation formInfo, File submissionXml,
        List<File> mediaFiles) throws EncryptionException {

    Document d = new Document();
    d.setStandalone(true);//from  ww  w .j  av  a2  s  . c o m
    d.setEncoding(UTF_8);
    Element e = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, DATA);
    e.setPrefix(null, XML_ENCRYPTED_TAG_NAMESPACE);
    e.setAttribute(null, ID, formInfo.formId);
    if (formInfo.formVersion != null) {
        e.setAttribute(null, VERSION, formInfo.formVersion);
    }
    e.setAttribute(null, ENCRYPTED, "yes");
    d.addChild(0, Node.ELEMENT, e);

    int idx = 0;
    Element c;
    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, BASE64_ENCRYPTED_KEY);
    c.addChild(0, Node.TEXT, formInfo.base64RsaEncryptedSymmetricKey);
    e.addChild(idx++, Node.ELEMENT, c);

    c = d.createElement(XML_OPENROSA_NAMESPACE, META);
    c.setPrefix("orx", XML_OPENROSA_NAMESPACE);
    {
        Element instanceTag = d.createElement(XML_OPENROSA_NAMESPACE, INSTANCE_ID);
        instanceTag.addChild(0, Node.TEXT, formInfo.instanceMetadata.instanceId);
        c.addChild(0, Node.ELEMENT, instanceTag);
    }
    e.addChild(idx++, Node.ELEMENT, c);
    e.addChild(idx++, Node.IGNORABLE_WHITESPACE, NEW_LINE);

    if (mediaFiles != null) {
        for (File file : mediaFiles) {
            c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, MEDIA);
            Element fileTag = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, FILE);
            fileTag.addChild(0, Node.TEXT, file.getName() + ".enc");
            c.addChild(0, Node.ELEMENT, fileTag);
            e.addChild(idx++, Node.ELEMENT, c);
            e.addChild(idx++, Node.IGNORABLE_WHITESPACE, NEW_LINE);
        }
    }

    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, ENCRYPTED_XML_FILE);
    c.addChild(0, Node.TEXT, submissionXml.getName() + ".enc");
    e.addChild(idx++, Node.ELEMENT, c);

    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, BASE64_ENCRYPTED_ELEMENT_SIGNATURE);
    c.addChild(0, Node.TEXT, formInfo.getBase64EncryptedElementSignature());
    e.addChild(idx++, Node.ELEMENT, c);

    FileOutputStream fout = null;
    OutputStreamWriter writer = null;
    try {
        fout = new FileOutputStream(submissionXml);
        writer = new OutputStreamWriter(fout, UTF_8);

        KXmlSerializer serializer = new KXmlSerializer();
        serializer.setOutput(writer);
        // setting the response content type emits the xml header.
        // just write the body here...
        d.writeChildren(serializer);
        serializer.flush();
        writer.flush();
        fout.getChannel().force(true);
        writer.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        String msg = "Error writing submission.xml for encrypted submission: "
                + submissionXml.getParentFile().getName();
        Log.e(t, msg);
        throw new EncryptionException(msg, ex);
    } finally {
        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(fout);
    }
}

From source file:javaapplication1.Prog.java

public void transferStuff(String obj, String user) throws MalformedURLException, IOException {

    URL object = new URL("http://localhost:8080/bankserver/users/" + user);
    HttpURLConnection con = (HttpURLConnection) object.openConnection();
    con.setDoOutput(true);/*from   w ww .  j  a  v a  2 s.  c  om*/
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Accept", "application/json");
    con.setRequestMethod("PUT");
    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
    wr.write(obj);
    wr.flush();
    wr.close();
    con.getInputStream();
}