Example usage for java.nio.file Files readAllBytes

List of usage examples for java.nio.file Files readAllBytes

Introduction

In this page you can find the example usage for java.nio.file Files readAllBytes.

Prototype

public static byte[] readAllBytes(Path path) throws IOException 

Source Link

Document

Reads all the bytes from a file.

Usage

From source file:org.ulyssis.ipp.config.Config.java

/**
 * Create a configuration from the given configuration file.
 * /*from  w w w  . ja v  a  2s  .c om*/
 * Expects the configuration file to be UTF-8 formatted.
 */
public static Optional<Config> fromConfigurationFile(Path configFile) {
    try {
        byte[] config = Files.readAllBytes(configFile);
        Charset charset = Charset.forName("utf-8");
        String configString = charset.decode(ByteBuffer.wrap(config)).toString();
        return fromConfigurationString(configString);
    } catch (IOException e) {
        LOG.error("Error reading configuration file: {}", configFile, e);
        return Optional.empty();
    }
}

From source file:cc.kave.commons.externalserializationtests.ExternalTestCaseProvider.java

private static List<TestCase[]> getTestCasesInCurrentFolder(File currentDirectory, String rootPrefix)
        throws ClassNotFoundException, IOException {
    List<TestCase[]> testCases = new LinkedList<TestCase[]>();

    Class<?> serializedType = Object.class;
    String expectedCompact = null;
    String expectedFormatted = null;

    for (File file : currentDirectory.listFiles()) {
        if (file.getName().equals(settingsFile)) {
            String className;/* ww  w  .  ja  v  a  2s  .  c  o  m*/
            className = TestSettingsReader.readSection(file, "Java").get(ExternalTestSetting.SerializedType);
            serializedType = Class.forName(className);
        }

        if (file.getName().equals(expectedCompactFile)) {
            expectedCompact = new String(Files.readAllBytes(file.toPath()));
        }

        if (file.getName().equals(expectedFormattedFile)) {
            expectedFormatted = new String(Files.readAllBytes(file.toPath()));
        }
    }

    if (expectedCompact == null) {
        return new LinkedList<TestCase[]>();
    }

    for (File file : currentDirectory.listFiles()) {
        boolean isInputFile = !file.getName().equals(settingsFile)
                && !file.getName().equals(expectedCompactFile) && !file.getName().equals(expectedFormattedFile);
        if (!isInputFile) {
            continue;
        }

        String input = new String(Files.readAllBytes(file.toPath()));
        testCases.add(new TestCase[] { new TestCase(getTestCaseName(file.getAbsolutePath(), rootPrefix),
                serializedType, input, expectedCompact, expectedFormatted) });
    }

    return testCases;
}

From source file:br.unb.bionimbuz.storage.bucket.methods.CloudMethodsAmazonGoogle.java

@Override
public void StorageAuth(StorageProvider sp) throws Exception {

    switch (sp) {

    case AMAZON: {

        byte[] encoded = Files.readAllBytes(Paths.get(authFolder + "AwsCredentials.txt"));
        String fileContent = new String(encoded, Charset.defaultCharset());
        //System.out.println("AuthString: " + fileContent);
        String accessKeyID, accessKey;
        int delimiter = fileContent.indexOf(':');
        accessKeyID = fileContent.substring(0, delimiter);
        accessKey = fileContent.substring(delimiter + 1);
        AWSCredentials credentials = new BasicAWSCredentials(accessKeyID, accessKey);
        s3client = new AmazonS3Client(credentials);

        break;//from  www  .  j av  a  2 s. co  m
    }
    case GOOGLE: {

        String command = gcloudFolder + "gcloud auth activate-service-account --key-file=" + authFolder
                + "GoogleCredentials.json";
        ExecCommand(command);

        break;
    }
    default: {
        throw new Exception("Provedor incorreto!");
    }
    }
}

From source file:br.unb.cic.bionimbuz.storage.bucket.methods.CloudMethodsAmazonGoogle.java

@Override
public void StorageAuth(StorageProvider sp) throws Exception {

    switch (sp) {

    case AMAZON: {

        byte[] encoded = Files.readAllBytes(Paths.get(authFolder + "accesskey.txt"));
        String fileContent = new String(encoded, Charset.defaultCharset());
        //System.out.println("AuthString: " + fileContent);
        String accessKeyID, accessKey;
        int delimiter = fileContent.indexOf(':');
        accessKeyID = fileContent.substring(0, delimiter);
        accessKey = fileContent.substring(delimiter + 1);
        AWSCredentials credentials = new BasicAWSCredentials(accessKeyID, accessKey);
        s3client = new AmazonS3Client(credentials);

        break;/*from   w ww.j  av  a 2  s.c o m*/
    }
    case GOOGLE: {

        String command = gcloudFolder + "gcloud auth activate-service-account --key-file=" + authFolder
                + "cred.json";
        ExecCommand(command);

        break;
    }
    default: {
        throw new Exception("Provedor incorreto!");
    }
    }
}

