Example usage for java.io FileInputStream read

List of usage examples for java.io FileInputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:eu.europa.esig.dss.applet.util.FileTypeDetectorUtils.java

/**
 * @param file//  w  ww. j  a  va  2  s .  co m
 * @return
 * @throws IOException
 */
private static String extractPreambleString(final File file) throws IOException {

    FileInputStream inputStream = null;

    try {
        inputStream = new FileInputStream(file);

        final byte[] preamble = new byte[5];
        final int read = inputStream.read(preamble);
        if (read < 5) {
            throw new RuntimeException();
        }

        return new String(preamble);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:Main.java

public static byte[] getBytesFromFile(File f) {

    if (f == null) {
        return null;
    }/*  ww w. j  av a 2s .co m*/

    FileInputStream stream = null;
    ByteArrayOutputStream out = null;

    try {
        stream = new FileInputStream(f);
        out = new ByteArrayOutputStream(1024);
        byte[] b = new byte[1024];
        int n;
        while ((n = stream.read(b)) != -1)
            out.write(b, 0, n);

    } catch (Exception e) {
    }

    try {
        if (out != null)
            out.close();

        if (stream != null)
            stream.close();
    } catch (Exception e) {
    }

    System.gc();

    if (out == null)
        return null;

    return out.toByteArray();
}

From source file:Main.java

/**
 * Dump the database file to external storage
 *
 * @param packageName/*from www  .  j  a v  a 2 s  . c o  m*/
 * @param fileName
 * @throws IOException
 */
public static void dumpDatabase(String packageName, String fileName) throws IOException {
    File dbFile = new File("/data/data/" + packageName + "/databases/" + fileName);
    if (dbFile.exists()) {
        FileInputStream fis = new FileInputStream(dbFile);
        String outFileName = Environment.getExternalStorageDirectory() + "/" + fileName;
        OutputStream output = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        output.flush();
        output.close();
        fis.close();
    }
}

From source file:Main.java

public static boolean fileCopy(String from, String to) {
    boolean result = false;

    int size = 1 * 1024;

    FileInputStream in = null;
    FileOutputStream out = null;//from   w  w w  . j a  va  2 s. c om
    try {
        in = new FileInputStream(from);
        out = new FileOutputStream(to);
        byte[] buffer = new byte[size];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
        result = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        }
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
        }
    }
    return result;
}

From source file:com.net.plus.common.Utils.Util.java

public static byte[] getBytesFromFile(String file) {
    if (file == null) {
        return null;
    }/*from  w  ww .j  a  v a2s  .com*/
    byte[] ret = null;
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
        byte[] b = new byte[4096];
        int n;
        while ((n = in.read(b)) != -1) {
            out.write(b, 0, n);
        }
        ret = out.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {

            }
        }
    }
    return ret;
}

From source file:Compress.java

/** Zip the contents of the directory, and save it in the zipfile */
public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
        throw new IllegalArgumentException("Compress: not a directory:  " + dir);
    String[] entries = d.list();// ww w  . ja  v a2 s .  c o m
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytes_read;

    // Create a stream to compress data and write it to the zipfile
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

    // Loop through all entries in the directory
    for (int i = 0; i < entries.length; i++) {
        File f = new File(d, entries[i]);
        if (f.isDirectory())
            continue; // Don't zip sub-directories
        FileInputStream in = new FileInputStream(f); // Stream to read file
        ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
        out.putNextEntry(entry); // Store entry
        while ((bytes_read = in.read(buffer)) != -1)
            // Copy bytes
            out.write(buffer, 0, bytes_read);
        in.close(); // Close input stream
    }
    // When we're done with the whole loop, close the output stream
    out.close();
}

From source file:FileCopy.java

