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.ericsson.eiffel.remrem.generate.cli.CLI.java

/**
 * Read file content as String/* w w  w.  j  a  va  2  s  .  co  m*/
 * 
 * @param filePath
 * @return file contents if successful 
 */
private String readFileContent(String filePath) {
    try {
        byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
        return new String(fileBytes);
    } catch (IOException e) {
        System.out.println("Unable to read File content from file path " + filePath);
        CLIOptions.exit(CLIExitCodes.CLI_READ_FILE_FAILED);
    }
    return null;
}

From source file:org.wso2.appserver.integration.tests.webapp.virtualhost.WSAS2058AppbasePathRepeatedVirtualHostTestCase.java

@BeforeClass(alwaysRun = true, enabled = false)
public void setEnvironment() throws Exception {
    super.init(userMode);
    serverConfigurationManager = new ServerConfigurationManager(
            new AutomationContext("AS", TestUserMode.SUPER_TENANT_ADMIN));
    webAppURL = getWebAppURL(WebAppTypes.WEBAPPS);

    //Restart the Server only once
    if (isRestarted == 0) {
        File sourceFile = Paths.get(TestConfigurationProvider.getResourceLocation(), "artifacts", "AS",
                "tomcat", "catalina-server-repeated-appbase-name.xml").toFile();

        Path targetFilePath = Paths.get(FrameworkPathUtil.getCarbonServerConfLocation(), "tomcat",
                "catalina-server.xml");
        serverConfigurationManager.applyConfigurationWithoutRestart(sourceFile, targetFilePath.toFile(), true);

        //Get first path and replace it in the server without restarting
        virtualHostAppBase = Paths.get(FrameworkPathUtil.getCarbonHome()).getParent().getFileName().toString();
        Charset charset = StandardCharsets.UTF_8;
        String content = new String(Files.readAllBytes(targetFilePath), charset);
        content = content.replaceAll("virtual_appbase_name", virtualHostAppBase);
        Files.write(targetFilePath, content.getBytes(charset));

        serverConfigurationManager.restartGracefully();
        sessionCookie = loginLogoutClient.login();
    }//from  ww  w .ja va  2  s.c  o m

    ++isRestarted;
}

From source file:com.cpjit.swagger4j.support.internal.DefaultApiViewWriter.java

@Deprecated
@Override/*from  w  w w.  j a  v  a2  s  .co m*/
public void writeApis(HttpServletRequest request, HttpServletResponse response, Properties props)
        throws Exception {
    APIParseable restParser = APIParser.newInstance(props);
    response.setContentType("application/json;charset=utf-8");
    String devMode = props.getProperty("devMode");
    if (Boolean.valueOf(devMode)) {
        Object apis = restParser.parseAndNotStore();
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.writeObject(apis);
        writer.flush();
        writer.close();
    } else {
        if (!scanfed) {
            restParser.parse();
            scanfed = true;
        }
        byte[] bs = Files.readAllBytes(Paths.get(props.getProperty("apiFile")));
        OutputStream out = response.getOutputStream();
        out.write(bs);
        out.flush();
        out.close();
    }
}

From source file:com.smartsheet.tin.filters.pkpublish.PKPublishFilterRules.java

/**
 * Read the specified file into a String.
 * // w  ww.  java2  s  . com
 * @param path
 * @return
 * @throws IOException
 */
private String readFile(String path) throws IOException {
    Path p = FileSystems.getDefault().getPath(path);
    String contents = new String(Files.readAllBytes(p));
    return contents;
}

From source file:ccm.pay2spawn.configurator.HTMLGenerator.java

public static String readFile(File file) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(file.toURI()));
    return Charset.defaultCharset().decode(ByteBuffer.wrap(encoded)).toString();
}

From source file:gr.cslab.Metric_test.java

static public JSONArray file2Json(String filename) {
    JSONArray ja = null;//from w w  w  .ja  v a 2  s. c om
    try {
        //read all bytes of a file (NIO)
        byte[] encoded = Files.readAllBytes(Paths.get(filename));
        //decode all bytes to  the default charset and return
        String in = Charset.defaultCharset().decode(ByteBuffer.wrap(encoded)).toString();
        ja = new JSONArray(in);
    } catch (IOException ex) {
        System.err.println("ERROR: could not read file: " + filename);
    } catch (JSONException ex) {
        System.err.println("ERROR: malformed json input for file:" + filename);
    }

    return ja;

}

From source file:com.nartex.RichFileManager.java

protected void readSmallFile(HttpServletResponse resp, Path file) {
    OutputStream os = null;//from w  w  w  .ja va  2s  . co m
    try {
        os = resp.getOutputStream();
        os.write(Files.readAllBytes(file));
    } catch (Exception e) {
        this.error(sprintf(lang("INVALID_DIRECTORY_OR_FILE"), file.toFile().getName()));
    } finally {
        try {
            if (os != null)
                os.close();
        } catch (Exception e2) {
        }
    }
}

From source file:com.juancarlosroot.threads.SimulatedUser.java

private void consultarFirma(int fileNameId) {
    try {/*ww  w  .j  a  v a2s.c  o  m*/
        FileOutputStream fos = new FileOutputStream(
                userFolderPath + "/" + Integer.toString(fileNameId) + "Firmada.zip");
        ZipOutputStream zos = new ZipOutputStream(fos);

        String file1Name = userFolderPath + "/" + Integer.toString(fileNameId) + "Firmada.png";
        File image = new File(file1Name);

        ZipEntry zipEntry = new ZipEntry(image.getName());
        zos.putNextEntry(zipEntry);
        FileInputStream fileInputStream = new FileInputStream(image);

        byte[] buf = new byte[2048];
        int bytesRead;

        while ((bytesRead = fileInputStream.read(buf)) > 0) {
            zos.write(buf, 0, bytesRead);
        }
        zos.closeEntry();
        zos.close();
        fos.close();

        Path path = Paths.get(userFolderPath + "/" + Integer.toString(fileNameId) + "Firmada.zip");

        byte[] data = Files.readAllBytes(path);
        byte[] byteArray = Base64.encodeBase64(data);
        String b64 = new String(byteArray);

        System.out.println(consultaFirma(b64, Integer.toString(fileNameId) + "Firmada"));

        nFilesVerified++;
        System.out.println("User : " + idSimulatedUser + " FIRMA VERIFICADA");
    } catch (IOException ex) {
        Logger.getLogger(SimulatedUser.class.getName()).log(Level.SEVERE, null, ex);
        nNotFileFound++;
    } catch (IOException_Exception ex) {
        Logger.getLogger(SimulatedUser.class.getName()).log(Level.SEVERE, null, ex);
        nVerifyErrors++;
    }
}

From source file:ddf.security.pdp.realm.xacml.processor.PollingPolicyFinderModule.java

public void onFileCreate(File createdFile) {
    try {/* ww w .ja v  a 2s.  c  om*/
        SecurityLogger.audit("File {} was created with content:\n{}", createdFile.getCanonicalPath(),
                new String(Files.readAllBytes(Paths.get(createdFile.getCanonicalPath())),
                        StandardCharsets.UTF_8));
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    reloadPolicies();
}

From source file:jvmoptions.OptionAnalyzer.java

static boolean contains(Path path) {
    try {//from   www .  j a v a  2  s .c  o  m
        byte[] bytes = Files.readAllBytes(path);
        String s = new String(bytes);
        return s.contains("_FLAGS(develop");
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}