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:Main.java

public static String setImBin(String fName) {
    FileInputStream fileInputStream = null;

    File file = new File(Environment.getExternalStorageDirectory().getPath(), "Pictures/" + fName);

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

    try {//ww  w . j av  a2 s  .c o m
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(bFile, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] readFromFile(File file) throws IOException {
    FileInputStream fin = null;
    try {//from   w w w  .j a  v a2s  .c  o m
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        fin = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        int read = 0;
        while ((read = fin.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        byte[] data = out.toByteArray();
        out.close();
        return data;
    } finally {
        if (fin != null) {
            try {
                fin.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:com.blockwithme.longdb.tools.Utils.java

/** Reads first 2 bytes, checks if its equal to GZIPInputStream.GZIP_MAGIC
 * //from   w  ww  . j  a va2  s .c o  m
 * @param theFile
 *        the file
 * @return true, if compressed
 * @throws Exception */
public static boolean compressed(final File theFile) throws Exception {
    // TODO: I the file name ends with .gz, then I think that we
    // don't need to check
    FileInputStream fis = null;
    try {
        fis = FileUtils.openInputStream(theFile);
        while (true) {
            final byte[] bytes = new byte[2];
            fis.read(bytes);
            final int head = (bytes[0] & BYTE_MASK) | ((bytes[1] << BYTE_BITS) & SECOND_BYTE_MASK);
            return head == GZIPInputStream.GZIP_MAGIC;
        }
    } finally {
        if (fis != null)
            fis.close();
    }
}

From source file:Main.java

public static byte[] getBytesFromFile(File f) {
    if (f == null) {
        return null;
    }//  w  ww  .  j  ava2s . com
    FileInputStream stream = null;
    ByteArrayOutputStream out = null;
    try {
        stream = new FileInputStream(f);
        out = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = stream.read(b)) != -1)
            out.write(b, 0, n);

        return out.toByteArray();
    } catch (IOException e) {
    } finally {
        try {
            if (stream != null)
                stream.close();
            if (out != null)
                out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return null;
}

From source file:com.compomics.pladipus.core.control.util.ZipUtils.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
        throws UnspecifiedPladipusException, FileNotFoundException, IOException {
    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {/*from  w w w. java 2 s.  c  om*/
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }
    }
}

From source file:Main.java

private static ByteArrayOutputStream readFileToStream(String file) {
    FileInputStream fileInputStream = null;
    try {/*from www . j  a  v a 2 s.  c  o m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        fileInputStream = new FileInputStream(file);
        int readedBytes;
        byte[] buf = new byte[1024];
        while ((readedBytes = fileInputStream.read(buf)) > 0) {
            bos.write(buf, 0, readedBytes);
        }
        return bos;
    } catch (Exception e) {
        return null;
    } finally {
        try {
            if (fileInputStream != null)
                fileInputStream.close();
        } catch (Exception ignored) {

        }
    }
}

From source file:com.sshtools.j2ssh.transport.publickey.SshPublicKeyFile.java

/**
 *
 *
 * @param keyfile/*from w ww.j a va2 s. c  o m*/
 *
 * @return
 *
 * @throws InvalidSshKeyException
 * @throws IOException
 */
public static SshPublicKeyFile parse(File keyfile) throws InvalidSshKeyException, IOException {
    FileInputStream in = new FileInputStream(keyfile);
    byte[] data = new byte[in.available()];
    in.read(data);
    in.close();

    return parse(data);
}

From source file:ch.entwine.weblounge.common.impl.util.TestUtils.java

/**
 * Loads the <code>XML</code> data from the specified file on the class path
 * and returns it after having stripped off any newlines, line breaks and
 * otherwise disturbing spaces and characters.
 * /*from w  w w .  j  a va  2s  . com*/
 * @param path
 *          the resource path
 * @return the contents of the resource
 */
public static String loadXmlFromResource(String path) {
    File templateFile = new File(TestUtils.class.getResource(path).getPath());
    String template = null;
    try {
        byte[] buffer = new byte[(int) templateFile.length()];
        FileInputStream f = new FileInputStream(templateFile);
        f.read(buffer);
        f.close();
        template = new String(buffer, "utf-8").replaceFirst("<\\?.*?>", "");
        template = template.replaceAll("(>\\s*)+", ">").replaceAll("(\\s*<)+", "<");
    } catch (IOException e) {
        throw new RuntimeException("Error reading test resource at " + path);
    }
    return template;
}

From source file:com.littcore.io.util.ZipUtils.java

/**
 * ?/*from www.ja v a 2s  . c  o  m*/
 * @param files 
 * @param targetFileNamePath 
 * @throws IOException
 */
public static void batchZip(File[] files, String targetFileNamePath) throws IOException {
    //?
    CheckedOutputStream cs = new CheckedOutputStream(new FileOutputStream(targetFileNamePath), new CRC32());
    //zip?
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
    File srcFile = null;
    for (int i = 0; i < files.length; i++) {
        srcFile = files[i];
        out.putNextEntry(new ZipEntry(srcFile.getName()));
        FileInputStream in = new FileInputStream(srcFile);
        int len;
        byte[] buf = new byte[BUFFERED_SIZE];
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
}

From source file:Main.java

public static void copyFile(String src, String dst) {
    FileInputStream in = null;
    FileOutputStream out = null;/* www  .  ja  v  a  2  s.c  o m*/
    try {
        in = new FileInputStream(src);
        out = new FileOutputStream(dst);
        byte[] b = new byte[1024 * 5];
        int len = 0;
        while ((len = in.read(b)) > 0) {
            out.write(b, 0, len);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}