Example usage for java.io DataOutputStream writeBytes

List of usage examples for java.io DataOutputStream writeBytes

Introduction

In this page you can find the example usage for java.io DataOutputStream writeBytes.

Prototype

public final void writeBytes(String s) throws IOException 

Source Link

Document

Writes out the string to the underlying output stream as a sequence of bytes.

Usage

From source file:SecurityManagerTest.java

public static void main(String[] args) throws Exception {
    try {//w  w  w .ja  va 2  s .c o m
        System.setSecurityManager(new PasswordSecurityManager("Booga Booga"));
    } catch (SecurityException se) {
        System.err.println("SecurityManager already set!");
    }

    DataInputStream in = new DataInputStream(new FileInputStream("inputtext.txt"));
    DataOutputStream out = new DataOutputStream(new FileOutputStream("outputtext.txt"));
    String inputString;
    while ((inputString = in.readLine()) != null) {
        out.writeBytes(inputString);
        out.writeByte('\n');
    }
    in.close();
    out.close();
}

From source file:Naive.java

public static void main(String[] args) throws Exception {
    // Basic access authentication setup
    String key = "CHANGEME: YOUR_API_KEY";
    String secret = "CHANGEME: YOUR_API_SECRET";
    String version = "preview1"; // CHANGEME: the API version to use
    String practiceid = "000000"; // CHANGEME: the practice ID to use

    // Find the authentication path
    Map<String, String> auth_prefix = new HashMap<String, String>();
    auth_prefix.put("v1", "oauth");
    auth_prefix.put("preview1", "oauthpreview");
    auth_prefix.put("openpreview1", "oauthopenpreview");

    URL authurl = new URL("https://api.athenahealth.com/" + auth_prefix.get(version) + "/token");

    HttpURLConnection conn = (HttpURLConnection) authurl.openConnection();
    conn.setRequestMethod("POST");

    // Set the Authorization request header
    String auth = Base64.encodeBase64String((key + ':' + secret).getBytes());
    conn.setRequestProperty("Authorization", "Basic " + auth);

    // Since this is a POST, the parameters go in the body
    conn.setDoOutput(true);/*from   ww w .  j  a v  a2s .  c  o m*/
    String contents = "grant_type=client_credentials";
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(contents);
    wr.flush();
    wr.close();

    // Read the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = rd.readLine()) != null) {
        sb.append(line);
    }
    rd.close();

    // Decode from JSON and save the token for later
    String response = sb.toString();
    JSONObject authorization = new JSONObject(response);
    String token = authorization.get("access_token").toString();

    // GET /departments
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("limit", "1");

    // Set up the URL, method, and Authorization header
    URL url = new URL("https://api.athenahealth.com/" + version + "/" + practiceid + "/departments" + "?"
            + urlencode(params));
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Authorization", "Bearer " + token);

    rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    sb = new StringBuilder();
    while ((line = rd.readLine()) != null) {
        sb.append(line);
    }
    rd.close();

    response = sb.toString();
    JSONObject departments = new JSONObject(response);
    System.out.println(departments.toString());

    // POST /appointments/{appointmentid}/notes
    params = new HashMap<String, String>();
    params.put("notetext", "Hello from Java!");

    url = new URL("https://api.athenahealth.com/" + version + "/" + practiceid + "/appointments/1/notes");
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Bearer " + token);

    // POST parameters go in the body
    conn.setDoOutput(true);
    contents = urlencode(params);
    wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(contents);
    wr.flush();
    wr.close();

    rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    sb = new StringBuilder();
    while ((line = rd.readLine()) != null) {
        sb.append(line);
    }
    rd.close();

    response = sb.toString();
    JSONObject note = new JSONObject(response);
    System.out.println(note.toString());
}

From source file:com.adobe.aem.demo.Analytics.java

