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 int byteArrayToInt(byte[] input) {
    try {/*from ww w. j  ava2  s  . c o m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(input);
        DataInputStream dis = new DataInputStream(bis);
        int numb = dis.readInt();
        dis.close();
        return numb;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getSuVersion() {
    Process process = null;/*from w  ww .  j a  va  2s .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) {
                return inLine;
            }
        } else {
            os.writeBytes("exit\n");
        }
    } catch (IOException e) {
        Log.e(TAG, "Problems reading current version.", e);
        return null;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }

    return null;
}

From source file:Main.java

/**
 *
 * Resolve DNS name into IP address/*  w w w  .j a v  a  2  s .com*/
 *
 * @param host DNS name
 * @return IP address in integer format
 * @throws IOException
 */
public static int resolve(String host) throws IOException {
    Socket localSocket = new Socket(ADB_HOST, ADB_PORT);
    DataInputStream dis = new DataInputStream(localSocket.getInputStream());
    OutputStream os = localSocket.getOutputStream();
    int count_read = 0;

    if (localSocket == null || dis == null || os == null)
        return -1;
    String cmd = "dns:" + host;

    if (!sendAdbCmd(dis, os, cmd))
        return -1;

    count_read = dis.readInt();
    localSocket.close();
    return count_read;
}

From source file:Main.java

/**
 * Locte the tun.ko driver on the filesystem  
 * @return a String with the path of the tun.ko driver
 *//*  ww w.  ja  v  a2s.  c  o m*/
public static String findTunDriverPath() {
    String tunDriverPath = "";
    String find_str;
    try {
        Process find_proc = Runtime.getRuntime().exec("find /sdcard /system /data -name tun.ko");
        DataInputStream find_in = new DataInputStream(find_proc.getInputStream());
        try {
            while ((find_str = find_in.readLine()) != null) {
                tunDriverPath = find_str;
            }
        } catch (IOException e) {
            System.exit(0);
        }
    } catch (IOException e1) {
        Log.e("OpenVpnSettings_Util", e1.toString());
    }

    //      if (new File(tunDriverPath).exists())
    //         System.out.println(tunDriverPath);
    //      else System.out.println("ERROR file does not exist: " + tunDriverPath);
    return tunDriverPath;
}

From source file:Main.java

static boolean isJPEG(byte[] data) throws IOException {
    return internalIsJPEG(new DataInputStream(new ByteArrayInputStream(data)));
}

From source file:MainClass.java

public void run() {
    try {//from  w ww. j  a  v a  2  s .c  o  m
        Socket client = serverSocket.accept();

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

        while (true) {
            String message = in.readUTF();
            System.out.println(message);
            System.out.print("Enter response: ");
            String response = console.readLine();

            out.writeUTF(response);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String executeHttpsPost(String url, String data, InputStream key) {
    HttpsURLConnection localHttpsURLConnection = null;
    try {//from w  w w .j a  v  a 2s  .c  o m
        URL localURL = new URL(url);
        localHttpsURLConnection = (HttpsURLConnection) localURL.openConnection();
        localHttpsURLConnection.setRequestMethod("POST");
        localHttpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        localHttpsURLConnection.setRequestProperty("Content-Length",
                "" + Integer.toString(data.getBytes().length));
        localHttpsURLConnection.setRequestProperty("Content-Language", "en-US");

        localHttpsURLConnection.setUseCaches(false);
        localHttpsURLConnection.setDoInput(true);
        localHttpsURLConnection.setDoOutput(true);

        localHttpsURLConnection.connect();
        Certificate[] arrayOfCertificate = localHttpsURLConnection.getServerCertificates();

        byte[] arrayOfByte1 = new byte[294];
        DataInputStream localDataInputStream = new DataInputStream(key);
        localDataInputStream.readFully(arrayOfByte1);
        localDataInputStream.close();

        Certificate localCertificate = arrayOfCertificate[0];
        PublicKey localPublicKey = localCertificate.getPublicKey();
        byte[] arrayOfByte2 = localPublicKey.getEncoded();

        for (int i = 0; i < arrayOfByte2.length; i++) {
            if (arrayOfByte2[i] != arrayOfByte1[i])
                throw new RuntimeException("Public key mismatch");
        }

        DataOutputStream localDataOutputStream = new DataOutputStream(
                localHttpsURLConnection.getOutputStream());
        localDataOutputStream.writeBytes(data);
        localDataOutputStream.flush();
        localDataOutputStream.close();

        InputStream localInputStream = localHttpsURLConnection.getInputStream();
        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream));

        StringBuffer localStringBuffer = new StringBuffer();
        String str1;
        while ((str1 = localBufferedReader.readLine()) != null) {
            localStringBuffer.append(str1);
            localStringBuffer.append('\r');
        }
        localBufferedReader.close();

        return localStringBuffer.toString();
    } catch (Exception localException) {
        byte[] arrayOfByte1;
        localException.printStackTrace();
        return null;
    } finally {
        if (localHttpsURLConnection != null)
            localHttpsURLConnection.disconnect();
    }
}

From source file:cfa.vo.interop.EncodeDoubleArray.java

private static double[] byteToDouble(byte[] data) throws IOException {

    int len = data.length;

    if (len % WORDSIZE != 0) {
        throw new IOException("Array length is not divisible by wordsize");
    }//from  w  w  w  .ja v  a2 s.c o m

    int size = len / WORDSIZE;
    double[] result = new double[size];

    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data));

    try {

        int ii = 0;
        while (inputStream.available() > 0) {
            result[ii] = inputStream.readDouble();
            ii++;
        }

    } catch (EOFException e) {
        throw new IOException("Unable to read from dataInputStream, found EOF");

    } catch (IOException e) {
        throw new IOException("Unable to read from dataInputStream, IO error");
    }

    return result;

}

From source file:info.dolezel.fatrat.plugins.util.FileUtils.java

/**
  * Returns the first line from the file.
  * @return <code>null</code> if the operation failed for any reason.
  *//*  w  w  w .  ja v  a2 s  .  com*/
public static String fileReadLine(String file) {
    FileInputStream fis = null;
    BufferedReader br = null;
    DataInputStream dis = null;
    InputStreamReader isr = null;

    try {
        fis = new FileInputStream(file);
        dis = new DataInputStream(fis);
        isr = new InputStreamReader(dis);
        br = new BufferedReader(isr);

        return br.readLine();
    } catch (IOException ex) {
        return null;
    } finally {
        IOUtils.closeQuietly(fis);
    }
}

From source file:ArrayDictionary.java

/**
 * A kludge for testing ArrayDictionary//from w ww .jav  a  2  s . c o m
 */
public static void main(String[] args) {
    try {
        PrintStream out = System.out;
        DataInputStream in = new DataInputStream(System.in);

        String line = null;

        out.print("n ? ");
        out.flush();
        line = in.readLine();
        int n = Integer.parseInt(line);
        ArrayDictionary ad = new ArrayDictionary(n);

        String key = null, value = null;
        while (true) {
            out.print("action ? ");
            out.flush();
            line = in.readLine();

            switch (line.charAt(0)) {
            case 'p':
            case 'P':
                out.print("key ? ");
                out.flush();
                key = in.readLine();
                out.print("value ? ");
                out.flush();
                value = in.readLine();
                value = (String) ad.put(key, value);
                out.println("old: " + value);
                break;
            case 'r':
            case 'R':
                out.print("key ? ");
                out.flush();
                key = in.readLine();
                value = (String) ad.remove(key);
                out.println("old: " + value);
                break;
            case 'g':
            case 'G':
                out.print("key ? ");
                out.flush();
                key = in.readLine();
                value = (String) ad.get(key);
                out.println("value: " + value);
                break;
            case 'd':
            case 'D':
                out.println(ad.toString());
                break;
            case 'q':
            case 'Q':
                return;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}