Java File to Byte Array getBytes(File archiveFile)

Here you can find the source of getBytes(File archiveFile)

Description

DOC amaumont Comment method "getBytes".

License

Open Source License

Parameter

Parameter Description
archiveFile a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static byte[] getBytes(File archiveFile) throws IOException 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

public class Main {
    /**//from  www.j a va 2 s.  c om
     * DOC amaumont Comment method "getBytes".
     * 
     * @param archiveFile
     * @throws IOException
     */
    public static byte[] getBytes(File archiveFile) throws IOException {
        long length = archiveFile.length();

        int lengthFinalSize = 0;

        if (length > Integer.MAX_VALUE) {
            throw new IllegalStateException("Capacity is over !");
        } else {
            lengthFinalSize = (int) length;
        }

        FileInputStream fis = new FileInputStream(archiveFile);
        int bufferSize = 1024;
        byte[] buf = new byte[bufferSize];
        int readBytes = 0;

        ByteArrayOutputStream bos = new ByteArrayOutputStream(lengthFinalSize);

        while ((readBytes = fis.read(buf)) != -1) {
            bos.write(buf, 0, readBytes);
        }

        return bos.toByteArray();
    }
}

Related

  1. fileToBytes(File file)
  2. fileToBytes(File path)
  3. fileToBytes(File source)
  4. fileToBytes(final File f)
  5. getBytes(File aFile)
  6. getBytes(File contentFile)
  7. getBytes(File f)
  8. getBytes(File f)
  9. getBytes(File f)