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.buglabs.dragonfly.util.WSHelper.java

protected static String post(URL url, String payload, Map props) throws IOException {
    String propstr = new String();

    for (Iterator i = props.keySet().iterator(); i.hasNext();) {
        String key = (String) i.next();
        propstr = propstr + URLEncoder.encode(key, "UTF-8") + "="
                + URLEncoder.encode((String) props.get(key), "UTF-8");
        if (i.hasNext()) {
            propstr = propstr + "&";
        }/*from ww w . java  2 s .  com*/
    }

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(propstr);
    osr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line, resp = new String("");
    while ((line = rd.readLine()) != null) {
        resp = resp + line + "\n";
    }
    osr.close();
    rd.close();

    return resp;
}

From source file:com.buglabs.dragonfly.util.WSHelper.java

protected static String post(URL url, Map props) throws IOException {
    String propstr = new String();

    for (Iterator i = props.keySet().iterator(); i.hasNext();) {
        String key = (String) i.next();
        propstr = propstr + URLEncoder.encode(key, "UTF-8") + "="
                + URLEncoder.encode((String) props.get(key), "UTF-8");
        if (i.hasNext()) {
            propstr = propstr + "&";
        }//from  ww w .j  a v  a 2s  .  c om
    }

    SSLUtils.verifyHost();

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(propstr);
    osr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line, resp = new String("");
    while ((line = rd.readLine()) != null) {
        resp = resp + line + "\n";
    }
    osr.close();
    rd.close();

    return resp;
}

From source file:controllers.base.Application.java

