Search in sources :

Example 6 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class StreamBinaryTest method testStreamBinary.

@Test
public void testStreamBinary() throws IOException {
    final String testValue = "hello world";
    final String xquery = "response:stream-binary(xs:base64Binary('" + Base64.encodeBase64String(testValue.getBytes()) + "'), 'application/octet-stream', 'test.bin')";
    final Request get = Request.Get(getCollectionRootUri() + "?_query=" + URLEncoder.encode(xquery, "UTF-8") + "&_indent=no");
    final HttpResponse response = get.execute().returnResponse();
    assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
        response.getEntity().writeTo(os);
        assertArrayEquals(testValue.getBytes(), os.toByteArray());
    }
}
Also used : Request(org.apache.http.client.fluent.Request) HttpResponse(org.apache.http.HttpResponse) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) RESTTest(org.exist.http.RESTTest) Test(org.junit.Test)

Example 7 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class PatchTest method assertResponse.

private void assertResponse(final Request method, String expectedData) throws IOException {
    final HttpResponse response = method.execute().returnResponse();
    final Matcher<String> valueMatcher = hasSimilarXml("<request><method>PATCH</method><data>" + expectedData + "</data></request>");
    assertHTTPStatusCode(HttpStatus.SC_OK, response);
    try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
        response.getEntity().writeTo(os);
        final String actualResponse = new String(os.toByteArray());
        assertThat(actualResponse, valueMatcher);
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

Example 8 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class ScaleFunction method eval.

/**
 * evaluate the call to the xquery scale() function,
 * it is really the main entry point of this class
 *
 * @param args		arguments from the scale() function call
 * @param contextSequence	the Context Sequence to operate on (not used here internally!)
 * @return		A sequence representing the result of the scale() function call
 *
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was an image and a mime-type speficifed
    if (args[0].isEmpty() || args[2].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the maximum dimensions to scale to
    int maxHeight = MAXHEIGHT;
    int maxWidth = MAXWIDTH;
    if (!args[1].isEmpty()) {
        maxHeight = ((IntegerValue) args[1].itemAt(0)).getInt();
        if (args[1].hasMany())
            maxWidth = ((IntegerValue) args[1].itemAt(1)).getInt();
    }
    // get the mime-type
    String mimeType = args[2].itemAt(0).getStringValue();
    String formatName = mimeType.substring(mimeType.indexOf("/") + 1);
    // TODO currently ONLY tested for JPEG!!!
    Image image = null;
    BufferedImage bImage = null;
    try (// get the image data
    InputStream inputStream = ((BinaryValue) args[0].itemAt(0)).getInputStream()) {
        image = ImageIO.read(inputStream);
        if (image == null) {
            logger.error("Unable to read image data!");
            return Sequence.EMPTY_SEQUENCE;
        }
        // scale the image
        bImage = ImageModule.createThumb(image, maxHeight, maxWidth);
        // get the new scaled image
        try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
            ImageIO.write(bImage, formatName, os);
            // return the new scaled image data
            return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), os.toInputStream());
        }
    } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) InputStream(java.io.InputStream) IntegerValue(org.exist.xquery.value.IntegerValue) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) XPathException(org.exist.xquery.XPathException)

Example 9 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class ByteBufferInputStreamTest method readMultipleBytesSpecificInLoop.

@Test
public void readMultipleBytesSpecificInLoop() throws IOException {
    // generate 1KB of test data
    Random random = new Random();
    byte[] testData = new byte[1024];
    random.nextBytes(testData);
    final ByteBuffer buf = ByteBuffer.wrap(testData);
    InputStream is = new ByteBufferInputStream(new TestableByteBufferAccessor(buf));
    try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(testData.length)) {
        byte[] readBuf = new byte[56];
        int read = -1;
        while ((read = is.read(readBuf, 0, readBuf.length)) > -1) {
            assertLessThanOrEqual(readBuf.length, read);
            baos.write(readBuf, 0, read);
        }
        assertArrayEquals(testData, baos.toByteArray());
    }
}
Also used : Random(java.util.Random) InputStream(java.io.InputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 10 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class RestBinariesTest method streamBinarySax.

/**
 * {@see https://github.com/eXist-db/exist/issues/790#error-case-2}
 *
 * response:stream is used to return Base64 encoded binary.
 */
@Test
public void streamBinarySax() throws JAXBException, IOException {
    final String query = "import module namespace util = \"http://exist-db.org/xquery/util\";\n" + "import module namespace response = \"http://exist-db.org/xquery/response\";\n" + "let $bin := util:binary-doc('" + TEST_COLLECTION.append(BIN1_FILENAME).toString() + "')\n" + "return response:stream($bin, 'media-type=application/octet-stream')";
    final HttpResponse response = postXquery(query);
    final HttpEntity entity = response.getEntity();
    try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
        entity.writeTo(baos);
        assertArrayEquals(BIN1_CONTENT, Base64.decodeBase64(baos.toByteArray()));
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Aggregations

UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)65 Test (org.junit.Test)24 InputStream (java.io.InputStream)23 IOException (java.io.IOException)20 HttpResponse (org.apache.http.HttpResponse)14 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)11 XPathException (org.exist.xquery.XPathException)11 FilterInputStream (java.io.FilterInputStream)7 SAXException (org.xml.sax.SAXException)7 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)6 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)6 Path (java.nio.file.Path)5 XmldbURI (org.exist.xmldb.XmldbURI)5 BinaryValue (org.exist.xquery.value.BinaryValue)5 HttpEntity (org.apache.http.HttpEntity)4 Request (org.apache.http.client.fluent.Request)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 Image (java.awt.Image)3 BufferedImage (java.awt.image.BufferedImage)3 XmlRpcClient (org.apache.xmlrpc.client.XmlRpcClient)3