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.cazcade.billabong.store.impl.MapBasedBinaryStore.java

/**
 * Basic implementation of addToMap that uses a simple InMemoryBinaryStoreEntry. This method can be overridden to
 * allow other entry types to be used.//w w w  . j  ava2 s . c  o m
 *
 * @param storeKey The key to use for adding the entry.
 * @param data     The data to be held in the entry.
 */
protected void addToMap(String storeKey, InputStream data) {
    try {
        if (data != null) {
            map.put(storeKey, new InMemoryBinaryStoreEntry(dateHelper.current(), IOUtils.toByteArray(data)));
        } else {
            map.remove(storeKey);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.android.email.mail.store.imap.ImapStringTest.java

public void testBasics() throws Exception {
    final ImapSimpleString s = new ImapSimpleString("AbcD");
    assertFalse(s.isEmpty());/*from   w  ww . j a  v a  2  s . c o  m*/
    assertEquals("AbcD", s.getString());
    assertEquals("AbcD", Utility.fromAscii(IOUtils.toByteArray(s.getAsStream())));

    assertFalse(s.isNumber());
    assertFalse(s.isDate());

    assertFalse(s.is(null));
    assertFalse(s.is(""));
    assertTrue(s.is("abcd"));
    assertFalse(s.is("abc"));

    assertFalse(s.startsWith(null));
    assertTrue(s.startsWith(""));
    assertTrue(s.startsWith("a"));
    assertTrue(s.startsWith("abcd"));
    assertFalse(s.startsWith("Z"));
    assertFalse(s.startsWith("abcde"));
}

From source file:inti.ws.spring.resource.ByteWebResource.java

/**
 * Reads the file and stores it's content.
 *///from w w w  . ja  v  a2  s . co  m
@Override
public void update() throws Exception {
    StringBuilder builder = new StringBuilder(32);
    MessageDigest digest = DIGESTS.get();
    InputStream inputStream = resource.getInputStream();

    try {
        lastModified = resource.lastModified();
        bytes = IOUtils.toByteArray(inputStream);
    } finally {
        inputStream.close();
    }

    digest.reset();
    builder.append(Hex.encodeHexString(digest.digest(bytes)));
    messageDigest = builder.toString();
    builder.delete(0, builder.length());

    DATE_FORMATTER.formatDate(lastModified, builder);
    lastModifiedString = builder.toString();
}

From source file:br.com.localizae.model.service.FileServiceLocal.java

@Override
public byte[] download(String uri) throws Exception {
    byte[] file = null;

    try {//from ww w .ja v  a2s  .c  o  m
        Connection conn = ConnectionManager.getInstance().getConnection();
        FileDAO dao = new FileDAO();

        Map<Enum, Object> criteria = new HashMap<>();
        File arq = dao.readByCriteria(conn, criteria, 0l, 0l).get(0);
        FileInputStream fis = new FileInputStream(Constantes.PATH_FILE + arq.getUri());
        arq.setFile(IOUtils.toByteArray(fis));
        file = arq.getFile();
        fis.close();
    } catch (Exception ex) {
        throw ex;
    }

    return file;
}

From source file:com.netflix.niws.client.http.TestResource.java

@POST
@Path("/postStream")
@Consumes({ MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_XML })
public Response handlePost(final InputStream in, @HeaderParam("Transfer-Encoding") String transferEncoding) {
    try {/*from ww  w  .j av a2s. c  om*/
        byte[] bytes = IOUtils.toByteArray(in);
        String entity = new String(bytes, "UTF-8");
        return Response.ok(entity).build();
    } catch (Exception e) {
        return Response.serverError().build();
    }
}

From source file:ee.ria.xroad.proxy.testsuite.testcases.Utf8BomNormalSubsystem.java

@Override
protected void validateNormalResponse(Message receivedResponse) throws Exception {

    RequestHash requestHashFromResponse = ((SoapMessageImpl) receivedResponse.getSoap()).getHeader()
            .getRequestHash();// ww  w . j av a  2 s. co m

    byte[] requestHash = CryptoUtils.calculateDigest(
            CryptoUtils.getAlgorithmId(requestHashFromResponse.getAlgorithmId()),
            IOUtils.toByteArray(getRequestInput(addUtf8BomToRequestFile).getRight()));

    if (!Arrays.areEqual(requestHash, CryptoUtils.decodeBase64(requestHashFromResponse.getHash()))) {
        throw new RuntimeException("Request message hash does not match request message");
    }
}

From source file:edu.eci.pdsw.samples.managedbeans.ImageBean.java

public void upload() {
    byte[] img = null;
    if (file != null) {
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
        try {/*www.  j  a  va  2  s  .c  o  m*/
            img = IOUtils.toByteArray(file.getInputstream());
        } catch (IOException ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO, ex.getMessage(), null));
        }
        ima = new Image(img, "prueba");
    }

}

From source file:info.magnolia.filesystembrowser.app.contentconnector.FileContentProperty.java

private byte[] getBytes(File file) {
    if (file.exists()) {
        FileInputStream fis = null;
        try {/*from  w w  w.j  a v a2  s  . c om*/
            fis = new FileInputStream(file);
            return IOUtils.toByteArray(fis);
        } catch (FileNotFoundException e) {
            log.error("File not {1} found: ", file.getPath());
        } catch (IOException e) {
            log.error("I/O issue {1} encountered: ", e.getMessage());
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }
    return new byte[0];
}

From source file:com.dalabs.droop.util.password.FilePasswordLoader.java

/**
 * Read bytes from given file./*from   w w  w.j a  v a  2s  . com*/
 *
 * @param fs Associated FileSystem
 * @param path Path
 * @return
 * @throws IOException
 */
protected byte[] readBytes(FileSystem fs, Path path) throws IOException {
    InputStream is = fs.open(path);
    try {
        return IOUtils.toByteArray(is);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlImageDownloadTest.java

/**
 * @throws Exception if the test fails//from www  . j  a  va2 s  . co m
 */
@Test
public void imageFileSize() throws Exception {
    final HtmlImage htmlimage = getHtmlElementToTest("image1");
    assertEquals("Image filesize", 140144,
            IOUtils.toByteArray(htmlimage.getWebResponse(true).getContentAsStream()).length);
}