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:com.ejisto.util.IOUtils.java

public static byte[] readFile(File file) throws IOException {
    return Files.readAllBytes(file.toPath());
}

From source file:com.sastix.cms.server.services.content.ResourceServiceTest.java

@Test
public void shouldCreateResourceFromExternalUri() throws Exception {
    URL localFile = getClass().getClassLoader().getResource("./logo.png");
    CreateResourceDTO createResourceDTO = new CreateResourceDTO();
    createResourceDTO.setResourceAuthor("Demo Author");
    createResourceDTO.setResourceExternalURI(localFile.getProtocol() + "://" + localFile.getFile());
    createResourceDTO.setResourceMediaType("image/png");
    createResourceDTO.setResourceName("logo.png");
    createResourceDTO.setResourceTenantId("zaq12345");
    ResourceDTO resourceDTO = resourceService.createResource(createResourceDTO);

    String resourceUri = resourceDTO.getResourceURI();

    //Extract TenantID
    final String tenantID = resourceUri.substring(resourceUri.lastIndexOf('-') + 1, resourceUri.indexOf("/"));

    final Path responseFile = hashedDirectoryService.getDataByURI(resourceUri, tenantID);
    File file = responseFile.toFile();

    byte[] actualBytes = Files.readAllBytes(file.toPath());
    byte[] expectedBytes = Files.readAllBytes(Paths.get(localFile.getPath()));
    Assert.assertArrayEquals(expectedBytes, actualBytes);
}

From source file:com.alliander.osgp.shared.security.EncryptionService.java

@PostConstruct
private void initEncryption() {
    try {// w w w .j  av a 2s. c o  m
        this.key = new SecretKeySpec(Files.readAllBytes(new File(this.keyPath).toPath()), SECRET_KEY_SPEC);
    } catch (final IOException e) {
        LOGGER.error(UNEXPECTED_EXCEPTION_WHEN_READING_KEY, e);
        throw new EncrypterException(UNEXPECTED_EXCEPTION_WHEN_READING_KEY, e);
    }
}

From source file:com.ontotext.s4.TwitterVisualization.processingTweets.ProcessingTweets.java

/**
 * Process all documents into the raw tweets folder. Save processed tweets
 * into the processed tweets Folder./*from w w w.  j  a  va 2  s .co  m*/
 */
public void ProcessTweets() {

    // get all documents in a folder
    File f = new File(rawTweetsFolder);
    File[] documents = f.listFiles();

    //
    if (documents == null) {
        logger.info("There are no files to process");
        return;
    }

    // the output
    File results = new File(processedTweetsFolder);
    if (!results.exists()) {
        results.mkdirs();
    }

    Writer w = null;
    for (File document : documents) {
        try {

            if (document.isDirectory() || !document.canRead()) {
                continue;
            }

            logger.info("Just send " + document.getName() + " for processing.");
            // annotate each file
            String result = ProcessThisTweet(new String(Files.readAllBytes(document.toPath())));

            logger.info("Received " + document.getName());
            w = new OutputStreamWriter(new FileOutputStream(processedTweetsFolder + "/" + document.getName()));
            w.append(result);

        } catch (FileNotFoundException e) {
            logger.debug(e);
        } catch (IOException e) {
            logger.debug(e);
        } finally {
            try {
                w.close();
            } catch (IOException e) {
                logger.debug(e);
            } catch (Exception e2) {
                logger.debug(e2);
            }
        }
    }
}

From source file:com.ejisto.modules.vertx.handler.Boilerplate.java

private static void internalServeResource(HttpServerResponse response, Path resourcePath, String contentType) {
    try {//from   www.  j a  v a2 s  . c o  m
        Objects.requireNonNull(resourcePath);
        final byte[] resourceBytes = Files.readAllBytes(resourcePath);
        Buffer b = new Buffer(resourceBytes);
        response.putHeader(HttpHeaders.CONTENT_TYPE, contentType)
                .putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(resourceBytes.length)).write(b).end();
    } catch (IOException e) {
        response.setStatusCode(HttpResponseStatus.NOT_FOUND.code());
        response.setStatusMessage(e.getMessage());
    }
}

From source file:fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.java

/**
 * Reads a mapping from dir/version/type.json file
 *
 * @param dir Directory containing mapping files per major version
 * @param version Elasticsearch major version number (only major digit is kept so for 2.3.4 it will be 2)
 * @param type The expected type (will be expanded to type.json)
 * @return the mapping//from  w  ww  .j  av a  2 s . c  o m
 * @throws IOException If the mapping can not be read
 */
private static String readJsonVersionedFile(Path dir, String version, String type) throws IOException {
    Path file = dir.resolve(version).resolve(type + ".json");
    return new String(Files.readAllBytes(file), "UTF-8");
}

From source file:org.createnet.raptor.db.mapdb.MapDBConnectionTest.java

protected ObjectNode loadData(String filename) throws IOException {

    String filepath = filename + ".json";
    URL res = getClass().getClassLoader().getResource(filepath);

    if (res == null) {
        throw new IOException("Cannot load " + filepath);
    }// w w w . j  a  v a 2 s  . c  om

    String strpath = res.getPath();

    Path path = Paths.get(strpath);
    byte[] content = Files.readAllBytes(path);

    return (ObjectNode) mapper.readTree(content);
}

From source file:ru.mystamps.web.service.FilesystemImagePersistenceStrategy.java

protected byte[] toByteArray(Path dest) throws IOException {
    return Files.readAllBytes(dest);
}

From source file:ch.sportchef.business.event.bundary.EventImageResourceTest.java

private byte[] readTestImage() throws IOException {
    final File file = new File(getClass().getClassLoader().getResource(TEST_IMAGE_NAME).getFile());
    return Files.readAllBytes(file.toPath());
}

From source file:org.trustedanalytics.h2oscoringengine.publisher.steps.AppBitsUploadingStepTest.java

private HttpEntity<MultiValueMap<String, Object>> createTestAppBitsRequest() throws IOException {
    HttpEntity<String> resourcesPart = new HttpEntity<String>("[]");
    HttpEntity<ByteArrayResource> dataPart = new HttpEntity<>(
            new ByteArrayResource(Files.readAllBytes(testAppBitsPath)), HttpCommunication.zipHeaders());

    MultiValueMap<String, Object> multiPartRequest = new LinkedMultiValueMap<>();
    multiPartRequest.add("resources", resourcesPart);
    multiPartRequest.add("application", dataPart);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);

    return new HttpEntity<>(multiPartRequest, headers);
}