Example usage for org.apache.commons.io FileUtils readFileToByteArray

List of usage examples for org.apache.commons.io FileUtils readFileToByteArray

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils readFileToByteArray.

Prototype

public static byte[] readFileToByteArray(File file) throws IOException 

Source Link

Document

Reads the contents of a file into a byte array.

Usage

From source file:com.cloudant.sync.util.TestUtils.java

public static DocumentBody createBDBody(String filePath) throws IOException {
    byte[] data = FileUtils.readFileToByteArray(new File(filePath));
    return DocumentBodyFactory.create(data);

}

From source file:edu.psu.citeseerx.utility.FileDigest.java

/**
 * Gets file content //from   www. ja v a 2  s  . com
 * @param file   File to read
 * @return   a <code>byte[]</code> with the file content
 * @throws   RuntimeException if a problem happens
 */
private static byte[] getFileContent(File file) {
    try {
        if (file.isDirectory()) {
            throw new RuntimeException(file.getAbsolutePath() + " must be a file not a directory");
        }
        return (FileUtils.readFileToByteArray(file));
    } catch (IOException ioE) {
        throw new RuntimeException(ioE.getMessage());
    }
}

From source file:com.huawei.ais.demo.ocr.DriverLicenseDemo.java

private static void driverLicenseDemo() throws IOException {
    ///*from  ww  w. ja v a  2 s .  c  om*/
    // 1. ClientContextUtils, ?AIS??, 
    // ??
    // 
    AisAccess service = new AisAccess(ClientContextUtils.getAuthInfo());

    //
    // 1.a ???AIS????, 
    // ???ClientContextUtils????(ProxyHostInfo)
    //
    //AisAccess service = new AisAccessWithProxy(ClientContextUtils.getAuthInfo(), ClientContextUtils.getProxyHost());

    try {
        //
        // 2.????
        //
        String uri = "/v1.0/ocr/driver-license";
        byte[] fileData = FileUtils.readFileToByteArray(new File("data/driver-license-demo.png"));
        String fileBase64Str = Base64.encodeBase64String(fileData);

        JSONObject json = new JSONObject();
        json.put("image", fileBase64Str);
        StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");

        // 3.??uri?, ????
        // ??JSON?, POST?
        HttpResponse response = service.post(uri, stringEntity);

        // 4.?????200, ?, ?
        ResponseProcessUtils.processResponseStatus(response);

        // 5.???
        ResponseProcessUtils.processResponse(response);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        // 6.?
        service.close();
    }
}

From source file:com.huawei.ais.demo.ocr.VehicleLicenseDemo.java

private static void vehicleLicenseDemo() throws IOException {
    //// w  w w  .j  av  a2s  . co m
    // 1. ClientContextUtils, ?AIS??, 
    // ??
    //       
    AisAccess service = new AisAccess(ClientContextUtils.getAuthInfo());

    //
    // 1.a ???AIS????, 
    // ???ClientContextUtils????(ProxyHostInfo)
    //
    //AisAccess service = new AisAccessWithProxy(ClientContextUtils.getAuthInfo(), ClientContextUtils.getProxyHost());

    try {
        //
        // 2.????
        //
        String uri = "/v1.0/ocr/vehicle-license";
        byte[] fileData = FileUtils.readFileToByteArray(new File("data/vehicle-license-demo.png"));
        String fileBase64Str = Base64.encodeBase64String(fileData);

        JSONObject json = new JSONObject();
        json.put("image", fileBase64Str);
        StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");

        // 3.??uri?, ????
        // ??JSON?, POST?
        HttpResponse response = service.post(uri, stringEntity);

        // 4.?????200, ?, ?
        ResponseProcessUtils.processResponseStatus(response);

        // 5.???
        ResponseProcessUtils.processResponse(response);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        // 6.?
        service.close();
    }
}

From source file:gov.nih.nci.cacis.common.test.TestUtils.java

/**
 * Returns contents of a file as byte array
 *
 * @param fileName - URL of the source file
 * @return byte[] - content of the file as byte[]
 * @throws java.io.IOException        - IOExpetion thrown
 * @throws java.net.URISyntaxException - URISyntaxException thrown
 *//*w ww  .  j a v a2 s.c o m*/
public static byte[] getBytesFromFile(URL fileName) throws IOException, URISyntaxException {
    return FileUtils.readFileToByteArray(new File(fileName.toURI()));
}

From source file:com.mirth.connect.plugins.datatypes.dicom.test.DICOMSerializerTest.java

@Test
public void testToXml1() throws Exception {
    String input = Base64//from   ww w . ja v a2 s  .c  o  m
            .encodeBase64String(FileUtils.readFileToByteArray(new File("tests/test-dicom-input-1.dcm")));
    String output = FileUtils.readFileToString(new File("tests/test-dicom-output-1.xml"));
    DICOMSerializer serializer = new DICOMSerializer();
    Assert.assertEquals(output, TestUtil.prettyPrintXml(serializer.toXML(input)));
}

From source file:buckley.maven.FileUtil.java

public byte[] read(File file) {
    try {/*w  w w. j  ava  2 s.c om*/
        return FileUtils.readFileToByteArray(file);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.greenpepper.server.license.LicenceGenerator.java

private static void buildOpenSource() throws Exception {
    File file = File.createTempFile("opensource", ".lic");
    License license = License.openSource("My Open Source Project", _2006, _2006);
    LicenseManager lm = new LicenseManager(getLicenseParam());
    lm.store(license, file);/* w w w  . java  2s. co  m*/
    if (deleteFiles)
        file.deleteOnExit();
    System.out.println("# Open source");
    System.out.println(new String(Base64.encodeBase64(FileUtils.readFileToByteArray(file))));
    System.out.println("");
}

From source file:name.martingeisse.esdk.util.ImmutableByteArray.java

/**
 * Creates an instance by loading a file.
 * //  w  w  w.  j a v  a 2s .  c  om
 * @param file the file to load
 * @throws IOException on I/O errors
 */
public ImmutableByteArray(final File file) throws IOException {
    this(FileUtils.readFileToByteArray(file), false);
}

From source file:com.clariano.avaj.ByteStreamStructureLearner.java

public ByteStreamStructureLearner(File classFile) {
    this.classfile = classFile;
    try {/*from  w  w w  .ja va 2 s  .c o  m*/
        bytes = FileUtils.readFileToByteArray(this.classfile);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}