Example usage for java.io OutputStreamWriter write

List of usage examples for java.io OutputStreamWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:com.jiubang.core.util.HttpUtils.java

/**
 * Send an HTTP(s) request with POST parameters.
 * /*from w ww.  j a  va  2  s. c  o m*/
 * @param parameters
 * @param url
 * @throws UnsupportedEncodingException
 * @throws IOException
 * @throws KeyManagementException
 * @throws NoSuchAlgorithmException
 */
static void doPost(Map<?, ?> parameters, URL url)
        throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException {

    URLConnection cnx = getConnection(url);

    // Construct data
    StringBuilder dataBfr = new StringBuilder();
    Iterator<?> iKeys = parameters.keySet().iterator();
    while (iKeys.hasNext()) {
        if (dataBfr.length() != 0) {
            dataBfr.append('&');
        }
        String key = (String) iKeys.next();
        dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=')
                .append(URLEncoder.encode((String) parameters.get(key), "UTF-8"));
    }
    // POST data
    cnx.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream());
    Loger.d(LOG_TAG, "Posting crash report data");
    wr.write(dataBfr.toString());
    wr.flush();
    wr.close();

    Loger.d(LOG_TAG, "Reading response");
    BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream()));

    String line;
    while ((line = rd.readLine()) != null) {
        Loger.d(LOG_TAG, line);
    }
    rd.close();
}

From source file:org.acra.HttpUtils.java

/**
 * Send an HTTP(s) request with POST parameters.
 * //from   www .  j  ava2 s .  c o  m
 * @param parameters
 * @param url
 * @throws UnsupportedEncodingException
 * @throws IOException
 * @throws KeyManagementException
 * @throws NoSuchAlgorithmException
 */
static void doPost(Map<?, ?> parameters, URL url)
        throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException {

    URLConnection cnx = getConnection(url);

    // Construct data
    StringBuilder dataBfr = new StringBuilder();
    Iterator<?> iKeys = parameters.keySet().iterator();
    while (iKeys.hasNext()) {
        if (dataBfr.length() != 0) {
            dataBfr.append('&');
        }
        String key = (String) iKeys.next();
        dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=')
                .append(URLEncoder.encode((String) parameters.get(key), "UTF-8"));
    }
    // POST data
    cnx.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream());
    Log.d(LOG_TAG, "Posting crash report data");
    wr.write(dataBfr.toString());
    wr.flush();
    wr.close();

    Log.d(LOG_TAG, "Reading response");
    BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream()));

    String line;
    while ((line = rd.readLine()) != null) {
        Log.d(LOG_TAG, line);
    }
    rd.close();
}

From source file:de.flapdoodle.embed.process.io.file.Files.java

public static void write(String content, File output) throws IOException {
    FileOutputStream out = new FileOutputStream(output);
    OutputStreamWriter w = new OutputStreamWriter(out);

    try {/*from w  w  w .j a va  2s .c o m*/
        w.write(content);
        w.flush();
    } finally {
        out.close();
    }
}

From source file:com.ct855.util.HttpsClientUtil.java

public static String postUrl(String url, Map<String, String> params)
        throws IOException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException {
    //SSLContext??
    TrustManager[] trustAllCerts = new TrustManager[] { new MyX509TrustManager() };
    SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

    //SSLContextSSLSocketFactory
    SSLSocketFactory ssf = sslContext.getSocketFactory();
    String data = "";
    for (String key : params.keySet()) {
        data += "&" + URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(params.get(key), "UTF-8");
    }/*from   w ww .j a  v a 2s  .com*/
    data = data.substring(1);

    System.out.println("postUrl=>data:" + data);
    URL aURL = new java.net.URL(url);
    HttpsURLConnection aConnection = (HttpsURLConnection) aURL.openConnection();
    aConnection.setSSLSocketFactory(ssf);
    aConnection.setDoOutput(true);
    aConnection.setDoInput(true);
    aConnection.setRequestMethod("POST");
    OutputStreamWriter streamToAuthorize = new java.io.OutputStreamWriter(aConnection.getOutputStream());
    streamToAuthorize.write(data);
    streamToAuthorize.flush();
    streamToAuthorize.close();
    InputStream resultStream = aConnection.getInputStream();
    BufferedReader aReader = new java.io.BufferedReader(new java.io.InputStreamReader(resultStream));
    StringBuffer aResponse = new StringBuffer();
    String aLine = aReader.readLine();
    while (aLine != null) {
        aResponse.append(aLine + "\n");
        aLine = aReader.readLine();
    }
    resultStream.close();
    return aResponse.toString();
}

From source file:eu.swiec.bearballin.common.io.FileIO.java

