Example usage for java.io DataInputStream readFully

List of usage examples for java.io DataInputStream readFully

Introduction

In this page you can find the example usage for java.io DataInputStream readFully.

Prototype

public final void readFully(byte b[]) throws IOException 

Source Link

Document

See the general contract of the readFully method of DataInput .

Usage

From source file:Main.java

public static void makeInternalCopy(Context c, String path, int resource) {
    InputStream is = c.getResources().openRawResource(resource);
    try {//from  w  ww. j  a va 2 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

static public String readFile(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    DataInputStream input = null;
    try {/*from w  ww  .j a  v  a  2  s. co  m*/
        input = new DataInputStream(new FileInputStream(file));
        input.readFully(buffer);
    } finally {
        closeQuietly(input);
    }
    return new String(buffer);
}

From source file:Main.java

public static String readFile(String path) throws IOException {
    InputStream is = new FileInputStream(new File(path));
    DataInputStream ds = new DataInputStream(is);
    // Estimated capacity, potential risk ???
    byte[] bytes = new byte[is.available()];
    ds.readFully(bytes);
    String text = new String(bytes);
    is.close();//from   w  w  w.j a  v  a  2s. c o m
    ds.close();
    return text;
}

From source file:Main.java

public static String executeHttpsPost(String url, String data, InputStream key) {
    HttpsURLConnection localHttpsURLConnection = null;
    try {//w w  w.j a  v a 2 s .  co 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:Main.java

/**
 * @param context/*from ww w.j  a  v a 2 s.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:ImportKey.java

/**
 * <p>//from   w  ww .j av  a2s  . co  m
 * Creates an InputStream from a file, and fills it with the complete file.
 * Thus, available() on the returned InputStream will return the full number
 * of bytes the file contains
 * </p>
 * 
 * @param fname
 *            The filename
 * @return The filled InputStream
 * @exception IOException
 *                , if the Streams couldn't be created.
 **/
private static InputStream fullStream(String fname) throws IOException {
    FileInputStream fis = new FileInputStream(fname);
    DataInputStream dis = new DataInputStream(fis);
    byte[] bytes = new byte[dis.available()];
    dis.readFully(bytes);
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    return bais;
}

From source file:org.thoughtcrime.securesms.mms.MmsCommunication.java

protected static byte[] parseResponse(HttpEntity entity) throws IOException {
    if (entity == null || entity.getContentLength() == 0)
        throw new IOException("Null response");

    byte[] responseBytes = new byte[(int) entity.getContentLength()];
    DataInputStream dataInputStream = new DataInputStream(entity.getContent());
    dataInputStream.readFully(responseBytes);
    dataInputStream.close();//from   w ww  .  j a v  a2 s .  c o m

    entity.consumeContent();
    return responseBytes;
}

From source file:TripleDES.java

/** Read a TripleDES secret key from the specified file */
public static SecretKey readKey(File f)
        throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    // Read the raw bytes from the keyfile
    DataInputStream in = new DataInputStream(new FileInputStream(f));
    byte[] rawkey = new byte[(int) f.length()];
    in.readFully(rawkey);
    in.close();//from www .  j  a  v  a  2  s . com

    // Convert the raw bytes to a secret key like this
    DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    SecretKey key = keyfactory.generateSecret(keyspec);
    return key;
}

From source file:com.google.code.rptm.mailarchive.DefaultMailingListArchive.java

private static boolean isMboxFile(File file) throws IOException {
    byte[] firstBytes = new byte[5];
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    try {/*from ww w  .  jav  a 2  s. c  o  m*/
        in.readFully(firstBytes);
    } finally {
        in.close();
    }
    return Arrays.equals(firstBytes, MBOX_MAGIC);
}

From source file:com.wso2telco.services.bw.FileUtil.java

public static String ReadFullyIntoVar(String fullpath) {

    String result = "";

    try {// ww  w.jav a  2s  . c  om
        FileInputStream file = new FileInputStream(fullpath);
        DataInputStream in = new DataInputStream(file);
        byte[] b = new byte[in.available()];
        in.readFully(b);
        in.close();
        result = new String(b, 0, b.length, "Cp850");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}