Example usage for java.io DataOutputStream DataOutputStream

List of usage examples for java.io DataOutputStream DataOutputStream

Introduction

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

Prototype

public DataOutputStream(OutputStream out) 

Source Link

Document

Creates a new data output stream to write data to the specified underlying output stream.

Usage

From source file:Main.java

public static int getSuVersionCode() {
    Process process = null;//from  w w w.  j a v  a 2 s  .c o  m
    String inLine = null;

    try {
        process = Runtime.getRuntime().exec("sh");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        BufferedReader is = new BufferedReader(
                new InputStreamReader(new DataInputStream(process.getInputStream())), 64);
        os.writeBytes("su -v\n");

        // We have to hold up the thread to make sure that we're ready to read
        // the stream, using increments of 5ms makes it return as quick as
        // possible, and limiting to 1000ms makes sure that it doesn't hang for
        // too long if there's a problem.
        for (int i = 0; i < 400; i++) {
            if (is.ready()) {
                break;
            }
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                Log.w(TAG, "Sleep timer got interrupted...");
            }
        }
        if (is.ready()) {
            inLine = is.readLine();
            if (inLine != null && Integer.parseInt(inLine.substring(0, 1)) > 2) {
                inLine = null;
                os.writeBytes("su -V\n");
                inLine = is.readLine();
                if (inLine != null) {
                    return Integer.parseInt(inLine);
                }
            } else {
                return 0;
            }
        } else {
            os.writeBytes("exit\n");
        }
    } catch (IOException e) {
        Log.e(TAG, "Problems reading current version.", e);
        return 0;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
    return 0;
}

From source file:com.ant.myteam.gcm.POST2GCM.java

public static void post(String apiKey, Content content) {

    try {//ww  w . ja v a  2  s. c om

        // 1. URL
        URL url = new URL("https://android.googleapis.com/gcm/send");

        // 2. Open connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // 3. Specify POST method
        conn.setRequestMethod("POST");

        // 4. Set the headers
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);

        conn.setDoOutput(true);

        // 5. Add JSON data into POST request body

        //`5.1 Use Jackson object mapper to convert Contnet object into JSON
        ObjectMapper mapper = new ObjectMapper();

        // 5.2 Get connection output stream
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

        // 5.3 Copy Content "JSON" into
        mapper.writeValue(wr, content);

        // 5.4 Send the request
        wr.flush();

        // 5.5 close
        wr.close();

        // 6. Get the response
        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // 7. Print result
        System.out.println(response.toString());

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Process runSuCommandAsync_miracle(Context context, String command) throws IOException {
    DataOutputStream fout_miracle = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_miracle, 0));
    fout_miracle.writeBytes(command);//  w w  w.  j av a2 s .  c o  m
    fout_miracle.close();

    String[] args_miracle = new String[] { "su", "-c",
            ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME_miracle };
    Process proc_miracle = Runtime.getRuntime().exec(args_miracle);
    return proc_miracle;
}

From source file:Main.java

public static void writeContatctInfo(String accountId, Vector contactInfo, File contactInfoFile)
        throws IOException {
    contactInfoFile.getParentFile().mkdirs();
    FileOutputStream contactFileOutputStream = new FileOutputStream(contactInfoFile);
    DataOutputStream out = new DataOutputStream(contactFileOutputStream);
    out.writeUTF((String) contactInfo.get(0));
    out.writeInt(((Integer) (contactInfo.get(1))).intValue());
    for (int i = 2; i < contactInfo.size(); ++i) {
        out.writeUTF((String) contactInfo.get(i));
    }//from  ww w.jav a 2  s.c  om
    out.close();
}

From source file:com.moz.fiji.hive.utils.ByteWritable.java

public static byte[] serialize(Writable writable) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DataOutputStream dataOut = null;
    try {/*from  w  w  w  . ja v  a 2 s  .  co  m*/
        dataOut = new DataOutputStream(out);
        writable.write(dataOut);
        return out.toByteArray();
    } finally {
        IOUtils.closeQuietly(dataOut);
    }
}

