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.spongepowered.api.asset.Asset.java

/**
 * Reads this Asset in it's entirety as a byte array and returns the
 * result.// w w  w  .j a v  a2s . co m
 *
 * @return Byte array representation of Asset
 * @throws IOException
 */
default byte[] readBytes() throws IOException {
    return Resources.toByteArray(getUrl());
}

From source file:org.icgc.dcc.portal.resource.ui.UISoftwareResource.java

@Path("/key")
@GET// w w w  .  j  a  va 2 s .  co m
@SneakyThrows
public Response getKey() {
    val url = Resources.getResource(PUBLIC_KEY_PATH);
    return Response.ok(Resources.toByteArray(url)).type("text/plain")
            .header("Content-Disposition", "attachment; filename=\"" + PUBLIC_KEY_FILE_NAME + "\"").build();
}

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

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    for (Class<?> bridgeClass : bridgeClasses) {
        if (bridgeClass.getName().equals(name)) {
            return bridgeClass;
        }/*from   w w  w  .j a  v a2s .  c o m*/
    }
    byte[] bytes = manualClasses.get(name);
    CodeSource codeSource = null;
    if (bytes == null) {
        String resourceName = ClassNames.toInternalName(name) + ".class";
        URL url = getResource(resourceName);
        if (url == null) {
            throw new ClassNotFoundException(name);
        }
        try {
            bytes = Resources.toByteArray(url);
        } catch (IOException e) {
            throw new ClassNotFoundException("Error loading class", e);
        }
        String path = url.getPath();
        if (url.getProtocol().equals("jar")) {
            int index = path.indexOf("!/");
            File jarFile = new File(path.substring(5, index));
            try {
                codeSource = new CodeSource(jarFile.toURI().toURL(), (CodeSigner[]) null);
            } catch (MalformedURLException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
    return weaveAndDefineClass(name, bytes, codeSource);
}

From source file:test.APIexample.java

private static void populateFileWithTuneTags(File file, Song song) throws IOException {
    try {/*from w  ww  .j  ava2 s  .c om*/
        AudioFile f = AudioFileIO.read(file);
        Tag tag = f.getTag();
        if (tag == null) {
            tag = new ID3v24Tag();
        }
        tag.setField(FieldKey.ALBUM, song.getAlbum());
        tag.setField(FieldKey.ALBUM_ARTIST, song.getAlbumArtist());
        tag.setField(FieldKey.ARTIST, song.getArtist());
        tag.setField(FieldKey.COMPOSER, song.getComposer());
        tag.setField(FieldKey.DISC_NO, String.valueOf(song.getDisc()));
        tag.setField(FieldKey.DISC_TOTAL, String.valueOf(song.getTotalDiscs()));
        tag.setField(FieldKey.GENRE, song.getGenre());
        tag.setField(FieldKey.TITLE, song.getTitle());
        tag.setField(FieldKey.TRACK, String.valueOf(song.getTrack()));
        tag.setField(FieldKey.TRACK_TOTAL, String.valueOf(song.getTotalTracks()));
        tag.setField(FieldKey.YEAR, String.valueOf(song.getYear()));

        if (song.getAlbumArtUrl() != null) {
            Artwork artwork = new Artwork();
            artwork.setBinaryData(Resources.toByteArray(song.getAlbumArtUrlAsURI().toURL()));
            tag.addField(artwork);

            /*
             * File imageFile = new File(new File(".") +
             * System.getProperty("path.separator") + song.getId() + ".im");
             * Files
             * .write(Resources.toByteArray(song.getAlbumArtUrlAsURI().toURL
             * ()), imageFile); artwork.setFromFile(imageFile);
             */
        }

        f.setTag(tag);
        AudioFileIO.write(f);
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:net.malisis.advert.advert.ServerAdvert.java

@Override
protected void downloadFile() {
    if (checkOldFile())
        return;/*  ww w.  j a va  2  s  .  c o m*/

    if (task != null && !task.isDone())
        return;

    task = threadPool.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            byte[] img = null;
            try {
                img = Resources.toByteArray(new URL(url));
            } catch (Exception e) {
                setError(e.getMessage());
                MalisisAdvert.log.error(e);
            }

            writeFile(img);
            writeListing();
            sendImageData();
            return null;
        }
    });
}

From source file:com.netflix.exhibitor.core.rest.UIResource.java

@Path("{file:.*}")
@GET/*from  w w w  . j  a v a  2s  . c  om*/
public Response getResource(@PathParam("file") String fileName) throws IOException {
    if (fileName.startsWith(jQueryUiPrefix)) {
        String stripped = fileName.substring(jQueryUiPrefix.length());
        fileName = "css/jquery/" + context.getExhibitor().getJQueryStyle().name().toLowerCase() + "/"
                + stripped;
    }

    URL resource;
    try {
        resource = Resources.getResource("com/netflix/exhibitor/core/ui/" + fileName);
    } catch (IllegalArgumentException dummy) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    String resourceFile = resource.getFile();
    String contentType;
    if (resourceFile.endsWith(".png")) {
        contentType = "image/png"; // not in default mime types
    } else if (resourceFile.endsWith(".js")) {
        contentType = "text/javascript"; // not in default mime types
    } else if (resourceFile.endsWith(".css")) {
        contentType = "text/css"; // not in default mime types
    } else {
        contentType = fileTypeMap.getContentType(resourceFile);
    }
    Object entity;
    if (contentType.startsWith("text/")) {
        entity = Resources.toString(resource, Charset.forName("UTF-8"));
    } else {
        entity = Resources.toByteArray(resource);
    }
    return Response.ok(entity).type(contentType).build();
}

From source file:org.glowroot.weaving.IsolatedWeavingClassLoader.java

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    for (Class<?> bridgeClass : bridgeClasses) {
        if (bridgeClass.getName().equals(name)) {
            return bridgeClass;
        }//w  ww .j a v a2  s .  com
    }
    String resourceName = ClassNames.toInternalName(name) + ".class";
    URL url = getResource(resourceName);
    if (url == null) {
        throw new ClassNotFoundException(name);
    }
    byte[] bytes;
    try {
        bytes = Resources.toByteArray(url);
    } catch (IOException e) {
        throw new ClassNotFoundException("Error loading class", e);
    }
    return weaveAndDefineClass(name, bytes);
}

