Example usage for com.google.common.io Resources toByteArray

List of usage examples for com.google.common.io Resources toByteArray

Introduction

In this page you can find the example usage for com.google.common.io Resources toByteArray.

Prototype

public static byte[] toByteArray(URL url) throws IOException 

Source Link

Document

Reads all bytes from a URL into a byte array.

Usage

From source file:org.zalando.stups.swagger.codegen.YamlToJson.java

protected String getYamlFileContentAsJson() throws IOException {
    String data = "";
    if (yamlInputPath.startsWith("http") || yamlInputPath.startsWith("https")) {

        data = new String(Resources.toByteArray(new URL(yamlInputPath)));
    } else {/*from   w  w w. j a  va 2s .c om*/
        data = new String(Files.readAllBytes(java.nio.file.Paths.get(new File(yamlInputPath).toURI())));
    }

    ObjectMapper yamlMapper = Yaml.mapper();
    JsonNode rootNode = yamlMapper.readTree(data);

    // must have swagger node set
    JsonNode swaggerNode = rootNode.get("swagger");

    return rootNode.toString();
}

From source file:org.eclipse.xtext.xtext.wizard.cli.CliProjectsCreator.java

public void createProject(final ProjectDescriptor project) {
    String _location = project.getLocation();
    final File projectRoot = new File(_location);
    projectRoot.mkdirs();/* w ww . j ava  2s  .  co  m*/
    final Consumer<AbstractFile> _function = (AbstractFile it) -> {
        try {
            String _pathFor = project.getConfig().getSourceLayout().getPathFor(it.getOutlet());
            String _plus = (_pathFor + "/");
            String _relativePath = it.getRelativePath();
            final String projectRelativePath = (_plus + _relativePath);
            final File file = new File(projectRoot, projectRelativePath);
            file.getParentFile().mkdirs();
            boolean _matched = false;
            if (it instanceof TextFile) {
                _matched = true;
                final String normalizedContent = ((TextFile) it).getContent().replace(Strings.newLine(),
                        this.lineDelimiter);
                Files.write(normalizedContent, file, project.getConfig().getEncoding());
            }
            if (!_matched) {
                if (it instanceof BinaryFile) {
                    _matched = true;
                    Files.write(Resources.toByteArray(((BinaryFile) it).getContent()), file);
                }
            }
            boolean _isExecutable = it.isExecutable();
            if (_isExecutable) {
                file.setExecutable(true);
            }
        } catch (Throwable _e) {
            throw Exceptions.sneakyThrow(_e);
        }
    };
    project.getFiles().forEach(_function);
    final Consumer<String> _function_1 = (String it) -> {
        new File(projectRoot, it).mkdirs();
    };
    project.getSourceFolders().forEach(_function_1);
}

From source file:org.graylog2.fongo.SeedingFongoRule.java

public void insertSeed(String seed) throws IOException {
    final byte[] bytes = Resources.toByteArray(Resources.getResource(seed));
    final Map<String, Object> map = objectMapper.readValue(bytes, MAP_TYPE);

    for (String collectionName : map.keySet()) {
        @SuppressWarnings("unchecked")
        final List<Map<String, Object>> documents = (List<Map<String, Object>>) map.get(collectionName);
        final MongoCollection<Document> indexSets = getDatabase().getCollection(collectionName);

        for (Map<String, Object> document : documents) {
            final Document parsedDocument = Document.parse(objectMapper.writeValueAsString(document));
            LOG.debug("Inserting parsed document: \n{}", parsedDocument.toJson(new JsonWriterSettings(true)));
            indexSets.insertOne(parsedDocument);
        }//  www . j a v a  2 s. c  o  m
    }
}

From source file:org.isisaddons.app.kitchensink.fixture.blobclob.BlobClobObjectsFixture.java

private Blob newBlob(final String name, final String mimeType, final String resourceName) throws IOException {
    final byte[] pdfBytes = Resources.toByteArray(Resources.getResource(getClass(), resourceName));
    return new Blob(name + "-" + resourceName, mimeType, pdfBytes);
}

From source file:com.geico.tfs.matcher.TfsMatcherRunner.java

private File getExecutable() {
    File tempFile = new File("");
    try {/*from www. ja  va2 s  .  c  o m*/
        tempFile = File.createTempFile(Long.toString(System.currentTimeMillis()), "TfsMatcher.exe");
        tempFile.deleteOnExit();
        URL tfsMatcherURL = TfsMatcherRunner.class.getResource("/TfsMatcher.exe");
        Files.write(Resources.toByteArray(tfsMatcherURL), tempFile);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    return tempFile;
}

From source file:com.linecorp.armeria.server.docs.DocStringExtractor.java

private Map<String, String> getAllDocStrings0(ClassLoader classLoader) {
    final Configuration configuration = new ConfigurationBuilder()
            .filterInputsBy(new FilterBuilder().includePackage(path)).setUrls(ClasspathHelper.forPackage(path))
            .addClassLoader(classLoader).addScanners(new ResourcesScanner());
    if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) {
        return Collections.emptyMap();
    }//from   w  ww  .j a v a  2s  .co m
    Map<String, byte[]> files = new Reflections(configuration).getResources(this::acceptFile).stream()
            .map(f -> {
                try {
                    URL url = classLoader.getResource(f);
                    if (url == null) {
                        throw new IllegalStateException("not found: " + f);
                    }
                    return new SimpleImmutableEntry<>(f, Resources.toByteArray(url));
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }).collect(toImmutableMap(Entry::getKey, Entry::getValue));
    return getDocStringsFromFiles(files);
}

From source file:org.glowroot.agent.weaving.PluginDetailBuilder.java

static byte[] getBytes(String className, @Nullable File pluginJar) throws IOException {
    String resourceName = "/" + className + ".class";
    URL url = PluginDetailBuilder.class.getResource(resourceName);
    if (url != null) {
        return Resources.toByteArray(url);
    }//  w w  w . j  a  va2 s. c  om
    if (pluginJar != null) {
        url = new URL("jar:" + pluginJar.toURI() + "!" + resourceName);
        return Resources.toByteArray(url);
    }
    throw new IOException("Class not found: " + className);
}

From source file:test.APIexample.java

private static void toDisk(Song song) throws MalformedURLException, IOException, URISyntaxException {
    File file = new File(new File(".") + System.getProperty("path.separator") + song.getId() + ".dl");
    Files.write(Resources.toByteArray(song.getAlbumArtUrlAsURI().toURL()), file);
}

From source file:org.isisaddons.module.excel.dom.ExcelFixture2.java

private byte[] readBytes() {

    final URL excelResource = getExcelResource();
    try {//from   www  .  j  a v a2 s .c o m
        bytes = Resources.toByteArray(excelResource);
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not read from resource: " + excelResource);
    }
    return bytes;
}

From source file:org.apache.drill.exec.store.ClassPathFileSystem.java

@Override
public FSDataInputStream open(Path arg0, int arg1) throws IOException {
    String file = getFileName(arg0);
    URL url = Resources.getResource(file);
    if (url == null) {
        throw new IOException(String.format("Unable to find path %s.", arg0.getName()));
    }/*from   w ww  .  j  a v a 2  s  .  c o m*/
    ResourceInputStream ris = new ResourceInputStream(Resources.toByteArray(url));
    return new FSDataInputStream(ris);
}