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.extrahardmode.config.messages.MessageConfig.java

/**
 * Set the header of the file before writing to it with bukkit yaml implementation
 *
 * @param file file to write the header to
 *///from ww w  .j a  v a2 s. c o m
private void setHeader(File file) {
    try {
        //Write header to a new file
        ByteArrayOutputStream memStream = new ByteArrayOutputStream();
        OutputStreamWriter memWriter = new OutputStreamWriter(memStream, Charset.forName("UTF-8").newEncoder());
        String[] header = { "Messages sent by ExtraHardMode",
                "Messages are only sent for modules that are activated",
                "Modes (has to match exactly, ignores case)",
                "Disabled: Message won't be sent even if feature that would sent the message is active",
                "One_Time: Will be sent to every player only once",
                "Notification: Gets sent every time with a timeout to prevent spamming chat",
                "Tutorial: Sent a limited number of times and not displayed after 3 times",
                "Broadcast: Shown to whole server. Only few messages make sense to be broadcasted",
                "Variables:", "$ALLCAPS is a variable and will be filled in for some messages",
                "$PLAYER: Affected player", "$PLAYERS: If multiple players are affected",
                "$DEATH_MSG: Death message if someone dies", "$ITEMS: a player lost" };
        StringBuilder sb = new StringBuilder();
        sb.append(StringUtils.repeat("#", 100));
        sb.append("%n");
        for (String line : header) {
            sb.append('#');
            sb.append(StringUtils.repeat(" ", 100 / 2 - line.length() / 2 - 1));
            sb.append(line);
            sb.append(StringUtils.repeat(" ", 100 / 2 - line.length() / 2 - 1 - line.length() % 2));
            sb.append('#');
            sb.append(String.format("%n"));
        }
        sb.append(StringUtils.repeat("#", 100));
        sb.append(String.format("%n"));
        //String.format: %n as platform independent line seperator
        memWriter.write(sb.toString());
        memWriter.close();

        IoHelper.writeHeader(file, memStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.megahardcore.config.messages.MessageConfig.java

/**
 * Set the header of the file before writing to it with bukkit yaml implementation
 *
 * @param file file to write the header to
 *///from  w w  w  .ja v  a 2  s. co m
private void setHeader(File file) {
    try {
        //Write header to a new file
        ByteArrayOutputStream memStream = new ByteArrayOutputStream();
        OutputStreamWriter memWriter = new OutputStreamWriter(memStream, Charset.forName("UTF-8").newEncoder());
        String[] header = { "Messages sent by MegaHardCore",
                "Messages are only sent for modules that are activated",
                "Modes (has to match exactly, ignores case)",
                "Disabled: Message won't be sent even if feature that would sent the message is active",
                "One_Time: Will be sent to every player only once",
                "Notification: Gets sent every time with a timeout to prevent spamming chat",
                "Tutorial: Sent a limited number of times and not displayed after 3 times",
                "Broadcast: Shown to whole server. Only few messages make sense to be broadcasted",
                "Variables:", "$ALLCAPS is a variable and will be filled in for some messages",
                "$PLAYER: Affected player", "$PLAYERS: If multiple players are affected",
                "$DEATH_MSG: Death message if someone dies", "$ITEMS: a player lost" };
        StringBuilder sb = new StringBuilder();
        sb.append(StringUtils.repeat("#", 100));
        sb.append(System.lineSeparator());
        for (String line : header) {
            sb.append('#');
            sb.append(StringUtils.repeat(" ", 100 / 2 - line.length() / 2 - 1));
            sb.append(line);
            sb.append(StringUtils.repeat(" ", 100 / 2 - line.length() / 2 - 1 - line.length() % 2));
            sb.append('#');
            sb.append(String.format(System.lineSeparator()));
        }
        sb.append(StringUtils.repeat("#", 100));
        sb.append(String.format(System.lineSeparator()));
        //String.format: %n as platform independent line seperator
        memWriter.write(sb.toString());
        memWriter.close();

        IoHelper.writeHeader(file, memStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Send an SMS.//w w w .ja  va 2  s  .  c  om
 *
 * @param destinationNumber the destination number
 * @param txt the Text of the message. Messages longer than the allowed
 * character length will be split into multiple messages.
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String sendSMS(String destinationNumber, String txt) throws IOException {
    String out = "";
    String smsdata = "";

    smsdata += URLEncoder.encode("phoneNumber", enc) + "=" + URLEncoder.encode(destinationNumber, enc);
    smsdata += "&" + URLEncoder.encode("text", enc) + "=" + URLEncoder.encode(txt, enc);
    smsdata += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc);
    URL smsurl = new URL("https://www.google.com/voice/b/0/sms/send/");

    URLConnection smsconn = smsurl.openConnection();
    smsconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    smsconn.setRequestProperty("User-agent", USER_AGENT);

    smsconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(smsconn.getOutputStream());
    callwr.write(smsdata);
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(smsconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Send an SMS.//from w w w.j  a v a2  s. co  m
 *
 * @param destinationNumber the destination number
 * @param txt the Text of the message. Messages longer than the allowed
 * character length will be split into multiple messages.
 * @param id the Text of the message. Messages longer than the allowed
 * character length will be split into multiple messages.
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String sendSMS(String destinationNumber, String txt, String id) throws IOException {
    String out = "";
    String smsdata = "";
    smsdata += URLEncoder.encode("id", enc) + "=" + URLEncoder.encode(id, enc);
    smsdata += "&" + URLEncoder.encode("phoneNumber", enc) + "=" + URLEncoder.encode(destinationNumber, enc);
    smsdata += "&" + URLEncoder.encode("conversationId", enc) + "=" + URLEncoder.encode(id, enc);
    smsdata += "&" + URLEncoder.encode("text", enc) + "=" + URLEncoder.encode(txt, enc);
    smsdata += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc);
    System.out.println("smsdata: " + smsdata);

    URL smsurl = new URL("https://www.google.com/voice/b/0/sms/send/");

    URLConnection smsconn = smsurl.openConnection();
    smsconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    smsconn.setRequestProperty("User-agent", USER_AGENT);

    smsconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(smsconn.getOutputStream());
    callwr.write(smsdata);
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(smsconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Executes the enable/disable action with the provided url params.
 *
 * @param paraString the URL Parameters (encoded), ie ?auth=3248sdf7234&enable=0&phoneId=1&enable=1&phoneId=2&_rnr_se=734682ghdsf
 * @return the raw response of the disable action.
 * @throws IOException Signals that an I/O exception has occurred.
 *///from w w w  .  jav  a  2  s .c om
private String phonesEnableDisableApply(String paraString) throws IOException {
    String out = "";

    // POST /voice/call/connect/ outgoingNumber=[number to
    // call]&forwardingNumber=[forwarding
    // number]&subscriberNumber=undefined&remember=0&_rnr_se=[pull from
    // page]

    //
    if (PRINT_TO_CONSOLE)
        System.out.println(phoneEnableURLString);
    if (PRINT_TO_CONSOLE)
        System.out.println(paraString);
    URL requestURL = new URL(phoneEnableURLString);

    URLConnection conn = requestURL.openConnection();
    conn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    conn.setRequestProperty("User-agent", USER_AGENT);

    conn.setDoOutput(true);
    conn.setDoInput(true);

    OutputStreamWriter callwr = new OutputStreamWriter(conn.getOutputStream());
    callwr.write(paraString);
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;

}

From source file:com.techventus.server.voice.Voice.java

/**
 * Posts a settings change./*from  w w w  .j a  v a 2 s .  c  o m*/
 *
 * @param requestURL the request url
 * @param paraString the para string
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
private String postSettings(URL requestURL, String paraString) throws IOException {
    String out = "";
    HttpURLConnection conn = (HttpURLConnection) requestURL.openConnection();
    conn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    conn.setRequestProperty("User-agent", USER_AGENT);

    conn.setDoOutput(true);
    conn.setDoInput(true);

    OutputStreamWriter callwr = new OutputStreamWriter(conn.getOutputStream());
    callwr.write(paraString);
    callwr.flush();

    // Get the response
    conn.connect();
    int responseCode = conn.getResponseCode();
    if (PRINT_TO_CONSOLE)
        System.out.println(requestURL + " - " + conn.getResponseMessage());
    InputStream is;
    if (responseCode == 200) {
        is = conn.getInputStream();
    } else {
        is = conn.getErrorStream();
    }
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader callrd = new BufferedReader(isr);

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";
    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    if (PRINT_TO_CONSOLE)
        System.out.println(out);

    return out;
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Cancel a call that was just placed.//from  w  ww .j a  va 2 s . co m
 * 
 * @param originNumber
 *            the origin number
 * @param destinationNumber
 *            the destination number
 * @param phoneType
 *            the phone type
 * @return the string
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public String cancelCall(String originNumber, String destinationNumber, String phoneType) throws IOException {
    String out = "";
    String calldata = "";
    calldata += URLEncoder.encode("outgoingNumber", enc) + "=" + URLEncoder.encode("undefined", enc);
    calldata += "&" + URLEncoder.encode("forwardingNumber", enc) + "=" + URLEncoder.encode("undefined", enc);

    calldata += "&" + URLEncoder.encode("cancelType", enc) + "=" + URLEncoder.encode("C2C", enc);
    calldata += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc);
    // POST /voice/call/connect/ outgoingNumber=[number to
    // call]&forwardingNumber=[forwarding
    // number]&subscriberNumber=undefined&remember=0&_rnr_se=[pull from
    // page]
    URL callURL = new URL("https://www.google.com/voice/b/0/call/cancel/");

    URLConnection callconn = callURL.openConnection();
    callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    callconn.setRequestProperty("User-agent", USER_AGENT);

    callconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream());
    callwr.write(calldata);
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;

}

From source file:com.techventus.server.voice.Voice.java

/**
 * Enables/disables the call Announcement setting (general for all phones).
 *
 * @param announceCaller <br/>//from   w w  w.  j  a v  a 2  s  .  co m
 * true Announces caller's name and gives answering options <br/>
 * false Directly connects calls when phones are answered
 * @return the raw response of the disable action.
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String setCallPresentation(boolean announceCaller) throws IOException {
    String out = "";

    URL requestURL = new URL(generalSettingsURLString);
    /** 0 for enable, 1 for disable **/
    String announceCallerStr = "";

    if (announceCaller) {
        announceCallerStr = "0";
        if (PRINT_TO_CONSOLE)
            System.out.println("Turning caller announcement on.");
    } else {
        announceCallerStr = "1";
        if (PRINT_TO_CONSOLE)
            System.out.println("Turning caller announcement off.");
    }

    String paraString = "";
    paraString += URLEncoder.encode("directConnect", enc) + "=" + URLEncoder.encode(announceCallerStr, enc);
    paraString += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc);

    URLConnection conn = requestURL.openConnection();
    conn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    conn.setRequestProperty("User-agent", USER_AGENT);

    conn.setDoOutput(true);
    conn.setDoInput(true);

    OutputStreamWriter callwr = new OutputStreamWriter(conn.getOutputStream());
    callwr.write(paraString);
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";
    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Mark a Conversation with a known Message ID as read.
 *
 * @param msgID the msg id/*from   ww  w.ja va2 s.  c  o  m*/
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String markAsRead(String msgID) throws IOException {
    String out = "";
    StringBuffer calldata = new StringBuffer();

    // POST /voice/inbox/mark/ 
    // messages=[messageID]
    // &read=1
    // &_rnr_se=[pull from page]

    calldata.append("messages=");
    calldata.append(URLEncoder.encode(msgID, enc));
    calldata.append("&read=1");
    calldata.append("&_rnr_se=");
    calldata.append(URLEncoder.encode(rnrSEE, enc));

    URL callURL = new URL(markAsReadString);

    URLConnection callconn = callURL.openConnection();
    callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    callconn.setRequestProperty("User-agent", USER_AGENT);

    callconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream());

    callwr.write(calldata.toString());
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;
}