From source file:org.sonar.plugins.scm.tfs.TfsBlameCommand.java

private static File extractExecutable(TempFolder temp) {
    File executable = temp.newFile("SonarTfsAnnotate", ".exe");
    try {/*from   w  w w .  j  av  a 2s .c  om*/
        Files.write(Resources.toByteArray(TfsBlameCommand.class.getResource("/SonarTfsAnnotate.exe")),
                executable);
    } catch (IOException e) {
        throw new IllegalStateException("Unable to extract SonarTfsAnnotate.exe", e);
    }
    return executable;
}

From source file:org.incode.eurocommercial.contactapp.app.services.export.ExportToWordMenu.java

private void initializeIfNecessary() {
    if (wordprocessingMLPackage == null) {
        try {//from   w w w  .ja  v a2  s .  c  o  m
            final byte[] bytes = Resources
                    .toByteArray(Resources.getResource(this.getClass(), "SimpleObjectsExport.docx"));
            wordprocessingMLPackage = docxService.loadPackage(new ByteArrayInputStream(bytes));
        } catch (IOException | LoadTemplateException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:todoapp.app.services.export.ExportToWordService.java

private void initializeIfNecessary() {
    if (wordprocessingMLPackage == null) {
        try {//from w ww. ja  v  a2  s. c  om
            final byte[] bytes = Resources
                    .toByteArray(Resources.getResource(this.getClass(), "ToDoItemsExport.docx"));
            wordprocessingMLPackage = docxService.loadPackage(new ByteArrayInputStream(bytes));
        } catch (IOException | LoadTemplateException e) {
            throw new RuntimeException(e);
        }
    }
}