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

/**
 * @param context/*from w w  w. j a  v a2s.  co m*/
 * @param filePath file path relative to assets, like request_init1/search_index.json
 * @return
 */
public static String readAssert(Context context, String filePath) {
    try {
        if (filePath.startsWith(File.separator)) {
            filePath = filePath.substring(File.separator.length());
        }
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open(filePath);
        DataInputStream stream = new DataInputStream(inputStream);
        int length = stream.available();
        byte[] buffer = new byte[length];
        stream.readFully(buffer);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(buffer);
        stream.close();
        return byteArrayOutputStream.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:TestPipes.java

public static void readData(InputStream is) {
    DataInputStream in = new DataInputStream(new BufferedInputStream(is));
    boolean eof = false;
    try {/*from  w  w  w  . j  a v a 2s. c o m*/
        while (!eof) {
            int iValue = in.readInt();
            System.out.println("read value = " + iValue);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("End of Data");
}

From source file:Main.java

public static void makeInternalCopy(Context c, String path, int resource) {
    InputStream is = c.getResources().openRawResource(resource);
    try {/*  w ww  .j av  a2 s  .  c  o  m*/

        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        File sniff = new File(path);
        if (sniff.exists()) {
            os.writeBytes("rm " + path + "\n");
        }
        byte[] bytes = new byte[is.available()];

        FileOutputStream setdbOutStream = new FileOutputStream(path);
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(bytes);

        setdbOutStream.write(bytes);
        setdbOutStream.close();

        os.writeBytes("chmod 777 " + path + "\n");
        os.writeBytes("exit\n");
        os.flush();
    } catch (Exception e) {
        //           Toast.makeText(c, "Error Copying file: " + e.toString(),Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}

From source file:Main.java

public static String runCommand(String[] commands) {
    DataOutputStream outStream = null;
    DataInputStream responseStream;
    try {/*from   ww w  .j av  a 2  s. c  o  m*/
        ArrayList<String> logs = new ArrayList<String>();
        Process process = Runtime.getRuntime().exec("su");
        Log.i(TAG, "Executed su");
        outStream = new DataOutputStream(process.getOutputStream());
        responseStream = new DataInputStream(process.getInputStream());

        for (String single : commands) {
            Log.i(TAG, "Command = " + single);
            outStream.writeBytes(single + "\n");
            outStream.flush();
            if (responseStream.available() > 0) {
                Log.i(TAG, "Reading response");
                logs.add(responseStream.readLine());
                Log.i(TAG, "Read response");
            } else {
                Log.i(TAG, "No response available");
            }
        }
        outStream.writeBytes("exit\n");
        outStream.flush();
        String log = "";
        for (int i = 0; i < logs.size(); i++) {
            log += logs.get(i) + "\n";
        }
        Log.i(TAG, "Execution compeleted");
        return log;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
    return null;
}

From source file:Main.java

public static int[] readBin83PtIndex(String dir, String fileName) {
    int i = 0;//from   www. j a  va 2s .co  m
    int x = 0;
    int[] tab = new int[83];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readInt();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readInt();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

public static int[] readBinIntArray(String dir, String fileName, int size) {
    int x;//  w w w . ja v  a2  s . com
    int i = 0;
    int[] tab = new int[size];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readInt();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readInt();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

public static float[] readBinFloat(String dir, String fileName, int size) {
    float x;//from   w w w  .  j a  va2s. co  m
    int i = 0;
    float[] tab = new float[size];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readFloat();
        while (i < size) {
            tab[i] = x;
            i++;
            x = in.readFloat();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

public static float[] readBinTextureArray(String dir, String fileName, int size) {
    float x;//from   ww w.  j  ava  2s . c om
    int i = 0;
    float[] tab = new float[size];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readFloat(); // convert here for OpenGl
        while (true) {
            tab[i] = x / 255.0f;
            i++;
            x = in.readFloat(); // convert here for OpenGl
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

public static float[] readBinShapeArray(String dir, String fileName, int size) {
    float x;//from w  w  w  .j  a v a 2s .c om
    int i = 0;
    float[] tab = new float[size];

    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        //read first x
        x = in.readFloat();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readFloat();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:PipedBytes.java

public static void readStuff(InputStream rawIn) {
    try {//from   w w w .j  a  v  a 2 s. com
        DataInputStream in = new DataInputStream(new BufferedInputStream(rawIn));

        boolean eof = false;
        while (!eof) {
            try {
                int i = in.readInt();
                System.out.println("just read: " + i);
            } catch (EOFException eofx) {
                eof = true;
            }
        }

        System.out.println("Read all data from the pipe");
    } catch (IOException x) {
        x.printStackTrace();
    }
}