Example usage for java.io DataInputStream DataInputStream

List of usage examples for java.io DataInputStream DataInputStream

Introduction

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

Prototype

public DataInputStream(InputStream in) 

Source Link

Document

Creates a DataInputStream that uses the specified underlying InputStream.

Usage

From source file:Main.java

public static byte[] readToEndAsArray(InputStream input) throws IOException {
    DataInputStream dis = new DataInputStream(input);
    byte[] stuff = new byte[1024];
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    int read = 0;
    while ((read = dis.read(stuff)) != -1) {
        buff.write(stuff, 0, read);// ww  w.  j a va2 s .c o m
    }
    dis.close();
    return buff.toByteArray();
}

From source file:Main.java

public static File WriteStreamToFile(InputStream resStream) {
    try {//from   www  .  jav  a  2s .  c o m
        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;
}

From source file:Main.java

public static String textFromFile(String filename) {

    String text = new String();
    String nextLine = new String();

    try {//from  w ww .  j  a  v a  2  s  .  c  om
        FileInputStream instream = new FileInputStream(filename);
        DataInputStream in = new DataInputStream(instream);

        BufferedReader buffered = new BufferedReader(new InputStreamReader(in));

        while ((nextLine = buffered.readLine()) != null) {
            text = text.concat(nextLine);
        }
    } catch (IOException e) {
        return null;
    }

    return text;
}

From source file:Main.java

static public String readFile(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    DataInputStream input = null;
    try {/* ww w  . j  av  a  2 s .co m*/
        input = new DataInputStream(new FileInputStream(file));
        input.readFully(buffer);
    } finally {
        closeQuietly(input);
    }
    return new String(buffer);
}

From source file:Main.java

public static String execRootCmd(String[] cmds) {
    String result = "";
    DataOutputStream dos = null;/*from ww  w. j ava  2s . c  o  m*/
    DataInputStream dis = null;

    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        dis = new DataInputStream(p.getInputStream());

        for (String cmd : cmds) {
            Log.i("CmdUtils", cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
        }
        dos.writeBytes("exit\n");
        dos.flush();
        String line;
        while ((line = dis.readLine()) != null) {
            Log.d("result", line);
            result += line;
        }
        p.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:Main.java

public static int execRootCmdForExitCode(String[] cmds) {
    int result = -1;
    DataOutputStream dos = null;/* w w w  .  j  a va2 s  . c o  m*/
    DataInputStream dis = null;

    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        dis = new DataInputStream(p.getInputStream());

        for (String cmd : cmds) {
            Log.i("CmdUtils", cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
        }
        dos.writeBytes("exit\n");
        dos.flush();
        String line;
        while ((line = dis.readLine()) != null) {
            Log.d("result", line);
        }
        p.waitFor();
        result = p.exitValue();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Determine whether a file is a ZIP File.
 *//*from  w w  w . ja  va2  s  .  c  om*/
public static boolean isZipFile(File file) throws IOException {
    if (file.isDirectory()) {
        return false;
    }
    if (!file.canRead()) {
        throw new IOException("Cannot read file " + file.getAbsolutePath());
    }
    if (file.length() < 4) {
        return false;
    }
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
    int test = in.readInt();
    in.close();
    return test == 0x504b0304;
}

From source file:Main.java

public static String getSimID() {
    // TODO Auto-generated method stub
    Log.v(TAG, "getSimID() called");
    String simID = "";
    try {/*from  www .  j a v  a  2 s  . c  o  m*/
        FileInputStream is = new FileInputStream(SIMCARD_PATH);
        DataInputStream dis = new DataInputStream(is);
        simID = dis.readLine();
        simID = simID.trim();
        is.close();
        dis.close();
        return simID;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Log.v(TAG, "getSimID exception: do not have a SIM Card!", e);
        return "123456";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.v(TAG, "getSimID exception: read IOException", e);
        return "";
    }

}

From source file:Main.java

public static void openNamedFile(String filename) {
    try {/*from w ww  .j av  a 2  s. c  om*/
        File f = new File(filename);
        //   Log.e("kuinfa", "filename= " + filename);
        FileInputStream fis = new FileInputStream(f);

        long size = f.length();
        name = f.getName();
        patch = f.getParentFile().toString();
        DataInputStream dis = new DataInputStream(fis);
        byte[] b = new byte[(int) size];
        int length = dis.read(b, 0, (int) size);

        dis.close();
        fis.close();

        String ttt = new String(b, 0, length, "UTF-8");

        try {
            ttt = new String(ttt.getBytes(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
        }

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

From source file:Main.java

/**
 * Degzips the compressed array and places the results into the decompressed array.
 *
 * @param compressed The compressed array.
 * @param decompressed The decompressed array.
 * @throws IOException If an I/O error occurs.
 *//*from  ww w .j  av a2 s.  c  om*/
public static void degzip(byte[] compressed, byte[] decompressed) throws IOException {
    try (DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed)))) {
        is.readFully(decompressed);
    }
}