Example usage for org.apache.commons.io IOUtils toByteArray

List of usage examples for org.apache.commons.io IOUtils toByteArray

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils toByteArray.

Prototype

public static byte[] toByteArray(String input) throws IOException 

Source Link

Document

Get the contents of a String as a byte[] using the default character encoding of the platform.

Usage

From source file:com.oneis.app.FileController.java

public static ZipFileExtractionResults zipFileExtraction(String zipFilePathname, String contentsDefault,
        String contentsRequested) throws IOException {
    ZipFileExtractionResults results = new ZipFileExtractionResults();

    ZipFile zipFile = new ZipFile(zipFilePathname);
    try {//from   www . j ava2 s. co m
        // Find the right entry, trying for the requested filename by defaulting back on the default given
        ZipEntry e = zipFile.getEntry(contentsRequested);
        results.chosenFile = contentsRequested;
        if (e == null) {
            e = zipFile.getEntry(contentsDefault);
            results.chosenFile = contentsDefault;
        }
        if (e == null) {
            return null;
        }

        results.data = IOUtils.toByteArray(zipFile.getInputStream(e));
    } finally {
        zipFile.close();
    }

    results.etagSuggestion = results.chosenFile.hashCode() & 0xfffff;

    return results;
}

From source file:com.heliosdecompiler.helios.Resources.java

public static void loadAllImages() {
    for (Resources resources : Resources.values()) {
        try {//from w  w  w  . ja va 2 s  .  co m
            resources.data = IOUtils.toByteArray(Resources.class.getResourceAsStream(resources.filePath));
        } catch (IOException exception) {
            ExceptionHandler.handle(exception);
        }
    }
}

From source file:net.darkmist.alib.io.Slurp.java

/**
 * @deprecated Use {@link org.apache.commons.io.IOUtils#toByteArray(java.io.InputStream)} instead.
 *///from  w  w w  .  j  av a 2  s  .  c  om
@Deprecated
public static byte[] slurp(InputStream stream) throws IOException {
    return IOUtils.toByteArray(stream);
}

From source file:com.imag.nespros.network.devices.AMIDevice.java

public AMIDevice(String name, double cpuSpeed, int totalMemory) {
    super(name, cpuSpeed, totalMemory, DeviceType.AMI);
    try {/*from www.j av a  2s  . c o  m*/
        byte[] imageInByte;
        imageInByte = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("image/meter.jpeg"));
        icon = new MyLayeredIcon(new ImageIcon(imageInByte).getImage());
    } catch (IOException ex) {
        Logger.getLogger(AMIDevice.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:crawlercommons.sitemaps.SiteMapTester.java

/**
 * Parses a Sitemap recursively meaning that if the sitemap is a
 * sitemapIndex then it parses all of the internal sitemaps
 *//*from  ww w . j a v a2  s. com*/
private static void parse(URL url, String mt) throws IOException, UnknownFormatException {
    byte[] content = IOUtils.toByteArray(url);

    AbstractSiteMap sm = null;
    // guesses the mimetype
    if (mt == null || mt.equals("")) {
        sm = parser.parseSiteMap(content, url);
    } else {
        sm = parser.parseSiteMap(mt, content, url);
    }

    if (sm.isIndex()) {
        Collection<AbstractSiteMap> links = ((SiteMapIndex) sm).getSitemaps();
        for (AbstractSiteMap asm : links) {
            parse(asm.getUrl(), mt); // Recursive call
        }
    } else {
        Collection<SiteMapURL> links = ((SiteMap) sm).getSiteMapUrls();
        for (SiteMapURL smu : links) {
            LOG.info(smu.getUrl().toString());
        }
    }
}

From source file:net.es.nsi.topology.translator.http.Decoder.java

private static byte[] gunzip(ByteArrayInputStream is) throws IOException {
    try (GZIPInputStream gis = new GZIPInputStream(is)) {
        return IOUtils.toByteArray(gis);
    } catch (IOException io) {
        log.error("Failed to gunzip document", io);
        throw io;
    }//from   w  ww .ja  v a  2  s . com
}

From source file:cd.go.authentication.ldap.utils.Util.java

public static byte[] readResourceBytes(String resourceFile) {
    try (InputStream in = Util.class.getResourceAsStream(resourceFile)) {
        return IOUtils.toByteArray(in);
    } catch (IOException e) {
        throw new RuntimeException("Could not find resource " + resourceFile, e);
    }/* www.j  av a2 s.c  o m*/
}

From source file:com.imag.nespros.network.devices.PADevice.java

public PADevice(String name) {
    super(name);/*from ww  w .ja  va  2s .  co m*/
    this.setCpuSpeed(1);
    this.setTotalMemory(5);
    this.setDeviceType(DeviceType.PA);
    this.setDeviceName(name);

    try {
        byte[] imageInByte;
        imageInByte = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("image/pa.jpg"));
        icon = new MyLayeredIcon(new ImageIcon(imageInByte).getImage());
    } catch (IOException ex) {
        Logger.getLogger(AMIDevice.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.imag.nespros.network.devices.DCDevice.java

public DCDevice(String name) {
    super(name);/*  ww w.j  av  a2 s. c  om*/
    this.setCpuSpeed(100);
    this.setTotalMemory(512);
    this.setDeviceType(DeviceType.DC);
    this.setDeviceName(name);

    try {
        byte[] imageInByte;
        imageInByte = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("image/dc.jpeg"));
        icon = new MyLayeredIcon(new ImageIcon(imageInByte).getImage());
    } catch (IOException ex) {
        Logger.getLogger(AMIDevice.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.github.tddts.jet.util.Util.java

/**
 * Load content of given resource as String.
 *
 * @param resourceName path to resource//  w ww . ja  v a2s  .c  o  m
 * @return file content as String
 * @throws FileLoadingException in case of any errors
 */
public static String loadContent(String resourceName) throws FileLoadingException {
    try (InputStream inputStream = Util.class.getResourceAsStream(resourceName)) {
        if (inputStream == null) {
            throw new FileLoadingException("Not found: " + resourceName);
        }
        return new String(IOUtils.toByteArray(inputStream), StandardCharsets.UTF_8);
    } catch (Exception e) {
        throw new FileLoadingException(e);
    }
}