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.leclercb.commons.api.license.LicenseManager.java

public static String keyEncoder(InputStream publicKey) throws Exception {
    byte[] input = IOUtils.toByteArray(publicKey);

    Base64 base64 = new Base64(40);
    byte[] data = base64.encode(input);

    return new String(data, "UTF-8");
}

From source file:eu.europa.ejusticeportal.dss.model.SigningMethodsHomeTest.java

/**
 * Test filter by platform./*from ww w.j a v a2s.c o m*/
 * @throws IOException 
 * @throws UnsupportedEncodingException 
 */
@Test
public void testByPlatform() throws UnsupportedEncodingException, IOException {
    PortalFacade portal = Mockito.mock(PortalFacade.class);
    InputStream is = SigningMethodsHomeTest.class.getClassLoader().getResourceAsStream("TestSigningRepo.xml");

    Mockito.when(portal.getCardProfileXML()).thenReturn(new String(IOUtils.toByteArray(is), "UTF-8"));

    Map<String, List<SigningMethod>> methods = SigningMethodsHome.getInstance().getSigningMethods(portal,
            "blah blah windows blah blah");
    Map<String, List<SigningMethod>> methods2 = SigningMethodsHome.getInstance().getSigningMethods(portal,
            "blah blah linux blah blah");
    Map<String, List<SigningMethod>> methods3 = SigningMethodsHome.getInstance().getSigningMethods(portal);

    assertEquals(1, getMethodsCount(methods3) - getMethodsCount(methods2));
    assertEquals(getMethodsCount(methods3), getMethodsCount(methods));
}

From source file:net.contextfw.web.application.internal.development.ReloadingClassLoader.java

private byte[] loadClassData(String className) throws IOException {
    InputStream stream = super.getResourceAsStream(className.replaceAll("\\.", "/") + ".class");
    if (stream != null) {
        DataInputStream dis = new DataInputStream(stream); // NOSONAR
        byte buff[] = IOUtils.toByteArray(stream);
        dis.close();/*from ww w .j av a2s.  com*/
        return buff;
    } else {
        return null;
    }
}

From source file:com.github.jcooky.jaal.agent.bytecode.asm.ASM5ProfilingTest.java

@Test
public void testInject() throws Throwable {
    InjectorStrategy is = new ProfilingInjectorStrategy(ProfilerFactory.class.getName(), Restrictions.any());
    ASM5Injector injector = new ASM5Injector(is);

    Class<?> cls = classLoader.defineClass(injector.inject(IOUtils.toByteArray(
            ClassLoader.getSystemResourceAsStream(Target.class.getName().replace('.', '/') + ".class"))));

    Object self = cls.newInstance();
    String str = (String) cls.getMethod("methodTarget").invoke(self);
    assertThat(str, is("hello world123"));
    str = (String) cls.getMethod("method", boolean.class).invoke(null, false);
    assertThat(str, is("Test"));

    verify(ProfilerFactory.PROFILER, times(3)).begin(any(ClassType.class), any(MethodType.class));
    verify(ProfilerFactory.PROFILER, times(3)).end(any(ClassType.class), any(MethodType.class), anyLong(),
            any(Throwable.class), anyLong());
}

From source file:eu.europa.esig.dss.xades.signature.XAdESLevelBDetachedDigestDocumentTest.java

@Before
public void init() throws Exception {
    File file = new File("src/test/resources/sample.xml");
    DigestDocument digestDocument = new DigestDocument(file);
    FileInputStream fis = new FileInputStream(file);
    byte[] bytes = IOUtils.toByteArray(fis);
    IOUtils.closeQuietly(fis);/*from w w w.  j a va2  s .c  o m*/
    String computedDigest = Base64.encodeBase64String(DSSUtils.digest(DigestAlgorithm.SHA256, bytes));
    digestDocument.addDigest(DigestAlgorithm.SHA256, computedDigest);

    documentToSign = digestDocument;

    CertificateService certificateService = new CertificateService();
    privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256);

    signatureParameters = new XAdESSignatureParameters();
    signatureParameters.bLevel().setSigningDate(new Date());
    signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
    signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
    signatureParameters.setSignaturePackaging(SignaturePackaging.DETACHED);
    signatureParameters.setSignatureLevel(SignatureLevel.XAdES_BASELINE_B);

    CertificateVerifier certificateVerifier = new CommonCertificateVerifier();
    service = new XAdESService(certificateVerifier);
}

From source file:com.migo.oss.QcloudCloudStorageService.java

