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.createnet.raptor.indexer.impl.ElasticSearchIndexer.java

/**
 *
 * @param file/* w  ww. jav  a2 s .c om*/
 * @return
 */
public static Map<String, JsonNode> loadIndicesFromFile(String file) {
    // Load indices.json to configuration
    Map<String, JsonNode> indices = new HashMap();

    ObjectMapper mapper = Indexer.getObjectMapper();
    JsonNode json;
    try {
        json = mapper.readTree(Files.readAllBytes(Paths.get(file)));
    } catch (IOException ex) {
        throw new IndexerException(ex);
    }

    Iterator<String> it = json.fieldNames();
    while (it.hasNext()) {
        String indexName = it.next();
        indices.put(indexName, json.get(indexName));
    }

    return indices;
}

From source file:com.hpe.caf.worker.testing.ContentResultValidationProcessor.java

@Override
protected boolean processWorkerResult(TestItem<TInput, TExpected> testItem, TaskMessage message,
        TResult workerResult) throws Exception {

    DataSource dataSource = new DataStoreSource(dataStore, getCodec());

    ReferencedData referencedData = getContentFunc.apply(workerResult);

    String contentFileName = testItem.getExpectedOutputData().getExpectedContentFile();
    if (contentFileName != null && contentFileName.length() > 0) {

        InputStream dataStream = referencedData.acquire(dataSource);
        String ocrText = IOUtils.toString(dataStream, StandardCharsets.UTF_8);
        Path contentFile = Paths.get(contentFileName);
        if (Files.notExists(contentFile)) {
            contentFile = Paths.get(testDataFolder, contentFileName);
        }/*from   www  . j a  v a2  s . c om*/
        String expectedOcrText = new String(Files.readAllBytes(contentFile));

        double similarity = ContentComparer.calculateSimilarityPercentage(expectedOcrText, ocrText);

        System.out.println("Test item: " + testItem.getTag() + ". Similarity: " + similarity + "%");
        if (similarity < testItem.getExpectedOutputData().getExpectedSimilarityPercentage()) {
            TestResultHelper.testFailed(testItem,
                    "Expected similarity of "
                            + testItem.getExpectedOutputData().getExpectedSimilarityPercentage()
                            + "% but actual similarity was " + similarity + "%");
        }
    } else {

        if (referencedData != null) {
            TestResultHelper.testFailed(testItem, "Expected null result.");
        }
    }
    return true;
}

From source file:eu.freme.bpt.service.EPublishing.java

@Override
public void run(FailurePolicy failurePolicy, int nrThreads, Callback callback) {
    logger.info("Running service EPublishing");
    ExecutorService executorService = Executors.newFixedThreadPool(nrThreads);
    Unirest.setTimeouts(30000, 300000); // TODO: configurable?

    // iterate over zip files
    File[] zipFiles = inputDirectory.listFiles((dir, name) -> {
        return name.endsWith(".zip");
    });/* w  w  w  .  j  a  v  a2 s  .  c  o m*/

    for (final File zipFile : zipFiles) {
        executorService.submit(() -> {
            File jsonFile = new File(zipFile.getParentFile(), zipFile.getName().replace(".zip", ".json"));
            if (jsonFile.exists()) {
                File outputFile = new File(outputDirectory, zipFile.getName().replace(".zip", ".epub"));
                try {
                    String json = new String(Files.readAllBytes(jsonFile.toPath()), StandardCharsets.UTF_8);
                    HttpResponse<InputStream> response = Unirest.post(endpoint).field("htmlZip", zipFile)
                            .field("metadata", json).asBinary();
                    if (response.getStatus() == 200) {
                        logger.debug("Request alright.");
                        try (InputStream responseInput = response.getBody();
                                OutputStream out = new FileOutputStream(outputFile)) {
                            IOUtils.copy(responseInput, out);
                            callback.onTaskComplete(zipFile, outputFile);
                        }
                        //Files.write(outputFile.toPath(), IOUtils.toByteArray())
                    } else {
                        String body = IOUtils.toString(response.getBody());
                        String msg = "Error response from service " + endpoint + ": Status "
                                + response.getStatus() + ": " + response.getStatusText() + " - " + body;
                        logger.error(msg);
                        callback.onTaskFails(zipFile, outputFile, msg);
                        if (!failurePolicy.check()) {
                            System.exit(3);
                        }
                    }
                } catch (IOException e) {
                    logger.error("Error while reading json file: {}", jsonFile, e);
                    callback.onTaskFails(zipFile, outputFile,
                            "Error while reading json file: " + jsonFile + " " + e.getMessage());
                    if (!failurePolicy.check()) {
                        System.exit(3);
                    }
                } catch (UnirestException e) {
                    logger.error("Request to {} failed." + endpoint, e);
                    callback.onTaskFails(zipFile, outputFile,
                            "Request to " + endpoint + " failed. " + e.getMessage());
                    if (!failurePolicy.check()) {
                        System.exit(3);
                    }
                }

            } else {
                String msg = "Missing metatada file " + jsonFile + " for input file " + zipFile;
                logger.error(msg);
                callback.onTaskFails(zipFile, null, msg);
                if (!failurePolicy.check()) {
                    System.exit(3);
                }
            }
        });
    }
}

