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.elasticsearch.plugins.PluginManagerIT.java

private void writeMd5(Path file, boolean corrupt) throws IOException {
    String md5Hex = MessageDigests.toHexString(MessageDigests.md5().digest(Files.readAllBytes(file)));
    try (BufferedWriter out = Files.newBufferedWriter(file.resolveSibling(file.getFileName() + ".md5"),
            StandardCharsets.UTF_8)) {
        out.write(md5Hex);/* w w  w  .j a v  a 2  s.c  om*/
        if (corrupt) {
            out.write("bad");
        }
    }
}

From source file:nc.noumea.mairie.appock.services.impl.MailServiceImpl.java

private void gereBonLivraisonDansMail(AppockMail appockMail, MimeMessage msg, String contenuFinal)
        throws MessagingException {
    if (appockMail.getBonLivraison() == null) {
        msg.setContent(contenuFinal, "text/html; charset=utf-8");
    } else {/*w w w . j a v a 2s. co m*/
        BonLivraison bonLivraison = appockMail.getBonLivraison();
        Multipart multipart = new MimeMultipart();
        BodyPart attachmentBodyPart = new MimeBodyPart();

        File file = commandeServiceService.getFileBonLivraison(appockMail.getBonLivraison());

        ByteArrayDataSource bds = null;
        try {
            bds = new ByteArrayDataSource(Files.readAllBytes(file.toPath()),
                    bonLivraison.getMimeType().getLibelle());
        } catch (IOException e) {
            e.printStackTrace();
        }

        attachmentBodyPart.setDataHandler(new DataHandler(bds));
        attachmentBodyPart.setFileName(bonLivraison.getNomFichier());
        multipart.addBodyPart(attachmentBodyPart);

        BodyPart htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent(contenuFinal, "text/html; charset=utf-8");
        multipart.addBodyPart(htmlBodyPart);
        msg.setContent(multipart);
    }
}

From source file:de.appsolve.padelcampus.utils.HtmlResourceUtil.java

private String concatenateCss(ServletContext context, Path path, Path outFile)
        throws FileNotFoundException, IOException {
    DirectoryStream<Path> cssFiles = Files.newDirectoryStream(path, "*.css");
    if (Files.exists(outFile)) {
        Files.delete(outFile);/*w ww  .j a  v  a 2  s  . com*/
    }
    List<Path> sortedFiles = new ArrayList<>();
    for (Path cssFile : cssFiles) {
        sortedFiles.add(cssFile);
    }

    Collections.sort(sortedFiles, new PathByFileNameComparator());

    for (Path cssFile : sortedFiles) {
        Files.write(outFile, Files.readAllBytes(cssFile), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
    }

    byte[] cssData;
    if (Files.exists(outFile)) {
        cssData = Files.readAllBytes(outFile);
    } else {
        //read from classpath
        InputStream is = context.getResourceAsStream(ALL_MIN_CSS);
        cssData = IOUtils.toByteArray(is);
    }
    String css = new String(cssData, Constants.UTF8);
    return css;
}

From source file:com.eternitywall.ots.OtsCli.java

public static void info(String argsOts, boolean verbose) {
    try {//from  w w  w.j a v  a 2 s  .  c  o m
        Path pathOts = Paths.get(argsOts);
        byte[] byteOts = Files.readAllBytes(pathOts);
        DetachedTimestampFile detached = DetachedTimestampFile.deserialize(byteOts);
        String infoResult = OpenTimestamps.info(detached, verbose);
        System.out.println(infoResult);
    } catch (IOException e) {
        log.severe("No valid file");
    }
}

From source file:cloudeventbus.pki.CertificateUtils.java

public static PrivateKey loadPrivateKey(String fileName)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    final Path path = Paths.get(fileName);
    final byte[] encodedPrivateKey = Files.readAllBytes(path);
    final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    return keyFactory.generatePrivate(privateKeySpec);
}

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

@Override
public byte[] getBytesByUID(final String uid, final String tenantID) throws IOException {
    //Get Filename (including directories)
    final String filename = getFilename(uid);

    //Add Volume and tenant ID to the Path
    return Files.readAllBytes(Paths.get(VOLUME + "/" + tenantID + filename));
}

From source file:Engine.Lua.PlayerLua.java

private static String ReadStringFile(String path) {
    String luaFile = null;//  ww w  .j a  v a2 s. c om
    try {
        luaFile = new String(Files.readAllBytes(Paths.get(path)));
    } catch (IOException ex) {
        Logger.getLogger(PlayerLua.class.getName()).log(Level.SEVERE, null, ex);
    }
    return luaFile;
}

From source file:com.ontotext.s4.service.S4ServiceClient.java

/**
 * Classifies the contents of a single file with the specified MIME type. Returns an object which allows
 * for convenient access to the classification information for the document.
 *
 * @param documentContent the file whose contents will be classified
 * @param documentEncoding the encoding of the document file
 * @param documentMimeType the MIME type of the document to classified content as well as the classifications produced
 * @throws IOException//from w w w.j a v  a  2 s  .  c  o  m
 * @throws S4ServiceClientException
 */
public ClassifiedDocument classifyFileContents(File documentContent, Charset documentEncoding,
        SupportedMimeType documentMimeType) throws IOException, S4ServiceClientException {

    Path documentPath = documentContent.toPath();
    if (!Files.isReadable(documentPath)) {
        throw new IOException("File " + documentPath.toString() + " is not readable.");
    }
    ByteBuffer buff;
    buff = ByteBuffer.wrap(Files.readAllBytes(documentPath));
    String content = documentEncoding.decode(buff).toString();

    return classifyDocument(content, documentMimeType);
}

From source file:com.github.anba.es6draft.test262.Test262Info.java

/**
 * Reads the file content.//from   w  ww.j  av  a  2s .  c  o m
 *
 * @return the file content
 * @throws IOException
 *             if there was any I/O error
 */
public String readFileContent() throws IOException {
    return new String(Files.readAllBytes(toFile()), StandardCharsets.UTF_8);
}

From source file:io.syndesis.project.converter.DefaultProjectGenerator.java

private void addAdditionalResources(TarArchiveOutputStream tos) throws IOException {
    for (Templates.Resource additionalResource : generatorProperties.getTemplates().getAdditionalResources()) {
        String overridePath = generatorProperties.getTemplates().getOverridePath();
        URL resource = null;/*from  ww  w  .j  a  v  a 2s.  c  o m*/

        if (!Strings.isEmpty(overridePath)) {
            resource = getClass()
                    .getResource("templates/" + overridePath + "/" + additionalResource.getSource());
        }
        if (resource == null) {
            resource = getClass().getResource("templates/" + additionalResource.getSource());
        }
        if (resource == null) {
            throw new IllegalArgumentException(
                    String.format("Unable to find te required additional resource (overridePath=%s, source=%s)",
                            overridePath, additionalResource.getSource()));
        }

        try {
            addTarEntry(tos, additionalResource.getDestination(),
                    Files.readAllBytes(Paths.get(resource.toURI())));
        } catch (URISyntaxException e) {
            throw new IOException(e);
        }
    }
}