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:com.splout.db.dnode.TCPStreamer.java

/**
 * This main method can be used for testing the TCP interface directly to a
 * local DNode. Will ask for protocol input from Stdin and print output to
 * Stdout//  w  w w  .j  ava2  s  . c om
 */
public static void main(String[] args) throws UnknownHostException, IOException, SerializationException {
    SploutConfiguration config = SploutConfiguration.get();
    Socket clientSocket = new Socket("localhost", config.getInt(DNodeProperties.STREAMING_PORT));

    DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
    DataOutputStream outToServer = new DataOutputStream(
            new BufferedOutputStream(clientSocket.getOutputStream()));

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter tablespace: ");
    String tablespace = reader.readLine();

    System.out.println("Enter version number: ");
    long versionNumber = Long.parseLong(reader.readLine());

    System.out.println("Enter partition: ");
    int partition = Integer.parseInt(reader.readLine());

    System.out.println("Enter query: ");
    String query = reader.readLine();

    outToServer.writeUTF(tablespace);
    outToServer.writeLong(versionNumber);
    outToServer.writeInt(partition);
    outToServer.writeUTF(query);

    outToServer.flush();

    byte[] buffer = new byte[0];
    boolean read;
    do {
        read = false;
        int nBytes = inFromServer.readInt();
        if (nBytes > 0) {
            buffer = new byte[nBytes];
            int inRead = inFromServer.read(buffer);
            if (inRead > 0) {
                Object[] res = ResultSerializer.deserialize(ByteBuffer.wrap(buffer), Object[].class);
                read = true;
                System.out.println(Arrays.toString(res));
            }
        }
    } while (read);

    clientSocket.close();
}

From source file:Main.java

public static void writeBin(String fileName, byte[] data) {
    try {/*from  ww w. j ava2s  . com*/
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
        for (byte b : data) {
            out.writeByte(b);
        }
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void killProcess(int pid) {
    Process sh = null;/*from ww w .ja v a  2s  . co  m*/
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        final String Command = "kill -9 " + pid + "\n";
        os.writeBytes(Command);
        os.flush();
        sh.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void forceStopAPK(String pkgName) {
    Process sh = null;/*from   ww  w.j  a  va 2  s .co  m*/
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        final String Command = "am force-stop" + pkgName;
        os.writeBytes(Command);
        os.flush();
        sh.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void RunAsRootNoOutput(String[] cmds) {
    try {/*from  w  w  w .  j  ava2s.c  om*/
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void runAsRoot(String[] commands) throws IOException, InterruptedException {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    for (String cmd : commands) {
        os.writeBytes(cmd + "\n");
    }/*from   w w w  .  j  ava  2  s .  c  o  m*/
    os.writeBytes("exit\n");
    os.flush();
    os.close();
    p.waitFor();
}

From source file:Main.java

public static void writeFile(File file, String string) throws IOException {
    file.getParentFile().mkdirs();//from w w  w  . j  a v a2s  .  co m
    DataOutputStream dout = new DataOutputStream(new FileOutputStream(file));
    dout.write(string.getBytes());
    dout.close();
}

From source file:Main.java

static void runCommandWithRoot(String command) {
    try {/*w ww .j av a  2s  .  c  o m*/
        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 void execCommand(String paramString) {
    try {/*from  w w w  .j  ava2 s  . c om*/
        String[] arrayOfString = new String[1];
        arrayOfString[0] = paramString;
        DataOutputStream localDataOutputStream = new DataOutputStream(
                Runtime.getRuntime().exec("su").getOutputStream());
        for (int i = 0;; i++) {
            if (i >= 1) {
                localDataOutputStream.writeBytes("exit\n");
                localDataOutputStream.flush();
                localDataOutputStream.wait();
                return;
            }
            localDataOutputStream.writeBytes(arrayOfString[i] + "\n");
            localDataOutputStream.flush();
        }
    } catch (Exception localException) {
    }
}

From source file:Main.java

private static void writeNonEmptyFile(final File file) {
    try {//from  w  ww .  ja va  2  s  . c  o m
        final OutputStream outputStream = new DataOutputStream(new FileOutputStream(file));
        final int expectedLength = 10;
        outputStream.write(expectedLength);
        // The negative beginning index is to accommodate the header. Fancy. Ever so fancy.
        for (int i = -3; i < expectedLength; i++)
            outputStream.write(0x0);
        outputStream.close();
    } catch (final IOException e) {
        throw new RuntimeException("Exception trying to create non-empty file!", e);
    }
}