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.gkh.racf.JSONUtil.java

public static JSONObject getJSONFromFile(String path) throws IOException, JSONException {
    // (1) Read JSON file into a string and attempt to parse it.
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    String json = new String(encoded, "UTF-8");

    if (isValidJSON(json)) {
        return new JSONObject(json);
    }//from   w  w w  .  ja v  a2s . c  o m

    return null;
}

From source file:it.polimi.diceH2020.launcher.service.Validator.java

public <T> Optional<T> objectFromPath(Path pathFile, Class<T> cls) {

    String serialized;/*from  w w w. j  a v  a2s . c  o m*/
    try {
        serialized = new String(Files.readAllBytes(pathFile));
        T data = mapper.readValue(serialized, cls);
        return Optional.of(data);
    } catch (IOException e) {
        return Optional.empty();
    }
}

From source file:org.gyt.schema.json.Utils.java

public static String readFile(String path, Charset encoding) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return new String(encoded, encoding);
}

From source file:fr.logfiletoes.Config.java

/**
 * Construct config from json file path/* w  ww  .j av  a2s  .  c  o m*/
 * @param filepath json config file path
 * @throws IOException
 */
public Config(String filepath) throws IOException {
    String json = new String(Files.readAllBytes(Paths.get(filepath)));
    JSONTokener jsont = new JSONTokener(json);
    JSONObject config = (JSONObject) jsont.nextValue();
    JSONArray unitsJSON = config.getJSONArray("unit");
    LOG.fine(unitsJSON.length() + " unit(s)");
    for (int i = 0; i < unitsJSON.length(); i++) {
        JSONObject unitFormJson = unitsJSON.getJSONObject(i);
        Unit unit = createUnit(unitFormJson);
        if (unit != null) {
            this.units.add(unit);
        } else {
            LOG.warning("unit skip : " + unitFormJson.toString());
        }
    }
}

From source file:dk.lnj.swagger4ee.AbstractJSonTest.java

public void makeCompare(SWRoot root, String expFile) throws IOException {

    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    // make deserializer use JAXB annotations (only)
    mapper.setAnnotationIntrospector(introspector);
    // make serializer use JAXB annotations (only)

    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, root);/*from   w  w  w  .  j a va2 s  .  com*/
    mapper.writeValue(new FileWriter(new File("lasttest.json")), root);

    String actual = sw.toString();
    String expected = new String(Files.readAllBytes(Paths.get(expFile)));

    String[] exp_split = expected.split("\n");
    String[] act_split = actual.split("\n");

    for (int i = 0; i < exp_split.length; i++) {
        String exp = exp_split[i];
        String act = act_split[i];
        String shortFileName = expFile.substring(expFile.lastIndexOf("/"));
        Assert.assertEquals(shortFileName + ":" + (i + 1), superTrim(exp), superTrim(act));
    }

}

From source file:org.createnet.search.raptor.search.query.impl.ObjectQueryTest.java

@Before
public void setUp() throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    Path path = Paths.get(classLoader.getResource("object-query.json").getPath());
    content = new String(Files.readAllBytes(path));
}

From source file:com.bmwcarit.barefoot.matcher.MatcherExample.java

public List<MatcherSample> readSamples() throws IOException, JSONException {
    String json = new String(
            Files.readAllBytes(Paths.get(ServerTest.class.getResource("x0001-001.json").getPath())),
            Charset.defaultCharset());

    JSONArray jsonsamples = new JSONArray(json);
    List<MatcherSample> samples = new LinkedList<>();

    for (int i = 0; i < jsonsamples.length(); ++i) {
        samples.add(new MatcherSample(jsonsamples.getJSONObject(i)));
    }/*from ww w  .j  a v  a  2  s .c o  m*/

    return samples;
}

From source file:com.shadwelldacunha.byteswipe.core.Utilities.java

public static String readFileAsString(String path, Charset encoding) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return new String(encoded, encoding);
}

From source file:eu.domibus.ebms3.sender.TLSReader.java

public TLSClientParameters getTlsClientParameters() {
    byte[] encoded = new byte[0];
    String config = "";
    try {/*from  ww  w  . j  a  v  a 2 s .  c o m*/
        encoded = Files.readAllBytes(
                Paths.get(System.getProperty("domibus.config.location"), CLIENTAUTHENTICATION_XML));
        config = new String(encoded, "UTF-8");
        config = config.replaceAll("\\Q${domibus.config.location}\\E",
                System.getProperty("domibus.config.location").replace('\\', '/'));

        return (TLSClientParameters) TLSClientParametersConfig.createTLSClientParameters(config);

    } catch (FileNotFoundException e) {
        LOG.warn("No tls configuration file " + System.getProperty("domibus.config.location")
                + CLIENTAUTHENTICATION_XML + " found. Mutual authentication will not be supported.");
        return null;
    } catch (IOException | RuntimeException e) {
        LOG.error("Mutual authentication will not be supported.", e);
        return null;
    }
}

From source file:com.wrmsr.kleist.file.FileReader.java

@Override
public Index readIndex() throws IOException {
    return requireNonNull(objectMapper
            .readValue(Files.readAllBytes(Paths.get(rootPath.toString(), "index.json")), Index.class));
}