public static void main(String[] args) {

    String hostname = null;/*from  w  w w  .java  2s  .c o m*/
    String url = null;
    String eventfile = null;

    // Command line options for this tool
    Options options = new Options();
    options.addOption("h", true, "Hostname");
    options.addOption("u", true, "Url");
    options.addOption("f", true, "Event data file");
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("u")) {
            url = cmd.getOptionValue("u");
        }

        if (cmd.hasOption("f")) {
            eventfile = cmd.getOptionValue("f");
        }

        if (cmd.hasOption("h")) {
            hostname = cmd.getOptionValue("h");
        }

        if (eventfile == null || hostname == null || url == null) {
            System.out.println("Command line parameters: -h hostname -u url -f path_to_XML_file");
            System.exit(-1);
        }

    } catch (ParseException ex) {

        logger.error(ex.getMessage());

    }

    URLConnection urlConn = null;
    DataOutputStream printout = null;
    BufferedReader input = null;
    String u = "http://" + hostname + "/" + url;
    String tmp = null;
    try {

        URL myurl = new URL(u);
        urlConn = myurl.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        printout = new DataOutputStream(urlConn.getOutputStream());

        String xml = readFile(eventfile, StandardCharsets.UTF_8);
        printout.writeBytes(xml);
        printout.flush();
        printout.close();

        input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

        logger.debug(xml);
        while (null != ((tmp = input.readLine()))) {
            logger.debug(tmp);
        }
        printout.close();
        input.close();

    } catch (Exception ex) {

        logger.error(ex.getMessage());

    }

}

From source file:Main.java

public static void remountSystemRO() throws IOException {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    os.writeBytes(CMD_REMOUNT_RO + "\n");
    os.writeBytes("exit\n");
    os.flush();/*  w  ww . j  a  va 2s  .  c  om*/

}

From source file:Main.java

static void runCommandWithRoot(String command) {
    try {//from  ww  w.  j  a v  a2  s  .  com
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        os.writeBytes(command);
        os.writeBytes("exit\n");
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Process execRuntimeProcess(String cmd) {
    Runtime localRuntime = Runtime.getRuntime();
    try {//from  www.  j av a 2 s  . co m
        Process pro = localRuntime.exec("su");
        DataOutputStream out = new DataOutputStream(pro.getOutputStream());
        out.writeBytes(cmd + " \n");
        return pro;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static boolean fileWriteOneLine(String fname, String value, Process su) {
    try {/*from   w  ww.  ja  v  a 2  s. c om*/
        DataOutputStream out = new DataOutputStream(su.getOutputStream());
        out.writeBytes("echo " + value + " > " + fname + "\n");
    } catch (Exception e) {
        String Error = "Error writing to " + fname + ". Exception: ";
        Log.e(TAG, Error, e);
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean restartThermald(Process su) {
    try {/*www.jav  a 2 s  .c  o  m*/
        DataOutputStream out = new DataOutputStream(su.getOutputStream());
        out.writeBytes("stop thermald\n");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
        }
        out.writeBytes("start thermald\n");
    } catch (Exception e) {
        String Error = "Error restarting thermald. Exception: ";
        Log.e(TAG, Error, e);
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * Run command as root./*from   w ww . j ava  2s.  c  o  m*/
 * 
 * @param command
 * @return true, if command was successfully executed
 */
private static boolean runAsRoot(final String command) {
    try {

        Process pro = Runtime.getRuntime().exec("su");
        DataOutputStream outStr = new DataOutputStream(pro.getOutputStream());

        outStr.writeBytes(command);
        outStr.writeBytes("\nexit\n");
        outStr.flush();

        int retval = pro.waitFor();

        return (retval == 0);

    } catch (Exception e) {

        return false;

    }
}

From source file:Main.java

public static void RunRootCmd(String cmd) {
    try {//from w  ww .  j a v a  2 s.  c  o  m
        //
        // run our command using 'su' to gain root
        //
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());

        outputStream.writeBytes(cmd + "\n");
        outputStream.flush();

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}