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.yahoo.gondola.container.impl.ZookeeperConfigProviderTest.java

@Test
public void testGetConfigFile() throws Exception {
    File configFile = configProvider.getConfigFile();
    assertTrue(Arrays.equals(IOUtils.toByteArray(configFile.toURI()), IOUtils.toByteArray(file)));
}

From source file:common.rest.client.transport.DefaultResponseHandler.java

@SuppressWarnings("unchecked")
@Override//from www  .jav a2  s .c o  m
public ITransport.Response<R, StatusEntity> handle(final int statusCode, final String statusReason,
        final Map<String, String> responseHeaders, final InputStream responseStream) throws IOException {
    final byte[] responseBytes = responseStream != null ? IOUtils.toByteArray(responseStream) : null;
    String response = responseBytes != null ? new String(responseBytes, "UTF-8") : null;
    ITransport.Response<R, StatusEntity> result;

    if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
        final R resource = (m_collectionType != null) ? (R) m_mapper.readValue(responseBytes, m_collectionType)
                : m_mapper.readValue(responseBytes, m_resourceType);
        result = new ITransport.Response<R, StatusEntity>(resource, null, responseHeaders);
        result.setResponse(response);
    } else if (statusCode == HttpStatus.SC_NO_CONTENT) {
        result = new ITransport.Response<R, StatusEntity>(null, null, responseHeaders);
    } else {
        final StatusEntity error = new StatusEntity(Integer.toString(statusCode), response);
        result = new ITransport.Response<R, StatusEntity>(null, error, null);
    }
    result.setHttpStatusCode(statusCode);
    result.setHttpStatusReason(statusReason);
    return result;
}

From source file:com.amazonaws.services.s3.internal.MD5DigestCalculatingInputStreamTest.java

@Test
public void test() throws Exception {
    byte[] data = "0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=".getBytes(StringUtils.UTF8);
    byte[] md5Expected = Md5Utils.computeMD5Hash(data);
    byte[] baExpected;
    {/*from   w ww . j a va  2s  . c o  m*/
        MD5DigestCalculatingInputStream is = new MD5DigestCalculatingInputStream(
                new ByteArrayInputStream(data));
        baExpected = IOUtils.toByteArray(is);
        byte[] md5 = is.getMd5Digest();
        assertTrue(Arrays.equals(md5Expected, md5));
    }
    {
        MD5DigestCalculatingInputStream is = new MD5DigestCalculatingInputStream(
                new ByteArrayInputStream(data));
        is.mark(-1);
        baExpected = IOUtils.toByteArray(is);
        byte[] md5 = is.getMd5Digest();
        assertTrue(Arrays.equals(md5Expected, md5));

        is.reset();
        byte[] ba = IOUtils.toByteArray(is);
        byte[] md5_2 = is.getMd5Digest();
        assertTrue(Arrays.equals(md5Expected, md5_2));
        assertTrue(Arrays.equals(baExpected, ba));
    }
    {
        MD5DigestCalculatingInputStream is = new MD5DigestCalculatingInputStream(
                new ByteArrayInputStream(data));
        is.mark(-1);
        is.read(new byte[10]);
        is.reset();
        byte[] ba = IOUtils.toByteArray(is);
        if (DEBUG)
            System.out.println("ba.length: " + ba.length);
        assertTrue(Arrays.equals(baExpected, ba));
        byte[] md5 = is.getMd5Digest();
        assertTrue(Arrays.equals(md5Expected, md5));
    }
    {
        MD5DigestCalculatingInputStream is = new MD5DigestCalculatingInputStream(
                new ByteArrayInputStream(data));
        is.mark(-1);
        is.read(new byte[10]);
        is.mark(-1);
        is.read(new byte[10]);
        is.reset();
        byte[] ba = IOUtils.toByteArray(is);
        if (DEBUG)
            System.out.println("ba.length: " + ba.length);
        assertFalse(Arrays.equals(baExpected, ba));
        byte[] md5 = is.getMd5Digest();
        assertTrue(Arrays.equals(md5Expected, md5));
    }
    {
        MD5DigestCalculatingInputStream is = new MD5DigestCalculatingInputStream(
                new ByteArrayInputStream(data));
        is.read(new byte[10]);
        is.mark(-1);
        is.read(new byte[10]);
        is.reset();
        is.read(new byte[10]);
        is.mark(-1);
        is.read();
        is.reset();
        byte[] ba = IOUtils.toByteArray(is);
        if (DEBUG)
            System.out.println("ba.length: " + ba.length);
        assertFalse(Arrays.equals(baExpected, ba));
        byte[] md5 = is.getMd5Digest();
        assertTrue(Arrays.equals(md5Expected, md5));
    }
}

From source file:es.uma.inftel.blog.bean.PerfilBean.java