From source file:com.laborun.servlet.UploadMyFile.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    //        System.out.println(request.getParameter("myfile"));
    if (ServletFileUpload.isMultipartContent(request)) {
        System.out.println("radwa");
        MultipartRequest m = new MultipartRequest(request, "E:\\ITI Work\\Projects\\LaboRunEtc\\New folder");

        Path path = Paths.get("E:\\ITI Work\\Projects\\LaboRunEtc\\sad.txt");
        byte[] array = Files.readAllBytes(path);
        System.out.println(array.length);
        AssignmentFiles assignment = new AssignmentFiles();
        assignment.setUploadFile(array);
        assignment.setUploadDate(new Date());

        AssignmentDaoInt upload = new AssignmentDaoImp();
        upload.uploadAssignment(assignment);

        //           response.sendRedirect("http://localhost:8082/trainee/buttons.jsp");
    }/*from  w w  w  .jav  a  2 s .  c o m*/
}

From source file:com.compomics.colims.core.io.colims_to_utilities.ColimsSpectrumMapperTest.java

/**
 * Test of map method, of class ColimsSpectrumMapper.
 * @throws java.lang.Exception in case of an exception
 *///from  ww w .j  a  v  a 2s . c om
@Test
public void testMap() throws Exception {
    Spectrum spectrum = new Spectrum();
    File inputSpectra = new ClassPathResource("data/input_spectra_Z.mgf.gz").getFile();
    byte[] data = Files.readAllBytes(inputSpectra.toPath());
    SpectrumFile spectrumFile = new SpectrumFile();
    spectrumFile.setContent(data);
    List<SpectrumFile> spectrumFileList = new ArrayList<>();
    spectrumFileList.add(spectrumFile);

    spectrum.setAccession("fake spectrum 1");
    spectrum.setFragmentationType(FragmentationType.CID);
    spectrum.setIntensity(1233123.32);
    spectrum.setRetentionTime(2123.1);
    spectrum.setMzRatio(123.12);
    spectrum.setCharge(3);
    spectrum.setSpectrumFiles(spectrumFileList);
    spectrum.setScanNumber(12L);
    spectrum.setScanTime(11231.1);
    spectrum.setPeptides(new ArrayList<>());

    MSnSpectrum targetSpectrum = new MSnSpectrum();
    colimsSpectrumMapper.map(spectrum, targetSpectrum);
    Assert.assertEquals(677, targetSpectrum.getPeakList().size());
    Assert.assertEquals("12", targetSpectrum.getScanNumber());
    Assert.assertEquals(1233123.32, targetSpectrum.getPrecursor().getIntensity(), 0.01);
    Assert.assertEquals(123.12, targetSpectrum.getPrecursor().getMz(), 0.01);
    Assert.assertEquals(new Charge(1, 3), targetSpectrum.getPrecursor().getPossibleCharges().get(0));
}

From source file:com.netflix.spinnaker.echo.pubsub.utils.MessageArtifactTranslator.java

public MessageArtifactTranslator(String templatePath) {
    if (!StringUtils.isEmpty(templatePath)) {
        try {/*w ww .  j a  v  a2 s . co  m*/
            jinjaTemplate = new String(Files.readAllBytes(Paths.get(templatePath)));
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }
}

From source file:com.arcanix.php.phar.PharEntry.java

public PharEntry(final File file, final String localPath, final PharCompression pharCompression)
        throws IOException {
    this.file = file;
    this.localPath = localPath;
    this.pharCompression = pharCompression;

    byte[] uncompressedBytes = Files.readAllBytes(this.file.toPath());
    this.checksum = new CRC32();
    this.checksum.update(uncompressedBytes);

    ByteArrayOutputStream compressed = new ByteArrayOutputStream();

    OutputStream compressor = null;
    try {//  w ww  . ja va  2  s.  c  o m
        compressor = getCompressorOutputStream(compressed);
        Files.copy(this.file.toPath(), compressor);
        compressor.flush();
    } finally {
        compressor.close();
    }

    this.compressedBytes = compressed.toByteArray();
}

From source file:common.Util.java

public static byte[] readFile(File file) {
    try {/*  w  ww . j  a  v  a  2 s .c o  m*/
        return Files.readAllBytes(file.toPath());
    } catch (Exception e) {
        System.out.println("Could not read file " + file + ": " + e);
        return null;
    }
}