Example usage for java.io InputStream read

List of usage examples for java.io InputStream read

Introduction

In this page you can find the example usage for java.io InputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:Main.java

public static byte[] readBytes(InputStream is) throws IOException {
    byte[] buffer;

    buffer = new byte[is.available()];
    is.read(buffer);
    is.close();//from www  .  j  av  a 2 s .  c  om

    return buffer;
}

From source file:Main.java

/**
 * Read input from input stream and write it to output stream 
 * until there is no more input from input stream.
 *
 * @param is input stream the input stream to read from.
 * @param os output stream the output stream to write to.
 * @param buf the byte array to use as a buffer
 *///from   w  w  w. j a  v a2 s .  co  m
public static void flow(InputStream is, OutputStream os, byte[] buf) throws IOException {
    int numRead;
    while ((numRead = is.read(buf)) >= 0) {
        os.write(buf, 0, numRead);
    }
}

From source file:Main.java

public static byte[] read(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    InputStream ios = null;
    try {//  w w  w.ja  v a 2 s .c o m
        ios = new FileInputStream(file);
        if (ios.read(buffer) == -1)
            throw new IOException("EOF reached while trying to read the whole file");
    } finally {
        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }
    return buffer;
}

From source file:MainClass.java

public static void copy(InputStream in, OutputStream out) throws IOException {

    byte[] buffer = new byte[1024];
    while (true) {
        int bytesRead = in.read(buffer);
        if (bytesRead == -1)
            break;
        out.write(buffer, 0, bytesRead);
    }//from ww w.j a  va  2 s.  c  o m
}

From source file:Main.java

public static String md5Calc(File f) {
    int i;// www .j a  v a  2s .  c om

    byte[] buffer = new byte[1024];
    int read = 0;
    String md5hash;
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(f);
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        md5hash = bigInt.toString(16);
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    if (md5hash.length() != 33) {
        String tmp = "";
        for (i = 1; i < (33 - md5hash.length()); i++) {
            tmp = tmp.concat("0");
        }
        md5hash = tmp.concat(md5hash);
    }

    return md5hash;
}

From source file:Main.java

public static String readFile(Context context, String name) throws IOException {
    File file = context.getFileStreamPath(name);
    InputStream is = new FileInputStream(file);

    byte b[] = new byte[(int) file.length()];

    is.read(b);
    is.close();//from   w w w  .j  av a2s  .  c om

    String string = new String(b);

    return string;
}

From source file:Main.java

public static String readAsset(AssetManager assets, String path) throws IOException {
    InputStream is = assets.open(path);
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();//www .j  av  a 2 s.co  m
    return new String(buffer);
}

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os, byte[] buffer) throws IOException {
    while (true) {
        int count = is.read(buffer);
        if (count != -1) {
            os.write(buffer, 0, count);//from  w  w  w.  j a  v a  2  s .c  om
        } else {
            return;
        }
    }
}

From source file:Main.java

public static byte[] loadIntoBytes(String fileName) {
    AssetManager assetManager = context.getAssets();
    try {//  www.ja  v a  2  s .c  o  m
        InputStream is = assetManager.open(fileName);
        byte buf[] = new byte[is.available()];
        is.read(buf);
        return buf;
    } catch (IOException e) {
    }
    return null;
}

From source file:Main.java

/**
 * DB test/* w w  w  .j a  va  2  s.  co  m*/
 */
public static void runBackup(Context context) {
    File file = context.getDatabasePath("PhotoDeskHiddenFolder.db");
    int size = (int) file.length();

    String path = Environment.getExternalStorageDirectory() + "/PhotoDesk/";
    try {
        byte[] buffer = new byte[size];
        InputStream inputStream = new FileInputStream(file);
        inputStream.read(buffer);
        inputStream.close();

        File outputDBDirectory = new File(path);
        if (!outputDBDirectory.isDirectory())
            outputDBDirectory.mkdir();

        path += "test.db";

        File outputFile = new File(path);
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(buffer);
        outputStream.flush();
        outputStream.close();

    } catch (Exception e) {
    }
}