public static Result login(String redirectTo, boolean isGuest) {
    if (isGuest) {
        createWebSession();//  ww w  .ja  v a  2  s .c o m
    } else {
        String token = request().body().asFormUrlEncoded().get("token")[0];
        String apiKey = Play.application().configuration().getString("rpx.apiKey");
        String data;
        String response = null;
        try {
            data = String.format("token=%s&apiKey=%s&format=json", URLEncoder.encode(token, "UTF-8"),
                    URLEncoder.encode(apiKey, "UTF-8"));
            URL url = new URL("https://rpxnow.com/api/v2/auth_info");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.connect();
            OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            osw.write(data);
            osw.close();
            response = IOUtils.toString(conn.getInputStream());
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }

        JsonNode profile = Json.parse(response).path("profile");
        String identifier = profile.path("identifier").asText();

        WebUser user = WebUser.find.where().like("providerId", identifier).findUnique();

        if (user == null) {
            user = new WebUser().setProviderId(identifier).setProfile(Json.stringify(profile));
            user.save();
        }

        createWebSession(user);
    }

    return postLoginRedirect(redirectTo);
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Create new hero in database//  w  w  w.j a  v a  2s.c  om
 * @param name  hero name
 * @param race  hero race
 * @param xp    hero experience
 */
private static void createHero(String name, String race, String xp) {
    try {
        HeroDTO h = new HeroDTO();
        h.setName(name);
        h.setRace(race);
        h.setXp(Integer.parseInt(xp));
        JSONObject jsonObject = new JSONObject(h);

        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/hero/post");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(jsonObject.toString());
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        while (in.readLine() != null) {
        }
        System.out.println("New hero " + h.getName() + " was created.");
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when creating new hero");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Update hero in database//www  .  j a v  a 2s  .com
 * @param id    hero id
 * @param name  hero name
 * @param race  hero race
 * @param xp    hero experience
 */
private static void updateHero(String id, String name, String race, String xp) {
    try {
        HeroDTO h = new HeroDTO();
        h.setId(Long.parseLong(id));
        h.setName(name);
        h.setRace(race);
        h.setXp(Integer.parseInt(xp));
        JSONObject jsonObject = new JSONObject(h);

        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/hero/put");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(jsonObject.toString());
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while (in.readLine() != null) {
        }
        System.out.println("Updated hero with id: " + h.getId());
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when updating hero");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Create new role in database//from   ww w. j ava 2 s.c om
 * @param name  role id
 * @param description   role desc
 * @param energy    role energy
 * @param attack    role attack
 * @param defense   role defense
 */
private static void createRole(String name, String description, String energy, String attack, String defense) {
    try {
        RoleDTO r = new RoleDTO();
        r.setName(name);
        r.setDescription(description);
        r.setEnergy(Integer.parseInt(energy));
        r.setAttack(Integer.parseInt(attack));
        r.setDefense(Integer.parseInt(defense));
        JSONObject jsonObject = new JSONObject(r);

        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/role/post");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(jsonObject.toString());
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        while (in.readLine() != null) {
        }
        System.out.println("New role " + r.getName() + " was created.");
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when creating new role");
        System.out.println(e);
    }
}

From source file:com.pa165.ddtroops.console.client.Application.java

/**
 * Update role in database/*from w  w  w.  ja  v  a 2s  .c o m*/
 * @param id    role id
 * @param name  role name
 * @param description   role desc
 * @param energy    role energy
 * @param attack    role attack
 * @param defense   role defense
 */
private static void updateRole(String id, String name, String description, String energy, String attack,
        String defense) {
    try {
        RoleDTO r = new RoleDTO();
        r.setId(Long.parseLong(id));
        r.setName(name);
        r.setDescription(description);
        r.setEnergy(Integer.parseInt(energy));
        r.setAttack(Integer.parseInt(attack));
        r.setDefense(Integer.parseInt(defense));
        JSONObject jsonObject = new JSONObject(r);

        URL url = new URL("http://localhost:8080/pa165/rest-jersey-server/role/put");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write(jsonObject.toString());
        out.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while (in.readLine() != null) {
        }
        System.out.println("Updated role with id: " + r.getId());
        System.out.println();
        in.close();
    } catch (Exception e) {
        System.out.println("\nError when updating role");
        System.out.println(e);
    }
}

From source file:be.fedict.eidviewer.lib.file.imports.Version35CSVFile.java

public static void X509CertToCSV(X509Certificate certificate, String label, OutputStreamWriter writer)
        throws Exception {
    writer.write(String.format("%s;1;%s;;", label, X509Utilities.eidBase64Encode(certificate.getEncoded())));
}

From source file:eu.digitisation.idiomaident.utils.ExtractTestSamples.java

private static void extractSamples(int numSamples, File inFolder, File outFile) {
    FileOutputStream fileOS = null;
    OutputStreamWriter outSWriter = null;

    try {/* w w  w .j  a v a2 s . com*/
        fileOS = new FileOutputStream(outFile);
        outSWriter = new OutputStreamWriter(fileOS, Charset.forName("UTF8"));

        int num = 0;
        while (num < numSamples) {
            String sample = nextSample(inFolder);

            if (sample == null)
                continue;

            outSWriter.write(sample);
            num++;
            System.out.println("Generating " + num + " of " + numSamples + " samples");
        }

        outSWriter.flush();

    } catch (IOException ex) {
        System.out.println(ex.toString());
    } finally {
        try {
            if (outSWriter != null) {
                outSWriter.close();
            }
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }

}

From source file:de.domjos.schooltools.helper.Helper.java

public static boolean writeStringToFile(String data, String path, Context context) {
    try {/*from  w w  w .ja  v  a  2 s  . c o  m*/
        path = path.replace(" ", "%20");
        String[] spl = path.split("/");
        String fileName = spl[spl.length - 1];

        File directory = new File(path.replace(fileName, ""));
        if (!directory.exists()) {
            if (!directory.mkdirs()) {
                return false;
            }
        }

        File newFile = new File(path);
        if (newFile.exists()) {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(newFile),
                    Charset.defaultCharset());
            outputStreamWriter.write(data);
            outputStreamWriter.close();
            return true;
        } else {
            if (newFile.createNewFile()) {
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(newFile),
                        Charset.defaultCharset());
                outputStreamWriter.write(data);
                outputStreamWriter.close();
                return true;
            }
        }
    } catch (Exception ex) {
        Helper.printException(context, ex);
    }
    return false;
}