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());
}
}
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);
}
}
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());
}
}
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());
}
}
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()));
}
}
Aggregations