From source file:com.fullhousedev.globalchat.bukkit.PluginMessageManager.java

public static void sendRawMessage(String subChannel, String serverName, byte[] message, Plugin pl) {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(b);

    try {/*w  ww  . ja va  2  s  . c o m*/
        out.writeUTF("Forward");
        out.writeUTF(serverName);
        out.writeUTF(subChannel);

        out.writeShort(message.length);
        out.write(message);
    } catch (IOException ex) {
        Logger.getLogger(GlobalChat.class.getName()).log(Level.SEVERE, null, ex);
    }

    Player p = Bukkit.getOnlinePlayers()[0];

    p.sendPluginMessage(pl, "BungeeCord", b.toByteArray());
}

From source file:MainClass.java

public void run() {
    try {/*from  www . j a  v  a2 s. c  o m*/
        Socket socket = new Socket("127.0.0.1", 2000);

        DataInputStream in = new DataInputStream(socket.getInputStream());
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());

        while (true) {
            System.out.print("Enter response: ");
            String response = console.readLine();
            out.writeUTF(response);

            String message = in.readUTF();
            System.out.println(message);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String upLoad(File file, String RequestURL) {
    String BOUNDER = UUID.randomUUID().toString();
    String PREFIX = "--";
    String END = "/r/n";

    try {/*from ww w . j av a 2s . c  om*/
        URL url = new URL(RequestURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(TIME_OUT);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Charset", CHARSET);
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cotent-Type", CONTENT_TYPE + ";boundary=" + BOUNDER);

        if (file != null) {
            OutputStream outputStream = connection.getOutputStream();

            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            StringBuffer sb = new StringBuffer();
            sb.append(PREFIX);
            sb.append(BOUNDER + END);
            dataOutputStream.write(sb.toString().getBytes());
            InputStream in = new FileInputStream(file);
            byte[] b = new byte[1024];
            int l = 0;
            while ((l = in.read()) != -1) {
                outputStream.write(b, 0, l);
            }
            in.close();
            dataOutputStream.write(END.getBytes());
            dataOutputStream.write((PREFIX + BOUNDER + PREFIX + END).getBytes());
            dataOutputStream.flush();

            int i = connection.getResponseCode();
            if (i == 200) {
                return SUCCESS;
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return FALIURE;

}

From source file:net.datenwerke.transloader.primitive.WrapperConverter.java

private static byte[] convert(Object wrapper) throws IOException {
    ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();
    DataOutputStream dataStream = new DataOutputStream(arrayStream);
    write(wrapper, dataStream);//from  w w  w.  j  a va  2  s  .co  m
    return arrayStream.toByteArray();
}

From source file:com.zf.util.Post_NetNew.java

public static String pn(Map<String, String> pams) throws Exception {
    if (null == pams) {
        return "";
    }/*from   ww  w .j av a  2 s  . c  o m*/
    String strtmp = "url";
    URL url = new URL(pams.get(strtmp));
    pams.remove(strtmp);
    strtmp = "body";
    String body = pams.get(strtmp);
    pams.remove(strtmp);
    strtmp = "POST";
    if (StringUtils.isEmpty(body))
        strtmp = "GET";
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setRequestMethod(strtmp);
    for (String pam : pams.keySet()) {
        httpConn.setRequestProperty(pam, pams.get(pam));
    }
    if ("POST".equals(strtmp)) {
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
        dos.writeBytes(body);
        dos.flush();
    }
    int resultCode = httpConn.getResponseCode();
    StringBuilder sb = new StringBuilder();
    sb.append(resultCode).append("\n");
    String readLine;
    InputStream stream;
    try {
        stream = httpConn.getInputStream();
    } catch (Exception ignored) {
        stream = httpConn.getErrorStream();
    }
    try {
        BufferedReader responseReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        while ((readLine = responseReader.readLine()) != null) {
            sb.append(readLine).append("\n");
        }
    } catch (Exception ignored) {
    }

    return sb.toString();
}