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.henrycortez.Controllers.UploadBean.java

public String uploadF() {
    try {//from   w  ww .  j a  va 2s.com
        DocumentoDaoImp dao = new DocumentoDaoImp();
        Documento doc = new Documento();
        doc.setNombredoc(file.getFileName());
        byte[] bytes = IOUtils.toByteArray(file.getInputstream());
        doc.setDocumento(bytes);
        dao.insertar(doc);

    } catch (Exception e) {

    }
    return "listaDocs";
}

From source file:com.jpcasati.loar.beans.LoadFile.java

public void load() throws Exception {
    this.status = "Not Loaded Yet.";
    try {/* w  w w.  j a  v  a2s  .  c o  m*/
        InputStream is = log.getInputStream();
        byte[] bytes = IOUtils.toByteArray(is);
        String logFile = new String(bytes);
        String lines[] = logFile.split("\\r?\\n");

        registers = new ArrayList<>(Arrays.asList(lines));

        this.showLoad = false;
        this.status = "Loaded Successfully.";
    } catch (IOException ex) {
        this.status = "Error on Load File.";
    }
}

From source file:com.opencredo.service.BookServiceImpl.java

private byte[] loadPngFile(String location) {
    try {//from   w  w w.j a v a  2 s.co m
        return IOUtils.toByteArray(getApplicationContext().getResource(location).getInputStream());
    } catch (IOException e) {
        throw new BeanInitializationException("unable to load png file '" + location + "'", e);
    }
}

From source file:cpcc.core.utils.ByteArrayStreamResponseTest.java

@Test
public void shouldReturnByteArrayAsStream() throws IOException {
    byte[] expected = new byte[] { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };

    ByteArrayStreamResponse sut = new ByteArrayStreamResponse(CONTENT_TYPE_TEXT, expected);

    byte[] actual = IOUtils.toByteArray(sut.getStream());

    assertThat(sut.getContentType()).isEqualTo(CONTENT_TYPE_TEXT);
    assertThat(actual).isEqualTo(expected);
}

From source file:ezbake.frack.submitter.util.JarUtilTest.java

@Test
public void addFileToJar() throws IOException {
    File tmpDir = new File("jarUtilTest");
    try {//from  w w w  . j a v a 2 s .c  o  m
        tmpDir.mkdirs();

        // Push the example jar to a file
        byte[] testJar = IOUtils.toByteArray(this.getClass().getResourceAsStream("/example.jar"));
        File jarFile = new File(tmpDir, "example.jar");
        BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(jarFile));
        os.write(testJar);
        os.close();

        // Push the test content to a file
        byte[] testContent = IOUtils.toByteArray(this.getClass().getResourceAsStream("/test.txt"));
        String stringTestContent = new String(testContent);
        File testFile = new File(tmpDir, "test.txt");
        os = new BufferedOutputStream(new FileOutputStream(testFile));
        os.write(testContent);
        os.close();

        assertTrue(jarFile.exists());
        assertTrue(testFile.exists());

        // Add the new file to the jar
        File newJar = JarUtil.addFilesToJar(jarFile, Lists.newArrayList(testFile));
        assertTrue("New jar file exists", newJar.exists());
        assertTrue("New jar is a file", newJar.isFile());

        // Roll through the entries of the new jar and
        JarInputStream is = new JarInputStream(new FileInputStream(newJar));
        JarEntry entry = is.getNextJarEntry();
        boolean foundNewFile = false;
        String content = "";
        while (entry != null) {
            String name = entry.getName();
            if (name.endsWith("test.txt")) {
                foundNewFile = true;
                byte[] buffer = new byte[1];
                while ((is.read(buffer)) > 0) {
                    content += new String(buffer);
                }
                break;
            }
            entry = is.getNextJarEntry();
        }
        is.close();
        assertTrue("New file was in repackaged jar", foundNewFile);
        assertEquals("Content of added file is the same as the retrieved new file", stringTestContent, content);
    } finally {
        FileUtils.deleteDirectory(tmpDir);
    }
}

From source file:io.apicurio.hub.api.github.GitHubCreateBlob.java

/**
 * @param content the content to set//from   w ww .  j a va  2  s.  co m
 */
public void setContent(InputStream content) throws IOException {
    this.content = Base64.encodeBase64String(IOUtils.toByteArray(content));
    this.encoding = "base64";
}

From source file:com.predic8.membrane.core.interceptor.schemavalidation.JSONSchemaValidationTest.java

private void validate(String schema, String json, boolean success) throws IOException, Exception {
    final StringBuffer sb = new StringBuffer();
    FailureHandler fh = new FailureHandler() {
        @Override/* w ww . j av a 2  s  .c  om*/
        public void handleFailure(String message, Exchange exc) {
            sb.append(message);
            sb.append("\n");
        }
    };
    JSONValidator jsonValidator = new JSONValidator(new ResolverMap(), schema, fh);
    Request request = new Request.Builder().body(IOUtils.toByteArray(getClass().getResourceAsStream(json)))
            .build();
    Exchange exchange = new Exchange(null);
    jsonValidator.validateMessage(exchange, request, "request");
    if (success)
        Assert.assertTrue(sb.toString(), sb.length() == 0);
    else
        Assert.assertTrue("No error occurred, but expected one.", sb.length() != 0);
}

From source file:com.seer.datacruncher.streams.ZipStreamTest.java

@Test
public void testZipStream() {
    String fileName = properties.getProperty("zip_test_stream_file_name");
    InputStream in = this.getClass().getClassLoader().getResourceAsStream(stream_file_path + fileName);
    byte[] arr = null;
    try {//from w w  w.j a  va  2 s .co  m
        arr = IOUtils.toByteArray(in);
    } catch (IOException e) {
        assertTrue("IOException while Zip test file reading", false);
    }

    ZipInputStream inStream = null;
    try {
        inStream = new ZipInputStream(new ByteArrayInputStream(arr));
        ZipEntry entry;
        while (!(isStreamClose(inStream)) && (entry = inStream.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                DatastreamsInput datastreamsInput = new DatastreamsInput();
                datastreamsInput.setUploadedFileName(entry.getName());
                byte[] byteInput = IOUtils.toByteArray(inStream);
                String res = datastreamsInput.datastreamsInput(null, (Long) schemaEntity.getIdSchema(),
                        byteInput, true);
                assertTrue("Zip file validation failed", Boolean.parseBoolean(res));
            }
            inStream.closeEntry();
        }
    } catch (IOException ex) {
        assertTrue("Error occured during fetch records from ZIP file.", false);
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
            }
    }
}

From source file:com.cloudera.parcel.validation.ParcelTestUtils.java

private static <T> T parseJson(Parser<T> parser, String path) {
    try {/* w  w w. j av a  2 s . co m*/
        InputStream stream = ParcelTestUtils.class.getResourceAsStream(path);
        return parser.parse(IOUtils.toByteArray(stream));
    } catch (IOException io) {
        throw new RuntimeException(io);
    }
}

From source file:eu.planets_project.services.datatypes.ContentTests.java

@Before
public void init() throws IOException {
    file = new File(LOCATION);
    url = file.toURI().toURL();//from   w w w .j  a  va2 s  .  co m
    stream = url.openStream();
    byteArray = IOUtils.toByteArray(new FileInputStream(file));
    content = FileUtils.readFileToString(file);
}