@Override
public String upload(InputStream inputStream, String path) {
    try {//from   w w w  . j a  v  a2s.  c  o  m
        byte[] data = IOUtils.toByteArray(inputStream);
        return this.upload(data, path);
    } catch (IOException e) {
        throw new RRException("", e);
    }
}

From source file:CA.InternalCA.java

/**
 * Method to read cert from file.//from   ww w  . j  av  a2s .  c  o m
 *
 * @param inputStream
 *
 * @return
 */
private X509CertificateHolder readCert(InputStream inputStream) {
    X509CertificateHolder x509CertificateHolder;
    try {
        x509CertificateHolder = new X509CertificateHolder(IOUtils.toByteArray(inputStream));
    } catch (Exception e) {
        LOG.info("Cannot parse Internal CA certificate: " + e.getMessage());
        return null;
    }
    return x509CertificateHolder;
}

From source file:mobisocial.musubi.provider.IdentityCursorWrapper.java

@Override
public byte[] getBlob(int columnIndex) {
    if (columnIndex != colThumb) {
        return super.getBlob(columnIndex);
    }/*from w  ww.  j a  v  a  2  s .  c o  m*/
    long identityId = getLong(getColumnCount() - 1);
    IdentitiesManager im = new IdentitiesManager(App.getDatabaseSource(mContext));
    MIdentity ident = im.getIdentityWithThumbnailsForId(identityId);
    if (ident == null) {
        return null;
    }
    byte[] thumbnail = ident.musubiThumbnail_;
    if (thumbnail == null) {
        thumbnail = ident.thumbnail_;
    }
    if (thumbnail == null) {
        try {
            InputStream is = mContext.getResources().openRawResource(R.drawable.ic_contact_picture);
            thumbnail = IOUtils.toByteArray(is);
        } catch (IOException e) {
        }
    }
    return thumbnail;
}

From source file:io.lightlink.excel.StreamingExcelTransformer.java

public void doExport(InputStream template, OutputStream out, ExcelStreamVisitor visitor) throws IOException {
    try {/*from w  ww . ja  v a 2  s.c o m*/
        ZipInputStream zipIn = new ZipInputStream(template);
        ZipOutputStream zipOut = new ZipOutputStream(out);

        ZipEntry entry;

        Map<String, byte[]> sheets = new HashMap<String, byte[]>();

        while ((entry = zipIn.getNextEntry()) != null) {

            String name = entry.getName();

            if (name.startsWith("xl/sharedStrings.xml")) {

                byte[] bytes = IOUtils.toByteArray(zipIn);
                zipOut.putNextEntry(new ZipEntry(name));
                zipOut.write(bytes);

                sharedStrings = processSharedStrings(bytes);

            } else if (name.startsWith("xl/worksheets/sheet")) {
                byte[] bytes = IOUtils.toByteArray(zipIn);
                sheets.put(name, bytes);
            } else if (name.equals("xl/calcChain.xml")) {
                // skip this file, let excel recreate it
            } else if (name.equals("xl/workbook.xml")) {
                zipOut.putNextEntry(new ZipEntry(name));

                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();
                Writer writer = new OutputStreamWriter(zipOut, "UTF-8");

                byte[] bytes = IOUtils.toByteArray(zipIn);
                saxParser.parse(new ByteArrayInputStream(bytes), new WorkbookTemplateHandler(writer));

                writer.flush();
            } else {
                zipOut.putNextEntry(new ZipEntry(name));
                IOUtils.copy(zipIn, zipOut);
            }

        }

        for (Map.Entry<String, byte[]> sheetEntry : sheets.entrySet()) {
            String name = sheetEntry.getKey();

            byte[] bytes = sheetEntry.getValue();
            zipOut.putNextEntry(new ZipEntry(name));
            processSheet(bytes, zipOut, visitor);
        }

        zipIn.close();
        template.close();

        zipOut.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.toString(), e);
    }
}

From source file:com.ctriposs.r2.filter.compression.DeflateCompressor.java

@Override
public byte[] deflate(InputStream data) throws CompressionException {
    byte[] input;
    try {//from w  w  w.j  a v  a  2 s .  c om
        input = IOUtils.toByteArray(data);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM,
                e);
    }

    Deflater zlib = new Deflater();
    zlib.setInput(input);
    zlib.finish();

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];

    int bytesRead;
    while (!zlib.finished()) {
        bytesRead = zlib.deflate(temp);

        if (bytesRead == 0) {
            if (!zlib.needsInput()) {
                throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
            } else {
                break;
            }
        }
        output.write(temp, 0, bytesRead);
    }
    zlib.end();

    return output.toByteArray();
}