From source file:org.trustedanalytics.h2oscoringengine.publisher.http.FilesDownloaderTest.java

@Test
public void download_serverResponse200_resourcesDownloaded() throws Exception {
    // given//from   www  . j av  a  2  s .  com
    FilesDownloader downloader = new FilesDownloader(testCredentials, restTemplateMock);

    // when
    when(responseMock.getBody()).thenReturn(testServerResponse.getBytes());
    downloader.download(testResource, testPath);

    // then
    verify(restTemplateMock).exchange(testCredentials.getHost() + testResource, HttpMethod.GET,
            HttpCommunication.basicAuthRequest(testCredentials.getBasicAuthToken()), byte[].class);
    assertThat(new String(Files.readAllBytes(testPath)), equalTo(testServerResponse));
}

From source file:com.destroystokyo.paperclipmavenplugin.GenerateDataMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    final File patch = new File(generatedResourceLocation, "paperMC.patch");
    final File json = new File(generatedResourceLocation, "patch.json");

    // Create the directory if needed
    if (!generatedResourceLocation.exists()) {
        try {//from ww w . j a va  2 s  . c o  m
            FileUtils.forceMkdir(generatedResourceLocation);
            try {
                FileUtils.forceDelete(patch);
            } catch (FileNotFoundException ignored) {
            }
        } catch (IOException e) {
            throw new MojoExecutionException("Could not create source directory", e);
        }
    }

    if (!vanillaMinecraft.exists()) {
        throw new MojoExecutionException("vanillaMinecraft jar does not exist!");
    }

    if (!paperMinecraft.exists()) {
        throw new MojoExecutionException("paperMinecraft jar does not exist!");
    }

    // Read the files into memory
    getLog().info("Reading jars into memory");
    final byte[] vanillaMinecraftBytes;
    final byte[] paperMinecraftBytes;
    try {
        vanillaMinecraftBytes = Files.readAllBytes(vanillaMinecraft.toPath());
        paperMinecraftBytes = Files.readAllBytes(paperMinecraft.toPath());
    } catch (IOException e) {
        throw new MojoExecutionException("Error reading jars", e);
    }

    getLog().info("Creating patch");
    try (final FileOutputStream paperMinecraftPatch = new FileOutputStream(patch)) {
        Diff.diff(vanillaMinecraftBytes, paperMinecraftBytes, paperMinecraftPatch);
    } catch (InvalidHeaderException | IOException | CompressorException e) {
        throw new MojoExecutionException("Error creating patches", e);
    }

    // Add the SHA-256 hashes for the files
    final MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new MojoExecutionException("Could not create SHA-256 hasher.", e);
    }

    getLog().info("Hashing files");
    final byte[] vanillaMinecraftHash = digest.digest(vanillaMinecraftBytes);
    final byte[] paperMinecraftHash = digest.digest(paperMinecraftBytes);

    final PatchData data = new PatchData();
    data.setOriginalHash(toHex(vanillaMinecraftHash));
    data.setPatchedHash(toHex(paperMinecraftHash));
    data.setPatch("paperMC.patch");
    data.setSourceUrl("https://s3.amazonaws.com/Minecraft.Download/versions/" + mcVersion + "/minecraft_server."
            + mcVersion + ".jar");

    data.setVersion(mcVersion);

    getLog().info("Writing json file");
    Gson gson = new Gson();
    String jsonString = gson.toJson(data);

    try (final FileOutputStream fs = new FileOutputStream(json);
            final OutputStreamWriter writer = new OutputStreamWriter(fs)) {
        writer.write(jsonString);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.romeikat.datamessie.core.base.util.FileUtil.java

public String readFile(final String file, final Charset charset) {
    try {/*from w  ww .  jav a 2s. c o  m*/
        final Path path = Paths.get(file);
        final byte[] bytes = Files.readAllBytes(path);
        final String contents = new String(bytes, charset);
        return contents;
    } catch (final Exception e) {
        LOG.error("Could not read file", e);
        return null;
    }
}

From source file:org.sakuli.services.forwarder.ScreenshotDivConverter.java

protected String extractScreenshotAsBase64(Throwable exception) {
    if (exception instanceof SakuliExceptionWithScreenshot) {
        Path screenshotPath = ((SakuliExceptionWithScreenshot) exception).getScreenshot();
        if (screenshotPath != null) {
            try {
                byte[] binaryScreenshot = Files.readAllBytes(screenshotPath);
                String base64String = new BASE64Encoder().encode(binaryScreenshot);
                for (String newLine : Arrays.asList("\n", "\r")) {
                    base64String = StringUtils.remove(base64String, newLine);
                }/*from  w  w w.j  av a  2  s .  co m*/
                return base64String;
            } catch (IOException e) {
                exceptionHandler.handleException(new SakuliForwarderException(e, String.format(
                        "error during the BASE64 encoding of the screenshot '%s'", screenshotPath.toString())));
            }
        }
    }
    return null;
}

From source file:grakn.core.daemon.executor.Executor.java

public String retrievePid(Path pidFile) {
    if (!pidFile.toFile().exists()) {
        return null;
    }/*from ww  w .ja  v a2s.c  om*/
    try {
        String pid = new String(Files.readAllBytes(pidFile), StandardCharsets.UTF_8);
        return pid.trim();
    } catch (NumberFormatException | IOException e) {
        return null;
    }
}

From source file:com.wrmsr.wava.TestOutlining.java

@Test
public void testOutlining() throws Throwable {
    Path outputFile = Paths.get("tmp/post.json");
    Function function = Json.OBJECT_MAPPER_SUPPLIER.get().readValue(Files.readAllBytes(outputFile),
            Function.class);
    Node body = function.getBody();

    LocalAnalysis loa = LocalAnalysis.analyze(body);
    ControlTransferAnalysis cfa = ControlTransferAnalysis.analyze(body);
    ValueTypeAnalysis vta = ValueTypeAnalysis.analyze(body, false);

    Map<Node, Optional<Node>> parentsByNode = Analyses.getParents(body);
    Map<Node, Integer> totalChildrenByNode = Analyses.getChildCounts(body);
    Map<Name, Node> nodesByName = Analyses.getNamedNodes(body);

    Node maxNode = body;/*from  w  w w . j  a v a2s. c o  m*/
    int maxDiff = 0;

    Node cur = body;
    while (true) {
        System.out.println(
                String.format("%s -> %d (%d)", cur, totalChildrenByNode.get(cur), cur.getChildren().size()));
        Optional<Node> maxChild = cur.getChildren().stream()
                .max((l, r) -> Integer.compare(totalChildrenByNode.get(l), totalChildrenByNode.get(r)));
        if (!maxChild.isPresent()) {
            break;
        }
        int diff = totalChildrenByNode.get(cur) - totalChildrenByNode.get(maxChild.get());
        if (diff > maxDiff) {
            maxNode = cur;
            maxDiff = diff;
        }
        cur = maxChild.get();
    }

    System.out.println();
    System.out.println(maxNode);

    System.out.println();
    List<Node> alsdfj = new ArrayList<>(maxNode.getChildren());
    Collections.sort(alsdfj,
            (l, r) -> -Integer.compare(totalChildrenByNode.get(l), totalChildrenByNode.get(r)));
    for (Node child : alsdfj) {
        System.out.println(String.format("%s -> %d", child, totalChildrenByNode.get(child)));
    }
    System.out.println();

    Index externalRetControl = Index.of(function.getLocals().getList().size());
    Index externalRetValue = Index.of(function.getLocals().getList().size() + 1);
    List<Local> localList = ImmutableList.<Local>builder().addAll(function.getLocals().getList())
            .add(new Local(Name.of("_external$control"), externalRetControl, Type.I32))
            .add(new Local(Name.of("_external$value"), externalRetValue, Type.I64)).build();

    maxNode.accept(new Visitor<Void, Void>() {
        @Override
        protected Void visitNode(Node node, Void context) {
            Outlining.OutlinedFunction of = Outlining.outlineFunction(function, node, Name.of("outlined"),
                    externalRetControl, externalRetValue, loa, cfa, vta, parentsByNode, nodesByName);

            try {
                compileFunction(of.getFunction());
            } catch (Throwable e) {
                throw Throwables.propagate(e);
            }

            return null;
        }

        @Override
        public Void visitSwitch(Switch node, Void context) {
            Optional<Switch.Entry> maxEntry = node.getEntries().stream().max((l, r) -> Integer
                    .compare(totalChildrenByNode.get(l.getBody()), totalChildrenByNode.get(r.getBody())));
            Node maxNode = maxEntry.get().getBody();

            Outlining.OutlinedFunction of = Outlining.outlineFunction(function, maxNode, Name.of("outlined"),
                    externalRetControl, externalRetValue, loa, cfa, vta, parentsByNode, nodesByName);

            try {
                compileFunction(of.getFunction());
            } catch (Throwable e) {
                throw Throwables.propagate(e);
            }

            Function newFunc = new Function(function.getName(), function.getResult(), function.getArgCount(),
                    new Locals(localList),
                    Transforms.replaceNode(function.getBody(), maxNode, of.getCallsite(), true));

            System.out.println();
            try {
                compileFunction(newFunc);
            } catch (Throwable e) {
                throw Throwables.propagate(e);
            }

            //                Map<Index, Index> localTranslation

            //                new Function(
            //                        Name.of("laksdjflkad"),
            //                        Type.I32,
            //
            //                        )

            /*
            TODO:
             - FALLTHROUGH analysis
             - local index translation
             - spills in/out
             - breaks as return codes (!! with break values), and returns as return codes
              - I64 retval cell
                    
            NEXT:
             - vta for non switch-cases, with inline value returns if no breaks
             - pre-alloc cells? would need to kid-glove return temp loading
             - sp based retvals (setjmp/exceptions gon fuck my day up?)
              - oh fuck. shadowstack? :/
               - NO, no this is doable. pushed ONLY immediately before ret, popped ALWAYS immediately after ret, stack remains same during execution
            */

            //                TempManager tm = new TempManager(
            //                        new NameGenerator(
            //                                function.getLocals().getLocals().stream().map(Local::getName).collect(toImmutableSet()),
            //                                "_temp$"),
            //                        Index.of(function.getLocals().getLocals().size()),
            //                        false);
            //
            //                Locals locals = new Locals(Stream.concat(function.getLocals().getLocals().stream(), tm.getTempList().stream().map(t -> new Local(t.getName(), t.getIndex(), t.getType()))).collect(toImmutableList()));
            //                function = new Function(
            //                        NameMangler.DEFAULT.mangleName(function.getName()),
            //                        function.getResult(),
            //                        function.getArgCount(),
            //                        locals,
            //                        body);

            return null;
        }
    }, null);
}

From source file:com.simiacryptus.util.io.IOUtil.java

/**
 * Read kryo t./*from  w w w .  j  a  va  2 s  .c o m*/
 *
 * @param <T>  the type parameter
 * @param file the file
 * @return the t
 */
public static <T> T readKryo(File file) {
    try {
        byte[] bytes = CompressionUtil.decodeBZRaw(Files.readAllBytes(file.toPath()));
        Input input = new Input(buffer.get(), 0, bytes.length);
        return (T) new KryoReflectionFactorySupport().readClassAndObject(input);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}