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

private static SSLEngineConfigurator createSslConfiguration() throws IOException {
    // Initialize SSLContext configuration
    SSLContextConfigurator sslContextConfig = new SSLContextConfigurator();
    if (Strings.isNotBlank(SETTINGS.certificatePath)) {
        sslContextConfig.setKeyStoreBytes(FileUtils.readFileToByteArray(new File(SETTINGS.certificatePath)));
        sslContextConfig.setKeyStorePass(SETTINGS.certificatePass);
    }//from   ww w  .ja  v a 2s  . c om

    // Create SSLEngine configurator
    return new SSLEngineConfigurator(sslContextConfig.createSSLContext(), false, false, false);
}

From source file:com.cloudant.sync.replication.CouchClientWrapperTest.java

@Before
public void setup() throws IOException {
    remoteDb = new CouchClientWrapper(CLOUDANT_TEST_DB_NAME, super.getCouchConfig());
    bodyOne = DocumentBodyFactory.create(FileUtils.readFileToByteArray(new File(documentOneFile)));
    bodyTwo = DocumentBodyFactory.create(FileUtils.readFileToByteArray(new File(documentTwoFile)));

    CouchClientWrapperDbUtils.deleteDbQuietly(remoteDb);
    remoteDb.createDatabase();//from ww w  . j a  v  a 2  s  .c  o m
}

From source file:com.logisima.selenium.server.action.StaticAction.java

@Override
public void execute() {
    this.setContentType();
    try {//  w w  w  . ja v  a2s . c om
        if (request.getUri().endsWith(".png") | request.getUri().endsWith(".gif")) {
            File image = new File(documentRoot.getAbsolutePath() + request.getUri());
            this.content = ChannelBuffers.copiedBuffer(FileUtils.readFileToByteArray(image));
        } else {
            // Process the request
            StringBuilder buf = new StringBuilder();
            buf.setLength(0);
            Scanner scanner = null;
            String fileName = documentRoot.getAbsolutePath() + request.getUri().split("[?]")[0];
            String NL = System.getProperty("line.separator");
            scanner = new Scanner(new FileInputStream(fileName), "utf-8");
            while (scanner.hasNextLine()) {
                buf.append(scanner.nextLine() + NL);
            }
            this.content = ChannelBuffers.copiedBuffer(buf.toString(), CharsetUtil.UTF_8);
        }
    } catch (FileNotFoundException e) {
        this.status = HttpResponseStatus.NOT_FOUND;
        this.content = ChannelBuffers.copiedBuffer(e.toString(), CharsetUtil.UTF_8);
    } catch (IOException e) {
        this.status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
        this.content = ChannelBuffers.copiedBuffer(e.toString(), CharsetUtil.UTF_8);
    }
}

From source file:com.github.imgur.api.UploadRequestTest.java

@Test
public void can_build_with_byte_array() throws IOException {

    final File image = new File("./src/test/resources/uploadme.jpg");
    byte[] array = FileUtils.readFileToByteArray(image);

    UploadRequest.Builder builder = new UploadRequest.Builder();

    UploadRequest request = builder.withImageData(array).build();
    String imageData = (String) request.buildParameters().get("image");
    assertThat(imageData).doesNotContain("http://");
}

From source file:com.sap.hana.cloud.samples.jenkins.storage.FileStorageTest.java

@Test
public void testStoreStreamUpdatesExistingDocumentAndDoesNotTryToCreateNewOne() throws Exception {
    final ByteArrayInputStream input = new ByteArrayInputStream("hello".getBytes());
    makeConfigurationExisting("huch".getBytes());
    storage.save(input);//w  w w  . j a  va2s  . c  o m
    final File configFile = new File(tempDir.getRoot(), "configuration.zip");
    assertTrue(configFile.exists());
    assertArrayEquals("hello".getBytes(), FileUtils.readFileToByteArray(configFile));
}

From source file:net.darkmist.alib.io.Slurp.java

public static byte[] slurp(String file) throws IOException {
    return FileUtils.readFileToByteArray(new File(file));
    //return slurp(new File(file));
}

From source file:br.com.fabiopereira.quebrazip.JavaZipSplitter.java

