List of usage examples for java.io DataInputStream DataInputStream
public DataInputStream(InputStream in)
From source file:com.polivoto.networking.ServicioDeIPExterna.java
public static String obtenerIPExterna() { String ip = null;/*from w w w. j a va 2s .c om*/ try { HttpURLConnection con = (HttpURLConnection) new URL(GET_EXTERNAL_HOST).openConnection(); DataInputStream entrada = new DataInputStream(con.getInputStream()); int length; byte[] chunk = new byte[64]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((length = entrada.read(chunk)) != -1) baos.write(chunk, 0, length); ip = baos.toString(); baos.close(); entrada.close(); con.disconnect(); } catch (IOException e) { e.printStackTrace(); } System.out.println("IP exterior: " + ip); return ip; }
From source file:Main.java
public static int[] readInts(String file) throws IOException { DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file), 4 * 1024)); try {/* w w w.j a v a 2 s . co m*/ int len = in.readInt(); int[] ints = new int[len]; for (int i = 0; i < len; i++) { ints[i] = in.readInt(); } return ints; } finally { in.close(); } }
From source file:Main.java
public static float[] readFloats(String file) throws IOException { DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file), 4 * 1024)); try {// w w w .j a va2 s . c om int len = in.readInt(); float[] floats = new float[len]; for (int i = 0; i < len; i++) { floats[i] = in.readFloat(); } return floats; } finally { in.close(); } }
From source file:Main.java
public static Object[] readSettings(String file) throws IOException { DataInputStream in = new DataInputStream(new FileInputStream(file)); try {//from w w w . ja v a2 s. c o m Object[] res = new Object[in.readInt()]; for (int i = 0; i < res.length; i++) { char cl = in.readChar(); switch (cl) { case 'S': res[i] = in.readUTF(); break; case 'F': res[i] = in.readFloat(); break; case 'D': res[i] = in.readDouble(); break; case 'I': res[i] = in.readInt(); break; case 'L': res[i] = in.readLong(); break; case 'B': res[i] = in.readBoolean(); break; case 'Y': res[i] = in.readByte(); break; default: throw new IllegalStateException("cannot read type " + cl + " from " + file); } } return res; } finally { in.close(); } }
From source file:Main.java
/** * Reads in model 83 points and returns a double array float containing the * coordinates x & y./*w ww . j av a 2 s .c o m*/ */ public static float[][][] readBinSFSV(String dir, String fileName) { float[][][] array3D = new float[60][83][3]; float x; 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 { for (int k = 0; k < 3; k++) { for (int j = 0; j < 83; j++) { for (int i = 0; i < 60; i++) { x = in.readFloat(); array3D[i][j][k] = x; } } } } 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 array3D; }
From source file:Main.java
/** * Reads in model 83 points and returns a double array float containing the * coordinates x & y./*from w w w . j a va2 s .com*/ */ public static float[][] readBinModel83Pt2DFloat(String dir, String fileName) { float[][] array2D = new float[83][2]; float x; int i = 0; 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 { while (true) { x = in.readFloat(); array2D[i][0] = x; x = in.readFloat(); array2D[i][1] = x; i++; } } 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 array2D; }
From source file:Main.java
private static String bytesToString(byte[] bytes) { if (bytes == null) { return null; }// www. ja v a2s .c o m ByteArrayInputStream bais = new ByteArrayInputStream(bytes); DataInputStream dis = new DataInputStream(bais); String res = ""; try { res = dis.readUTF(); } catch (IOException ex) { } finally { try { dis.close(); bais.close(); } catch (IOException ex1) { } dis = null; bais = null; } return res; }
From source file:nl.dreamkernel.s4.tweaker.util.RuntimeExec.java
public static String[] execute(String[] commands, boolean needResponce) { try {/*from w w w . ja va 2s. co m*/ Process process = Runtime.getRuntime().exec(commands); if (needResponce) { DataInputStream inputStream = new DataInputStream(process.getInputStream()); if (inputStream != null) { String ret = ""; int size = 0; byte[] buffer = new byte[1024]; try { do { size = inputStream.read(buffer); if (size > 0) { ret += new String(buffer, 0, size, HTTP.UTF_8); } } while (inputStream.available() > 0); } catch (IOException e) { } return ret.split("\n"); } } } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String[] getUvValues() { ArrayList<String> value = new ArrayList<String>(); try {/* w ww . java2 s . c o 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[] val = strLine.split("\\s+"); value.add(val[1]); } } // Close the input stream in.close(); } catch (Exception e) {// Catch exception if any System.err.println("Error: " + e.getMessage()); } String[] values = new String[value.size() - 1]; values = value.toArray(values); return values; }
From source file:Main.java
/** * Read the featureVector_Shape.dat file. * Build the double array and return it. * k : number of rows// ww w . j a v a 2 s.c om * n : number of columns */ public static float[][] readBinFloatDoubleArray(String dir, String fileName, int k, int n) { float[][] array2D = new float[k][n]; float x; 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 { for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { x = in.readFloat(); array2D[i][j] = x; } } } 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 array2D; }