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:com.eucalyptus.auth.crypto.StringCryptoTest.java

private static byte[] readfile(String filename) throws Exception {
    FileInputStream fis = new FileInputStream(filename);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] block = new byte[512];
    int n;// ww  w  .  j a  va  2 s . c om
    while ((n = fis.read(block)) > 0) {
        baos.write(block, 0, n);
    }
    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;
}

From source file:Main.java

private static byte[] getFileBytes(String filePath) {
    byte[] buffer = null;
    try {/*w w  w . j  a va  2  s  .c  o  m*/
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
    return buffer;
}

From source file:Main.java

public static String compress(String filename) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[4096];
    int bytesRead;
    String[] entries = { ".mp4" };
    String zipfile = filename.replace(".mp4", ".zip");
    if (!(new File(zipfile)).exists()) {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        out.setLevel(Deflater.BEST_COMPRESSION);
        out.setMethod(ZipOutputStream.DEFLATED);
        for (int i = 0; i < entries.length; i++) {
            File f = new File(filename.replace(".mp4", entries[i]));
            if (f.exists()) {
                FileInputStream in = new FileInputStream(f);
                ZipEntry entry = new ZipEntry(f.getName());
                out.putNextEntry(entry);
                while ((bytesRead = in.read(buffer)) != -1)
                    out.write(buffer, 0, bytesRead);
                in.close();/*  w w  w  . j a v a 2 s  .  co  m*/
            }
        }
        out.close();
    }
    return zipfile;
}

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("Not a directory:  " + dir);
    String[] entries = d.list();//from   w  w  w.j  a va  2  s  .c  o m
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

    for (int i = 0; i < entries.length; i++) {
        File f = new File(d, entries[i]);
        if (f.isDirectory())
            continue;//Ignore directory
        FileInputStream in = new FileInputStream(f); // Stream to read file
        ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
        out.putNextEntry(entry); // Store entry
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
        in.close();
    }
    out.close();
}

From source file:com.grapeshot.halfnes.FileUtils.java

public static int[] readfromfile(final String path) {
    File f = new File(path);
    byte[] bytes = new byte[(int) f.length()];
    FileInputStream fis;
    try {/*from  w  ww.ja v a2s  . c  o  m*/
        fis = new FileInputStream(f);
        fis.read(bytes);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.println("Failed to load file");
        e.printStackTrace();
    }
    int[] ints = new int[bytes.length];

    for (int i = 0; i < bytes.length; i++) {
        ints[i] = (short) (bytes[i] & 0xFF);
    }

    return ints;
}

From source file:Main.java

public static synchronized boolean downloadFileMD5Check(File f, String expectedMD5) {
    boolean flag = false;
    try {// www. j a  v  a 2  s  .  com
        MessageDigest md = MessageDigest.getInstance("MD5");
        FileInputStream fis = new FileInputStream(f);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = fis.read(b)) != -1) {
            md.update(b, 0, len);
        }

        if (md5(md).equals(expectedMD5)) {
            flag = true;
        }
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return flag;
}

From source file:com.redsqirl.workflow.utils.FileStream.java

public static byte[] decryptFile(File in) throws Exception {
    //Decrypt the file data:
    byte[] encData;

    //Read in the file:
    FileInputStream inStream = new FileInputStream(in);
    encData = new byte[(int) in.length()];
    inStream.read(encData);
    inStream.close();//from  w w w . ja  va 2s . co  m

    byte[] decData = decrypt(encData);

    //Figure out how much padding to remove
    int padCount = (int) decData[decData.length - 1];

    //Naive check, will fail if plaintext file actually contained
    //this at the end
    //For robust check, check that padCount bytes at the end have same value
    if (padCount >= 1 && padCount <= 8) {
        decData = Arrays.copyOfRange(decData, 0, decData.length - padCount);
    }

    //Write the decrypted data to a new file:
    return decData;
}

From source file:it.geosolutions.imageio.plugins.nitronitf.NITFImageWriterSpi.java

public static boolean isNITF(File file) {
    FileInputStream fin = null;
    try {/* ww w  . j  a va2s  .c om*/
        fin = new FileInputStream(file);
        byte[] firstFour = new byte[4];
        fin.read(firstFour);
        return new String(firstFour).equals("NITF");
    } catch (IOException e) {
        return false;
    } finally {
        try {
            if (fin != null)
                fin.close();
        } catch (IOException e) {
            log.error(ExceptionUtils.getStackTrace(e));
        }
    }
}

From source file:FileUtil.java

public final static byte[] load(FileInputStream fin) {
    byte readBuf[] = new byte[512 * 1024];

    try {//from ww  w  . j  av  a  2s  . c om
        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        int readCnt = fin.read(readBuf);
        while (0 < readCnt) {
            bout.write(readBuf, 0, readCnt);
            readCnt = fin.read(readBuf);
        }

        fin.close();

        return bout.toByteArray();
    } catch (Exception e) {

        return new byte[0];
    }
}

From source file:Main.java

public static byte[] getBytes(String filePath) {
    byte[] buffer = null;
    try {//from  w w  w  .j  a v a 2s .c  o m
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}