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:com.cloudera.branchreduce.impl.thrift.Writables.java

public static <T extends Writable> T fromByteBuffer(ByteBuffer bb, Class<T> clazz) {
    T instance = ReflectionUtils.newInstance(clazz, DUMMY);
    try {/*from ww  w .  j a v  a2 s . c  o m*/
        instance.readFields(
                new DataInputStream(new ByteArrayInputStream(bb.array(), bb.arrayOffset(), bb.limit())));
    } catch (IOException e) {
        LOG.error("Deserialization error for class: " + clazz, e);
    }
    return instance;
}

From source file:Main.java

static boolean isJPEG(File file) throws IOException {
    return internalIsJPEG(new DataInputStream(new BufferedInputStream(new FileInputStream(file))));
}

From source file:Main.java

public static int getSuVersionCode() {
    Process process = null;/*from   w w  w .  ja  va2s .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 && Integer.parseInt(inLine.substring(0, 1)) > 2) {
                inLine = null;
                os.writeBytes("su -V\n");
                inLine = is.readLine();
                if (inLine != null) {
                    return Integer.parseInt(inLine);
                }
            } else {
                return 0;
            }
        } else {
            os.writeBytes("exit\n");
        }
    } catch (IOException e) {
        Log.e(TAG, "Problems reading current version.", e);
        return 0;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
    return 0;
}

From source file:Main.java

public String downloadWWWPage(URL pageURL) throws Exception {
    String host, file;/* ww  w .  j a v a 2  s .  co m*/
    host = pageURL.getHost();
    file = pageURL.getFile();

    InputStream pageStream = getWWWPageStream(host, file);
    if (pageStream == null) {
        return "";
    }

    DataInputStream in = new DataInputStream(pageStream);
    StringBuffer pageBuffer = new StringBuffer();
    String line;

    while ((line = in.readUTF()) != null) {
        pageBuffer.append(line);
    }
    in.close();
    return pageBuffer.toString();
}

From source file:com.ccoe.build.core.utils.FileUtils.java

public static String readFile(File file) {
    BufferedReader br = null;//w ww.  j ava 2  s .c om
    DataInputStream in = null;
    StringBuffer sBuffer = new StringBuffer();

    try {
        in = new DataInputStream(new FileInputStream(file));
        br = new BufferedReader(new InputStreamReader(in));

        String sCurrentLine = null;

        while ((sCurrentLine = br.readLine()) != null) {
            sBuffer.append(sCurrentLine);
            sBuffer.append("\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return sBuffer.toString().trim();
}

From source file:Main.java

public static float[] readBinAverageShapeArray(String dir, String fileName, int size) {
    float x;/*from  w  w  w .  j ava  2  s.  c  o m*/
    int i = 0;
    float[] tab = new float[size];

    // theses values was for 64140 points average shape
    //float minX = -90.3540f, minY = -22.1150f, minZ = -88.7720f, maxX = 101.3830f, maxY = 105.1860f, maxZ = 102.0530f;

    // values for average shape after simplification (8489 points)
    float maxX = 95.4549f, minX = -85.4616f, maxY = 115.0088f, minY = -18.0376f, maxZ = 106.7329f,
            minZ = -90.4051f;

    float deltaX = (maxX - minX) / 2.0f;
    float deltaY = (maxY - minY) / 2.0f;
    float deltaZ = (maxZ - minZ) / 2.0f;

    float midX = (maxX + minX) / 2.0f;
    float midY = (maxY + minY) / 2.0f;
    float midZ = (maxZ + minZ) / 2.0f;

    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();
        //Log.d(TAG,"first Vertices = "+x);
        while (true) {

            tab[i] = (x - midX) / deltaX; //rescale x
            i++;

            //read y
            x = in.readFloat();
            tab[i] = (x - midY) / deltaY; //rescale y
            i++;

            //read z
            x = in.readFloat();
            tab[i] = (x - midZ) / deltaZ; //rescale z
            i++;

            //read x
            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:PNGDecoder.java

public static BufferedImage decode(InputStream in) throws IOException {
    DataInputStream dataIn = new DataInputStream(in);
    readSignature(dataIn);//from   w  w  w  . ja  v a2 s.  c  om
    PNGData chunks = readChunks(dataIn);

    long widthLong = chunks.getWidth();
    long heightLong = chunks.getHeight();
    if (widthLong > Integer.MAX_VALUE || heightLong > Integer.MAX_VALUE)
        throw new IOException("That image is too wide or tall.");
    int width = (int) widthLong;
    int height = (int) heightLong;

    ColorModel cm = chunks.getColorModel();
    WritableRaster raster = chunks.getRaster();

    BufferedImage image = new BufferedImage(cm, raster, false, null);

    return image;
}

From source file:Main.java

public void run() {
    while (true) {
        try {/* w w w.ja  va2  s .  c  o  m*/
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();

            System.out.println("Just connected to " + server.getRemoteSocketAddress());
            DataInputStream in = new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());

            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");

            server.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}

From source file:com.polivoto.networking.ServicioDeIPExterna.java

public static String obtenerIPServidorRemoto() {
    String ip = null;//  w w w  . j a va2  s. c  o m
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(REMOTE_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);
        JSONObject json = new JSONObject(baos.toString());
        baos.close();
        entrada.close();
        con.disconnect();
        ip = json.getString("content");
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP servidor remoto: " + ip);
    return ip;
}

From source file:com.cloud.utils.crypt.RSAHelper.java

private static RSAPublicKey readKey(String key) throws Exception {
    byte[] encKey = Base64.decodeBase64(key.split(" ")[1]);
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(encKey));

    byte[] header = readElement(dis);
    String pubKeyFormat = new String(header);
    if (!pubKeyFormat.equals("ssh-rsa"))
        throw new RuntimeException("Unsupported format");

    byte[] publicExponent = readElement(dis);
    byte[] modulus = readElement(dis);

    KeySpec spec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
    KeyFactory keyFactory = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
    RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(spec);

    return pubKey;
}