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:eu.seaclouds.platform.planner.optimizerTest.service.OptimizerServiceOpenShiftTest.java

private static String filenameToString(String path) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return new String(encoded, StandardCharsets.UTF_8);
}

From source file:duthientan.mmanm.com.CipherRSA.java

@Override
public void encrypt(String filePath) {
    try {/*from ww  w  .j  av  a2 s .c  o m*/
        Cipher ecipher = Cipher.getInstance("RSA");
        ecipher.init(Cipher.ENCRYPT_MODE, publicKey);
        Path path = Paths.get(filePath);
        String encryptFilePath = path.getParent().toString() + "/" + "RSA" + "_"
                + path.getFileName().toString();
        byte[] data = Files.readAllBytes(path);
        byte[] textEncrypted = null;
        int chunkSize = 245;
        if (data.length < 245) {
            textEncrypted = ecipher.doFinal(data);
        } else {
            for (int i = 0; i < data.length; i += chunkSize) {
                byte[] segment = Arrays.copyOfRange(data, i,
                        i + chunkSize > data.length ? data.length : i + chunkSize);
                byte[] segmentEncrypted = ecipher.doFinal(segment);
                textEncrypted = ArrayUtils.addAll(textEncrypted, segmentEncrypted);
            }
        }
        FileOutputStream fos = new FileOutputStream(encryptFilePath);
        fos.write(textEncrypted);
        fos.close();
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(CipherRSA.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.sastix.cms.server.services.cache.CacheFileUtilsServiceTest.java

@Test
public void downloadOnlineResourceTest() throws IOException, URISyntaxException {
    //from online resource
    URL localFile = getClass().getClassLoader().getResource("./logo.png");
    byte[] expected = Files.readAllBytes(Paths.get(localFile.getFile()));
    byte[] bytesFound = cacheFileUtilsService.downloadResource(
            new URL("https://raw.githubusercontent.com/sastix/cms/master/server/src/test/resources/logo.png"));
    Assert.assertArrayEquals(expected, bytesFound);
}

From source file:jBittorrentAPI.utils.IOManager.java

/**
 * Read all available bytes from the given file
 * @param path String/*from  w w  w  .j  ava  2s .c  om*/
 * @param encoding Charset
 * @return String
 */
public static String readStringFromFile(String path, Charset encoding) {
    String result = null;
    try {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        result = new String(encoded, encoding);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:FileSystemProviderTest.java

@Test
public void testCreate() throws IOException {
    Path p = fileSystemProvider.getFileSystem().getPath("/testfile");
    Files.createFile(p);/*ww  w .j  a  va  2 s  .c om*/
    Assert.assertTrue(Files.exists(p));
    String hello = "Hello world";
    Files.write(p, hello.getBytes());

    Assert.assertEquals(hello, new String(Files.readAllBytes(p)));
}

From source file:org.opendaylight.alto.manager.AltoManager.java

protected String readFromFile(String path) throws IOException {
    return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
}

From source file:com.spotify.helios.Utils.java

private static String imageInfo(final String path) {
    final JsonNode node;
    try {//from w ww  . j  a v  a  2 s  .c o m
        final String json = new String(Files.readAllBytes(Paths.get(path)));
        node = Json.readTree(json);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    final JsonNode imageNode = node.get("image");
    return (imageNode == null || imageNode.getNodeType() != STRING) ? null : imageNode.asText();
}

From source file:it.polimi.diceH2020.launcher.FileService.java

public static ArrayList<String[]> getTxT(ArrayList<String> folders) throws IOException {
    ArrayList<String[]> txtList = new ArrayList<>();
    for (String folder : folders) {
        for (String file : listFile(folder, ".txt")) {
            String[] txtInfo = new String[2];
            txtInfo[0] = Paths.get(file).getFileName().toString();
            txtInfo[1] = new String(Files.readAllBytes(Paths.get(folder + File.separator + file)));
            txtList.add(txtInfo);/*from  w w w  .  j  a  v a2s  .  c o m*/
        }
    }

    return txtList;
}

From source file:com.sastix.cms.server.services.cache.CacheServiceTest.java

@Before
public void setUp() throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    Path path = Paths.get(classLoader.getResource("logo.png").getFile());
    bytesToBeCached = Files.readAllBytes(path);
}

From source file:by.creepid.docsreporter.context.DocContextProcessorTest.java

private static byte[] getImage(String path) {
    File fi = new File(path);

    try {//from  w  w  w .ja  va 2  s. co m
        return Files.readAllBytes(fi.toPath());
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    throw new RuntimeException("Cannot set the photo");
}