private static void split() {
    try {/*from   ww w.j a  v a  2  s  .  c  o  m*/
        byte[] bytes = FileUtils.readFileToByteArray(new File(BIN_PATH));
        // Nmero de arquivos inteiros do tamanho bytesSize
        int numFilesInteiros = (bytes.length / bytesSize);
        // Calculando a sobra...(ultimo arquivo)
        int kByteSizeUltimoArquivo = bytes.length - bytesSize * numFilesInteiros;
        System.out.println("Nmero de arquivos que sero gerados: "
                + (numFilesInteiros + (kByteSizeUltimoArquivo != 0 ? 1 : 0)));
        System.out.println("Tamanhos: ");
        for (int i = 0; i < numFilesInteiros; i++) {
            System.out.println("File " + i + ": " + bytesSize);
        }
        System.out.println("File " + numFilesInteiros + ": " + kByteSizeUltimoArquivo);
        // Escrevendo arquivos inteiros
        byte[][] b = new byte[numFilesInteiros][bytesSize];
        int posicaoInicial = 0;
        for (int i = 0; i < numFilesInteiros; i++) {
            // Um arquivo inteiro
            int j = 0;
            for (j = posicaoInicial; j < posicaoInicial + bytesSize; j++) {
                b[i][j - posicaoInicial] = (bytes[j]);
            }
            posicaoInicial = j;
            invertArrayBytes(b[i]);
            FileUtils.writeByteArrayToFile(
                    new File(BIN_PATH.replaceAll(".zip", ".txt") + (i < 10 ? "0" + i : i)), b[i]);
        }
        // Escrevendo ultimo arquivo
        if (kByteSizeUltimoArquivo > 0) {
            byte[] ultimoArquivo = new byte[kByteSizeUltimoArquivo];
            for (int i = posicaoInicial; i < posicaoInicial + kByteSizeUltimoArquivo; i++) {
                ultimoArquivo[i - posicaoInicial] = (bytes[i]);
            }
            invertArrayBytes(ultimoArquivo);
            FileUtils.writeByteArrayToFile(
                    new File(BIN_PATH.replaceAll(".zip", ".txt")
                            + (numFilesInteiros < 10 ? "0" + numFilesInteiros : numFilesInteiros)),
                    ultimoArquivo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.cloudant.sync.datastore.DatastoreImplForceInsertTest.java

@Before
public void setUp() throws Exception {
    database_dir = TestUtils.createTempTestingDir(DatastoreImplForceInsertTest.class.getName());
    datastore = new DatastoreImpl(database_dir, "test");

    jsonData = FileUtils.readFileToByteArray(TestUtils.loadFixture(documentOneFile));
    bodyOne = new DocumentBodyImpl(jsonData);

    jsonData = FileUtils.readFileToByteArray(TestUtils.loadFixture(documentTwoFile));
    bodyTwo = new DocumentBodyImpl(jsonData);
}

From source file:com.thoughtworks.go.utils.encryption.Encrypter.java

private void process() throws IOException, InvalidCipherTextException {
    if (verifyCipher()) {
        System.out.println("Using cipher file at " + cipherLocation);
        System.out.println("Enter plain text password: ");
        char[] password = System.console().readPassword();
        System.out.println("Confirm plain text password: ");
        char[] confirmPassword = System.console().readPassword();
        if (validateConfirmation(password, confirmPassword)) {
            File cipherFile = new File(cipherLocation);
            String cipherText = cipher(FileUtils.readFileToByteArray(cipherFile),
                    String.valueOf(confirmPassword));
            System.out.println(String.format("Encrypted text: %s", cipherText));
        } else {/*from   w ww  . j a v a 2 s .  com*/
            System.err.println("Password and confirmation do not match. Aborting...");
        }
    } else {
        throw new RuntimeException("Could not find cipher file at " + cipherLocation);
    }
}

From source file:com.endgame.binarypig.util.BuildSequenceFileFromDir.java

@Override
public int run(String[] args) throws Exception {

    File inDir = new File(args[0]);
    Path name = new Path(args[1]);

    Text key = new Text();
    BytesWritable val = new BytesWritable();

    Configuration conf = getConf();
    FileSystem fs = FileSystem.get(conf);
    SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, name, Text.class, BytesWritable.class,
            CompressionType.RECORD);//from   www  . j  a  va 2  s.  co  m

    for (File file : inDir.listFiles()) {
        if (!file.isFile()) {
            System.out.println("Skipping " + file + " (not a file) ...");
            continue;
        }

        byte[] bytes = FileUtils.readFileToByteArray(file);
        val.set(bytes, 0, bytes.length);
        key.set(DigestUtils.md5Hex(bytes));
        writer.append(key, val);
    }
    writer.close();

    return 0;
}