Search in sources :

Example 36 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestBinary method testBinarySerialize.

@Test
public void testBinarySerialize() throws IOException, ClassNotFoundException {
    NavajoFactory.getInstance().setSandboxMode(true);
    Binary binaryx = new Binary(getClass().getResourceAsStream("binary1.txt"));
    binaryx.getLength();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(binaryx);
    byte[] bb = baos.toByteArray();
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bb));
    Binary ooo = (Binary) ois.readObject();
    Assert.assertTrue(ooo.equals(binaryx));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Binary(com.dexels.navajo.document.types.Binary) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 37 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestBinary method testEqual6.

@Test
public void testEqual6() throws IOException, URISyntaxException {
    Binary binaryStreamed = new Binary(getClass().getResourceAsStream("logo.gif"));
    Path path = Paths.get(getClass().getResource("logo.gif").toURI());
    byte[] data = Files.readAllBytes(path);
    Binary binaryByteArray = new Binary(data);
    Assert.assertEquals(binaryStreamed, binaryByteArray);
}
Also used : Path(java.nio.file.Path) Binary(com.dexels.navajo.document.types.Binary) Test(org.junit.Test)

Example 38 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestBinary method testMimeDetection4.

@Test
// Ignore until we find a proper fix for this problem...
@Ignore
public void testMimeDetection4() throws IOException {
    // File-based test - no sandbox mode
    NavajoFactory.getInstance().setSandboxMode(false);
    URL url = this.getClass().getResource("doc3.docx");
    Binary binaryx = new Binary(new File(url.getFile()));
    Assert.assertEquals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", binaryx.guessContentType());
}
Also used : Binary(com.dexels.navajo.document.types.Binary) File(java.io.File) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 39 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class Base64ImageStringToBinary method evaluate.

// I think this function does not work when you pass a Binary into it.
// It is a bit pointless anyway, it is more like a 'clone' then.
@Override
public Object evaluate() throws TMLExpressionException {
    String data = getStringOperand(0);
    Binary b;
    String partSeparator = ",";
    if (data.contains(partSeparator)) {
        String encodedImg = data.split(partSeparator)[1];
        // Basic Base64 decoding
        byte[] decodedValue = Base64.getDecoder().decode(encodedImg.getBytes(StandardCharsets.UTF_8));
        b = new Binary(decodedValue);
        return b;
    } else if (data != null && !data.contains(partSeparator)) {
        // Might be just the base64 string without id part
        // Basic Base64 decoding
        byte[] decodedValue = Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8));
        b = new Binary(decodedValue);
        return b;
    }
    return null;
}
Also used : Binary(com.dexels.navajo.document.types.Binary)

Example 40 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestBinary method testResolveOnTransport.

@Test
public void testResolveOnTransport() throws IOException {
    URL u = new URL("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
    Binary b = new Binary(u, true);
    Assert.assertFalse(b.isResolved());
    StringWriter sw = new StringWriter();
    b.writeBase64(sw);
    sw.close();
    String result = sw.toString();
    Assert.assertTrue(b.isResolved());
    Assert.assertTrue(result.length() > 1000);
}
Also used : StringWriter(java.io.StringWriter) Binary(com.dexels.navajo.document.types.Binary) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Binary (com.dexels.navajo.document.types.Binary)139 Test (org.junit.Test)38 IOException (java.io.IOException)32 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 File (java.io.File)25 Ignore (org.junit.Ignore)17 Property (com.dexels.navajo.document.Property)16 URL (java.net.URL)16 UserException (com.dexels.navajo.script.api.UserException)14 OutputStream (java.io.OutputStream)13 FileOutputStream (java.io.FileOutputStream)12 Navajo (com.dexels.navajo.document.Navajo)11 MappableException (com.dexels.navajo.script.api.MappableException)11 FileInputStream (java.io.FileInputStream)9 InputStream (java.io.InputStream)9 Message (com.dexels.navajo.document.Message)8 StringWriter (java.io.StringWriter)8 OutputStreamWriter (java.io.OutputStreamWriter)7 NavajoException (com.dexels.navajo.document.NavajoException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6