Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import java.io.DataOutputStream;
import java.io.DataInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;

public class Main {
    public static void CopyResFile(InputStream resStream, String sTarget) {
        File tmpFile = WriteStreamToFile(resStream);
        if (tmpFile != null) {
            RunSystemCmd(String.format("busybox cp %s %s; chmod 777 %s", tmpFile.getPath(), sTarget, sTarget));
            tmpFile.delete();
        }
    }

    public static File WriteStreamToFile(InputStream resStream) {
        try {
            byte[] bytes = new byte[resStream.available()];
            File tmpFile = File.createTempFile("z4-", ".tmp");
            tmpFile.deleteOnExit();
            DataInputStream dis = new DataInputStream(resStream);
            dis.readFully(bytes);
            FileOutputStream foutStream = new FileOutputStream(tmpFile.getPath());
            foutStream.write(bytes);
            foutStream.close();
            dis.close();
            return tmpFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void RunSystemCmd(String cmd) {
        String command = new String(
                "busybox mount -o remount,rw,llw,check=no /system || busybox mount -o remount,rw /system; " + cmd
                        + ";" + "busybox mount -o remount,ro,llw,check=no /system");
        RunRootCmd(command);
    }

    public static void RunRootCmd(String cmd) {
        try {
            //
            // 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();
        }
    }
}