@Deprecated
public static void writeStringtoFileSource(String fileName, String stringToWrite, String encoding)
        throws IOException {

    File outputFile = new File(fileName);

    OutputStream outStream = new FileOutputStream(outputFile);
    OutputStreamWriter osw = new OutputStreamWriter(outStream, encoding);

    osw.write(stringToWrite);
    osw.close();//from   ww  w.java 2  s  . co m
    outStream.close();
}

From source file:cc.vileda.sipgatesync.api.SipgateApi.java

public static String getToken(final String username, final String password) {
    try {/*  w  w  w.  ja  v  a  2 s . com*/
        final HttpURLConnection urlConnection = getConnection("/authorization/token");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        JSONObject request = new JSONObject();
        request.put("username", username);
        request.put("password", password);
        OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
        Log.d("SipgateApi", request.getString("username"));
        wr.write(request.toString());
        wr.flush();
        StringBuilder sb = new StringBuilder();
        int HttpResult = urlConnection.getResponseCode();
        if (HttpResult == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            br.close();
            Log.d("SipgateApi", "" + sb.toString());
            final JSONObject response = new JSONObject(sb.toString());
            return response.getString("token");
        } else {
            System.out.println(urlConnection.getResponseMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/** Write the text provided to a File.
 * //  www.j a  v  a2s .c o m
 * @param file the file to write to.
 * @param text the text to write.
 * @throws IOException
 */
public static void write(File file, String text) throws IOException {
    FileOutputStream out = null;
    try {
        file.getParentFile().mkdirs();
        file.delete();
        file.createNewFile();
        out = new FileOutputStream(file);
        OutputStreamWriter writer = new OutputStreamWriter(out);
        writer.write(text);
        writer.flush();
    } finally {
        try {
            out.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

From source file:com.pureinfo.srm.outlay.action.SearchCheckCodeAction.java

/**
 * //from  www .  j av  a  2 s. co m
 * @param strUrl
 * @param strPostRequest
 * @return
 */
public static String getPageContent(String strUrl, String strPostRequest) {
    //
    StringBuffer buffer = new StringBuffer();
    System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
    System.setProperty("sun.net.client.defaultReadTimeout", "5000");
    try {
        URL newUrl = new URL(strUrl);
        HttpURLConnection hConnect = (HttpURLConnection) newUrl.openConnection();
        //POST
        if (strPostRequest.length() > 0) {
            hConnect.setDoOutput(true);
            OutputStreamWriter out = new OutputStreamWriter(hConnect.getOutputStream());
            out.write(strPostRequest);
            out.flush();
            out.close();
        }
        //
        BufferedReader rd = new BufferedReader(new InputStreamReader(hConnect.getInputStream()));
        int ch;
        for (int length = 0; (ch = rd.read()) > -1; length++)
            buffer.append((char) ch);
        rd.close();
        hConnect.disconnect();
        return buffer.toString().trim();
    } catch (Exception e) {
        // return ":";
        return null;
    } finally {
        buffer.setLength(0);
    }
}

From source file:at.jku.rdfstats.hist.builder.HistogramCodec.java

public static void writeString(ByteArrayOutputStream stream, String string) {
    try {//from   w  w  w .  j a v  a2 s.  co m
        OutputStreamWriter out = new OutputStreamWriter(stream);
        if (string == null)
            out.write((int) EMPTY_STRING);
        else {
            out.write(string);
            out.write((int) END_OF_STRING);
        }
        out.flush();
    } catch (IOException e) {
        throw new RuntimeException("Unexpected error: cannot write String into ByteArrayOutputStream.", e);
    }
}

From source file:com.wareninja.opensource.common.wsrequest.HttpUtils.java

/**
 * Send an HTTP(s) request with POST parameters.
 * //from  w  w w  .  j  a v a  2 s.c  om
 * @param parameters
 * @param url
 * @throws UnsupportedEncodingException
 * @throws IOException
 * @throws KeyManagementException
 * @throws NoSuchAlgorithmException
 */
public static void doPost(Map<?, ?> parameters, URL url)
        throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException {

    URLConnection cnx = getConnection(url);

    // Construct data
    StringBuilder dataBfr = new StringBuilder();
    Iterator<?> iKeys = parameters.keySet().iterator();
    while (iKeys.hasNext()) {
        if (dataBfr.length() != 0) {
            dataBfr.append('&');
        }
        String key = (String) iKeys.next();
        dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=')
                .append(URLEncoder.encode((String) parameters.get(key), "UTF-8"));
    }
    // POST data
    cnx.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream());
    if (LOGGING.DEBUG)
        Log.d(LOG_TAG, "Posting crash report data");
    wr.write(dataBfr.toString());
    wr.flush();
    wr.close();

    if (LOGGING.DEBUG)
        Log.d(LOG_TAG, "Reading response");
    BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream()));

    String line;
    int linecount = 0;
    while ((line = rd.readLine()) != null) {
        linecount++;
        if (linecount <= 2) {
            if (LOGGING.DEBUG)
                Log.d(LOG_TAG, line);
        }
    }
    rd.close();
}