public String modificarUsuario() {
    Usuario usuario = usuarioBean.getUsuario();
    if (password.equals("")) {
        password = usuarioBean.getUsuario().getPassword();
    }//from   ww  w . j  ava2 s. c  o  m
    if (email.equals("")) {
        email = usuarioBean.getUsuario().getEmail();
    }

    usuario.setPassword(password);
    usuario.setEmail(email);
    if (avatar != null) {
        try {
            usuario.setAvatar(IOUtils.toByteArray(avatar.getInputStream()));
        } catch (IOException ex) {
            Logger.getLogger(PerfilBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    usuarioFacade.modifyUser(usuario);
    return "perfil?faces-redirect=true";
}

From source file:com.basistech.readability.AbstractPageReader.java

protected String readContent(InputStream response, String forceEncoding) throws IOException {
    byte[] bytes = IOUtils.toByteArray(response);
    charset = null;/*from  w w w  . j a  v a  2s  .com*/
    String hint = null;
    if (forceEncoding != null) {
        serverReturnedEncoding = true;
        try {
            charset = Charset.forName(forceEncoding);
            hint = charset.name();
        } catch (Exception e) {
            //
        }
    }
    if (charsetDetector != null && !respectServerEncoding || charset == null) {

        try {
            String charsetName = charsetDetector.detect(bytes, hint);
            if (charsetName != null) {
                try {
                    charset = Charset.forName(charsetName);
                    detectedEncoding = charset.name();
                } catch (Exception e) {
                    LOG.warn("Detected character set " + charsetName + " not supported");
                }
            }
        }

        catch (Exception e) {
            LOG.warn("Failed to detect bytes " + e.toString());
        }
    }
    if (charset == null) {
        LOG.warn("Defaulting to utf-8");
        charset = UTF8;
    }
    return new String(bytes, charset);
}

From source file:com.newinit.filter.XSSRequestWrapper.java

@Override
public BufferedReader getReader() throws IOException {
    if (rawData == null) {
        rawData = IOUtils.toByteArray(this.request.getReader());
        servletStream.stream = new ByteArrayInputStream(rawData);
    }//from   w  ww . j a va  2  s .c o  m
    return new BufferedReader(new InputStreamReader(servletStream));
}

From source file:com.chnoumis.commons.zip.utils.test.ZipUtilsTest.java

public void testZiptoZipInfo() throws Exception {

    // Archive/*from   w w w .  ja  va2  s.  c  o  m*/
    List<ZipInfo> zipInfos = new ArrayList<ZipInfo>();
    File output = new File("target/bla.zip");

    ZipInfo zipinfo = new ZipInfo();
    zipinfo.setFileName("src/test/resources/1.txt");
    FileInputStream v = new FileInputStream("src/test/resources/1.txt");
    zipinfo.setFileContent(IOUtils.toByteArray(v));
    zipInfos.add(zipinfo);

    ZipInfo zipinfo1 = new ZipInfo();
    zipinfo1.setFileName("src/test/resources/2.txt");
    FileInputStream q = new FileInputStream("src/test/resources/2.txt");
    zipinfo1.setFileContent(IOUtils.toByteArray(q));
    zipInfos.add(zipinfo1);

    OutputStream out = new FileOutputStream(output);
    //OutputStream out = new ByteArrayOutputStream();

    //ZipUtils zipUtils = new ZipUtils();

    ZipUtils.zip(zipInfos, out);

}

From source file:cpcc.rv.base.pages.update.UpdateConfiguration.java

Object onActivate() throws IOException {
    InputStream inputStream = requestGlobals.getHTTPServletRequest().getInputStream();
    byte[] requestData = IOUtils.toByteArray(inputStream);

    synchronizer.importConfiguration(requestData);

    JSONObject jsonObject = new JSONObject("result", "OK");
    return new JsonStreamResponse(jsonObject);
}

From source file:com.googlecode.dex2jar.reader.CCZipExtractor.java

@Override
public byte[] extract(byte[] data, String name) throws IOException {
    ZipArchiveInputStream zis = null;/*  w w  w  . j a  va 2  s .co m*/
    try {
        zis = new ZipArchiveInputStream(new ByteArrayInputStream(data));
        for (ZipArchiveEntry e = zis.getNextZipEntry(); e != null; e = zis.getNextZipEntry()) {
            e.getGeneralPurposeBit().useEncryption(false);
            if (e.getName().equals(name)) {
                data = IOUtils.toByteArray(zis);
                zis.close();
                return data;
            }
        }
    } finally {
        IOUtils.closeQuietly(zis);
    }
    throw new IOException("can't find classes.dex in the zip");
}

From source file:com.google.code.ddom.commons.cl.TransformingClassLoader.java

@Override
protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
    Class<?> clazz = findLoadedClass(name);
    if (clazz == null) {
        if (needsTransformation(name)) {
            String resourceName = ClassLoaderUtils.getResourceNameForClassName(name);
            InputStream in = super.getResourceAsStream(resourceName);
            if (in == null) {
                throw new ClassNotFoundException(name);
            }// w w w  .  j a v  a 2 s .  co  m
            try {
                try {
                    byte[] classDef = transformClass(name, IOUtils.toByteArray(in));
                    clazz = defineClass(name, classDef, 0, classDef.length);
                } finally {
                    in.close();
                }
            } catch (IOException ex) {
                throw new ClassNotFoundException(name, ex);
            }
        } else {
            clazz = super.loadClass(name, false);
        }
    }
    if (resolve) {
        resolveClass(clazz);
    }
    return clazz;
}