public static void copy(String fromFileName, String toFileName) throws IOException {
    File fromFile = new File(fromFileName);
    File toFile = new File(toFileName);

    if (!fromFile.exists())
        throw new IOException("FileCopy: " + "no such source file: " + fromFileName);
    if (!fromFile.isFile())
        throw new IOException("FileCopy: " + "can't copy directory: " + fromFileName);
    if (!fromFile.canRead())
        throw new IOException("FileCopy: " + "source file is unreadable: " + fromFileName);

    if (toFile.isDirectory())
        toFile = new File(toFile, fromFile.getName());

    if (toFile.exists()) {
        if (!toFile.canWrite())
            throw new IOException("FileCopy: " + "destination file is unwriteable: " + toFileName);
        System.out.print("Overwrite existing file " + toFile.getName() + "? (Y/N): ");
        System.out.flush();/*from   w w w.jav a  2  s .co m*/
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String response = in.readLine();
        if (!response.equals("Y") && !response.equals("y"))
            throw new IOException("FileCopy: " + "existing file was not overwritten.");
    } else {
        String parent = toFile.getParent();
        if (parent == null)
            parent = System.getProperty("user.dir");
        File dir = new File(parent);
        if (!dir.exists())
            throw new IOException("FileCopy: " + "destination directory doesn't exist: " + parent);
        if (dir.isFile())
            throw new IOException("FileCopy: " + "destination is not a directory: " + parent);
        if (!dir.canWrite())
            throw new IOException("FileCopy: " + "destination directory is unwriteable: " + parent);
    }

    FileInputStream from = null;
    FileOutputStream to = null;
    try {
        from = new FileInputStream(fromFile);
        to = new FileOutputStream(toFile);
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1)
            to.write(buffer, 0, bytesRead); // write
    } finally {
        if (from != null)
            try {
                from.close();
            } catch (IOException e) {
                ;
            }
        if (to != null)
            try {
                to.close();
            } catch (IOException e) {
                ;
            }
    }
}

From source file:cc.arduino.utils.FileHash.java

/**
 * Calculate a message digest of a file using the algorithm specified. The
 * result is a string containing the algorithm name followed by ":" and by the
 * resulting hash in hex.//w  w w .  j  a  va 2  s.  c  om
 *
 * @param file
 * @param algorithm For example "SHA-256"
 * @return The algorithm followed by ":" and the hash, for example:<br />
 * "SHA-256:ee6796513086080cca078cbb383f543c5e508b647a71c9d6f39b7bca41071883"
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
public static String hash(File file, String algorithm) throws IOException, NoSuchAlgorithmException {
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        byte buff[] = new byte[10240];
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        while (in.available() > 0) {
            int read = in.read(buff);
            digest.update(buff, 0, read);
        }
        byte[] hash = digest.digest();
        String res = "";
        for (byte b : hash) {
            int c = b & 0xFF;
            if (c < 0x10)
                res += "0";
            res += Integer.toHexString(c);
        }
        return algorithm + ":" + res;
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:Main.java

public static boolean copyFile(File src, File dest) {
    if (!src.exists())
        return false;

    FileInputStream fis = null;
    FileOutputStream fos = null;/*  w w  w . j av a  2s  . c  o m*/
    try {
        fis = new FileInputStream(src);
        fos = new FileOutputStream(dest);

        byte[] buffer = new byte[2048];
        int bytesread = 0;
        while ((bytesread = fis.read(buffer)) != -1) {
            if (bytesread > 0)
                fos.write(buffer, 0, bytesread);
        }

        return true;

    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e2) {
            }
        }

        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e2) {
            }
        }
    }
}

From source file:Main.java

public static String checksum(String path, String algorithm) {
    MessageDigest md = null;/* www .j av a 2s .  co m*/

    try {
        md = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    FileInputStream i = null;

    try {
        i = new FileInputStream(path);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }

    byte[] buf = new byte[1024];
    int len = 0;

    try {
        while ((len = i.read(buf)) != -1) {
            md.update(buf, 0, len);
        }
    } catch (IOException e) {

    }

    byte[] digest = md.digest();

    // HEX
    StringBuilder sb = new StringBuilder();
    for (byte b : digest) {
        sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
    }

    return sb.toString();

}