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:Main.java

public static void wirteStringToStream(OutputStream out, String src) throws IOException {
    final OutputStreamWriter writer = new OutputStreamWriter(out);
    writer.write(src);//from   ww w  .  j a v  a2 s.  c om
    writer.flush();
}

From source file:Main.java

public static void reboot() {
    try {/*  w  ww  . jav a 2 s  .c  o m*/
        Process su = Runtime.getRuntime().exec("su");
        OutputStreamWriter out = new OutputStreamWriter(su.getOutputStream());
        out.write("reboot");
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Method - saveMessage//from w  ww  .j av  a 2s.c om
 * 
 * Description - Saves messages to the file by either appending or rewriting, used by any activity that needs to save the messages
 * 
 * @param ctx - Context of the Application that called the function
 * @param message - The messages to save to the file, most often a long String of several files
 * @param mode - Very likely either Context.MODE_APPEND or Context.MODE_PRIVATE for appending or rewriting respectively
 */

public static void saveMessage(Context ctx, String message, int mode) {
    try {

        // Creating objects to write to the file
        FileOutputStream fos = ctx.openFileOutput("messages.dat", mode);
        OutputStreamWriter osw = new OutputStreamWriter(fos);

        osw.write(message); // Writing to the file
        osw.flush(); // Making sure all characters have been written

        // Closing objects after writing has finished
        osw.close();
        fos.close();

    } catch (FileNotFoundException e) {
        return;
    } catch (IOException e) {
        return;
    }
}

From source file:Main.java

public static synchronized void writeToFile(String rootPath, String filename, String data) {

    File file = new File(rootPath);
    if (!file.exists()) {
        file.mkdirs();//from  w  ww  .  jav a  2 s.  c o m

    }

    FileOutputStream fOut = null;
    try {

        File savedfile = new File(rootPath + filename);
        savedfile.createNewFile();//create file if not exists
        fOut = new FileOutputStream(savedfile, true); //append content to the end of file
        OutputStreamWriter outWriter = new OutputStreamWriter(fOut);

        outWriter.write(data);

        fOut.flush();
        outWriter.flush();

        fOut.close();
        outWriter.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

From source file:Main.java

public static File writeLog(String root, String filename) {
    StringBuilder log = new StringBuilder();
    try {// ww w. j ava2s.  co m
        Process process = Runtime.getRuntime().exec("logcat -d");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            log.append(line).append("\n");
        }
    } catch (IOException e) {
        log.append(e.toString());
    }

    File file = new File(root, filename);

    try {
        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        // Write the string to the file
        osw.write(log.toString());
        osw.flush();
        osw.close();

    } catch (Exception e) {
        Log.e(TAG, String.format("Failed to write log to [%s]", file), e);
    }
    return file;
}

From source file:lapispaste.Main.java

private static void paster(Map<String, String> pastemap, StringBuffer pdata) {
    try {//from   ww w  . jav  a  2  s. c  o  m
        URL url = new URL("http://paste.linux-sevenler.org/index.php");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // set connection 'writeable'
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");

        // construct the data...
        String data;
        String pdataString = pdata.toString();
        data = URLEncoder.encode("language", "ISO-8859-9") + "="
                + URLEncoder.encode(pastemap.get("format"), "ISO-8859-9");
        data += "&" + URLEncoder.encode("source", "ISO-8859-9") + "="
                + URLEncoder.encode(pdataString, "ISO-8859-9");
        data += "&" + URLEncoder.encode("submit", "ISO-8859-9") + "="
                + URLEncoder.encode(" Kaydet ", "ISO-8859-9");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // get new url where the paste is
        conn.getInputStream();
        System.out.println(conn.getURL());
        conn.disconnect();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:de.mas.telegramircbot.utils.images.ImgurUploader.java

public static String uploadImageAndGetLink(String clientID, byte[] image) throws IOException {
    URL url;// ww w.j ava2s  .  c  o m
    url = new URL(Settings.IMGUR_API_URL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String dataImage = Base64.getEncoder().encodeToString(image);
    String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(dataImage, "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    wr.close();
    rd.close();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ImgurResponse.class, new ImgurResponseDeserializer());
    Gson gson = gsonBuilder.create();

    // The JSON data
    try {
        ImgurResponse response = gson.fromJson(stb.toString(), ImgurResponse.class);
        return response.getLink();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return stb.toString();
}

From source file:nz.co.lolnet.lolnetachievements.Achievements.Achievements.java

public static void awardPlayerAchievement(String playername, String achievementname)
        throws MalformedURLException, IOException, ParseException {

    JSONObject data = new JSONObject();
    data.put("serverName", Config.SERVER_NAME);
    data.put("serverKey", Config.SERVER_HASH);
    data.put("achievementName", LolnetAchievements.convertAchievementName(achievementname));
    data.put("playerName", playername.toLowerCase());

    if (Config.DEBUG_MODE) {
        System.out.println(data.toJSONString());
    }//from   www  .  j av  a  2  s  .c o m

    URL url = new URL("https://api-lnetachievements.rhcloud.com/api/achievements");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    conn.setConnectTimeout(API_TIMEOUT);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data.toJSONString());
    wr.flush();
    wr.close();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String temp = rd.readLine();
    if (Config.DEBUG_MODE) {
        System.out.println(temp);
    }
    //JSONObject object = (JSONObject) new JSONParser().parse(temp);
    //System.out.println("");
    rd.close();
}

From source file:com.fluidops.iwb.provider.GoogleRefineProvider.java

/**
 * utility to simplify doing POST requests
 * /* w  ww.  jav  a 2 s.com*/
 * @param url         URL to post to
 * @param parameter      map of parameters to post
 * @return            URLConnection is a state where post parameters have been sent
 * @throws IOException
 * 
 * TODO: move to util class
 */
public static URLConnection doPost(URL url, Map<String, String> parameter) throws IOException {
    URLConnection con = url.openConnection();
    con.setDoOutput(true);

    StringBuilder params = new StringBuilder();
    for (Entry<String, String> entry : parameter.entrySet())
        params.append(StringUtil.urlEncode(entry.getKey())).append("=")
                .append(StringUtil.urlEncode(entry.getValue())).append("&");

    // Send data
    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
    wr.write(params.toString());
    wr.flush();
    wr.close();

    return con;
}

From source file:jp.go.nict.langrid.foundation.servlet.ResponseProcessor.java

static ServiceResponse processTemplate(LangridException exception, String hostName, OutputStream out)
        throws IOException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("escapeUtils", StringEscapeUtils.class);
    map.put("exception", exception);
    map.put("hostName", hostName);
    String cn = exception.getClass().getName();
    map.put("nsSuffix", "");

    List<Attribute> properties = new ArrayList<Attribute>();
    for (Method m : exception.getClass().getDeclaredMethods()) {
        String methodName = m.getName();
        if (!methodName.startsWith("get"))
            continue;

        try {/*from   ww  w .  j  a v  a  2 s.c  om*/
            String name = m.getName().substring("get".length());
            name = name.substring(0, 1).toLowerCase() + name.substring(1);
            String value = (String) m.invoke(exception);
            if (value == null)
                continue;
            properties.add(new Attribute(name, value));
        } catch (ClassCastException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
    }
    map.put("properties", properties);

    if (cn.startsWith("jp.go.nict.langrid.service_1_2.")) {
        String prefix = cn.substring("jp.go.nict.langrid.service_1_2.".length());
        int i = prefix.lastIndexOf(".");
        if (i != -1) {
            map.put("nsSuffix", prefix.substring(0, i).replace(".", "/") + "/");
        }
    }
    CountingOutputStream co = new CountingOutputStream(out);
    OutputStreamWriter w = new OutputStreamWriter(co);
    exceptionTemplate.make(map).writeTo(w);
    w.flush();

    return new ServiceResponse(500, co.getCount(), "soapenv:Server.userException", exception.getDescription());
}