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 String[] getUvTableNames() {
    ArrayList<String> Tokens = new ArrayList<String>();

    try {/*from w ww  . ja  v a  2  s.co  m*/
        // Open the file that is the first
        // command line parameter
        FileInputStream fstream = null;
        File f = new File("/sys/devices/system/cpu/cpufreq/vdd_table/vdd_levels");
        if (f.exists()) {
            fstream = new FileInputStream("/sys/devices/system/cpu/cpufreq/vdd_table/vdd_levels");
        } else {
            File ff = new File("/sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table");
            if (ff.exists()) {
                fstream = new FileInputStream("/sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table");
            }
        }
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        // Read File Line By Line
        while ((strLine = br.readLine()) != null) {
            strLine = strLine.trim();

            if ((strLine.length() != 0)) {
                String[] names = strLine.replaceAll(":", "").split("\\s+");
                Tokens.add(names[0]);
            }

        }
        // Close the input stream
        in.close();
    } catch (Exception e) {// Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
    String[] names = new String[Tokens.size() - 1];
    names = Tokens.toArray(names);
    return names;
}

From source file:FileUtil.java

public static byte[] readFileAsBytes(String path, Integer start, Integer length) {

    byte[] byteData = null;

    try {/*from   w  w w . jav a2s  .co  m*/

        File file = new File(path);

        DataInputStream dis;
        dis = new DataInputStream(new FileInputStream(file));

        if (dis.available() > Integer.MAX_VALUE) {
            System.out.println("dis.available() > Integer.MAX_VALUE");
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream(length);
        byte[] bytes = new byte[length];

        dis.skipBytes(start);
        int readBytes = dis.read(bytes, 0, length);
        os.write(bytes, 0, readBytes);

        byteData = os.toByteArray();

        dis.close();
        os.close();

    } catch (Exception e) {
        System.out.println(e);
    }

    return byteData;
}

From source file:com.ning.arecibo.util.timeline.times.TimesAndSamplesCoder.java

public static int getSizeOfTimeBytes(final byte[] timesAndSamples) {
    final DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(timesAndSamples));
    try {/* ww w .j  a va2  s  .c  om*/
        return inputStream.readInt();
    } catch (IOException e) {
        throw new IllegalStateException(String.format(
                "Exception reading timeByteCount in TimelineChunkMapper.map() for timesAndSamples %s",
                Hex.encodeHex(timesAndSamples)), e);
    }
}

From source file:Main.java

/**
 * The URL looks like a request for a resource, such as a JavaScript or CSS file. Write
 * the given resource to the response output in binary format (needed for images).
 *//*from w  w  w.  j  a v  a2s  . c  o m*/
public static void readWriteBinaryUtil(String sxURL, String resource, OutputStream outStream)
        throws IOException {

    DataOutputStream outData = null;
    DataInputStream inData = null;
    int byteCnt = 0;
    byte[] buffer = new byte[4096];

    // get full qualified path of class
    System.out.println("RW Base Directory - " + sxURL);

    // remove class and add resource relative path
    sxURL = getResourceURL(sxURL, resource);

    System.out.println("RW Loading - " + sxURL);
    try {
        outData = new DataOutputStream(outStream);
        inData = new DataInputStream(new URL(sxURL).openConnection().getInputStream());

        while ((byteCnt = inData.read(buffer)) != -1) {
            if (outData != null && byteCnt > 0) {
                outData.write(buffer, 0, byteCnt);
            }
        }
    } catch (IOException e) {
        throw e;
    } finally {
        try {
            if (inData != null) {
                inData.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:flexpos.restfulConnection.java

public static ArrayList<ArrayList<String>> getRESTful(String RESTfull_URL, ArrayList<String> columnasTabla) {
    try {/*from   w  w  w.  j  av  a2s.  c  o  m*/
        URL url;
        URLConnection urlConnection;
        DataInputStream readString;
        url = new URL(RESTfull_URL);
        urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        readString = new DataInputStream(urlConnection.getInputStream());
        String s;

        String getRequest = "";
        while ((s = readString.readLine()) != null) {
            getRequest += s;
        }
        readString.close();
        if (!"".equals(getRequest)) {
            return completeArray(getRequest, columnasTabla);
        }

    } catch (Exception ex) {
        Logger.getLogger(restfulConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.flexdesktop.connections.restfulConnection.java

public static ArrayList<ArrayList<String>> getRESTful(String RESTfull_URL, ArrayList<String> columnasTabla) {
    try {//from ww w  . j av  a  2 s .  c  o  m
        URL url;
        URLConnection urlConnection;
        DataInputStream readString;
        url = new URL(RESTfull_URL);
        urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        readString = new DataInputStream(urlConnection.getInputStream());
        String s;

        String getRequest = "";
        while ((s = readString.readLine()) != null) {
            getRequest += s;
        }
        readString.close();
        if (!"".equals(getRequest)) {
            return completeArray(getRequest, columnasTabla);
        }

    } catch (Exception ex) {
        Logger.getLogger(com.flexdesktop.connections.restfulConnection.class.getName()).log(Level.SEVERE, null,
                ex);
    }
    return null;
}

From source file:Main.java

static int readFirmwareFirmware(File fp) {
    try {/*from   w ww. java2s.c om*/
        FileInputStream fis = new FileInputStream(fp);
        DataInputStream dis = new DataInputStream(fis);
        if (dis.skipBytes(2048) != 2048) {
            // Log.v("DistoX", "failed skip");
            return 0; // skip 8 bootloader blocks
        }
        byte[] buf = new byte[64];
        if (dis.read(buf, 0, 64) != 64) {
            // Log.v("DistoX", "failed read");
            return 0;
        }
        for (int k = 0; k < 64; ++k) {
            // Log.v("DistoX", "byte " + k + " " + buf[k] + " sign " + signature[k] );
            if (k == 6 || k == 7 || k == 16 || k == 17)
                continue;
            if (buf[k] != signature[k])
                return 0;
        }
        if (buf[7] == (byte) 0xf8) {
            if (buf[6] == (byte) 0x34) {
                return 21;
            } else if (buf[6] == (byte) 0x3a) {
                return 22;
            }
        } else if (buf[7] == (byte) 0xf9) {
            if (buf[6] == (byte) 0x90) {
                return 23;
            }
        } else if (buf[7] == (byte) 0xfa) {
            if (buf[6] == (byte) 0x0a) {
                return 24;
            }
        }
    } catch (IOException e) {
    }
    return 0;
}

From source file:Main.java

public static String execRootCmd(String cmd) {
    String result = "result : ";
    try {/*from ww  w. ja va 2s.c  o  m*/
        Process p = Runtime.getRuntime().exec("su ");
        OutputStream outStream = p.getOutputStream();
        DataOutputStream dOutStream = new DataOutputStream(outStream);
        InputStream inStream = p.getInputStream();
        DataInputStream dInStream = new DataInputStream(inStream);
        String str1 = String.valueOf(cmd);
        String str2 = str1 + "\n";
        dOutStream.writeBytes(str2);
        dOutStream.flush();
        String str3 = null;
        String line = "";
        while ((line = dInStream.readLine()) != null) {
            Log.d("result", str3);
            str3 += line;
        }
        dOutStream.writeBytes("exit\n");
        dOutStream.flush();
        p.waitFor();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return result;
    }
}

From source file:MainClass.java

public void run() {
    try {/*from   ww  w . j  av  a 2 s .  com*/
        Socket socket = new Socket("127.0.0.1", 2000);

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

        while (true) {
            System.out.print("Enter response: ");
            String response = console.readLine();
            out.writeUTF(response);

            String message = in.readUTF();
            System.out.println(message);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String readFromInternalStorage(Context context, String fileName) {

    File file = new File(context.getFilesDir(), fileName);

    try {/*from www. ja  v a2  s.  c  o  m*/
        FileInputStream fis = new FileInputStream(file);
        DataInputStream in = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        String line;

        StringBuilder stringBuilder = new StringBuilder();
        while ((line = br.readLine()) != null) {
            stringBuilder.append(line);
        }
        String json = stringBuilder.toString();

        br.close();
        in.close();
        fis.close();

        return json;
    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
    }

    return null;
}