use of org.exist.xquery.value.BinaryValue in project exist by eXist-db.
the class BinaryToStringTest method roundtrip.
@Test
public void roundtrip() throws XPathException {
final String value = "hello world";
final String encoding = "UTF-8";
TestableBinaryToString testable = new TestableBinaryToString(new MockXQueryContext(), null);
final BinaryValue binary = testable.stringToBinary(value, encoding);
StringValue result = testable.binaryToString(binary, encoding);
assertEquals(value, result.getStringValue());
}
use of org.exist.xquery.value.BinaryValue 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.exist.xquery.value.BinaryValue in project exist by eXist-db.
the class FilterInputStreamCacheMonitorTest method binaryResult.
@Test
public void binaryResult() throws XMLDBException {
final FilterInputStreamCacheMonitor monitor = FilterInputStreamCacheMonitor.getInstance();
// assert no binaries in use yet
int activeCount = monitor.getActive().size();
if (activeCount != 0) {
fail("FilterInputStreamCacheMonitor should have no active binaries, but found: " + activeCount + "." + System.getProperty("line.separator") + monitor.dump());
}
ResourceSet resourceSet = null;
try {
resourceSet = existXmldbEmbeddedServer.executeQuery("util:binary-doc('/db/" + TEST_COLLECTION_NAME + "/icon.png')");
assertEquals(1, resourceSet.getSize());
try (final EXistResource resource = (EXistResource) resourceSet.getResource(0)) {
assertTrue(resource instanceof LocalBinaryResource);
assertTrue(((ExtendedResource) resource).getExtendedContent() instanceof BinaryValue);
// one active binary (as it is in the result set)
assertEquals(1, monitor.getActive().size());
}
// assert no active binaries as we just closed the resource in the try-with-resources
activeCount = monitor.getActive().size();
if (activeCount != 0) {
fail("FilterInputStreamCacheMonitor should again have no active binaries, but found: " + activeCount + "." + System.getProperty("line.separator") + monitor.dump());
}
} finally {
resourceSet.clear();
}
}
use of org.exist.xquery.value.BinaryValue in project exist by eXist-db.
the class CropFunction method eval.
/**
* evaluate the call to the xquery crop() function,
* it is really the main entry point of this class
*
* @param args arguments from the crop() function call
* @param contextSequence the Context Sequence to operate on (not used here internally!)
* @return A sequence representing the result of the crop() function call
*
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
@Override
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 crop to
int x1 = 0;
int y1 = 0;
int x2 = MAXHEIGHT;
int y2 = MAXWIDTH;
int width = 0;
int height = 0;
if (!args[1].isEmpty()) {
x1 = ((IntegerValue) args[1].itemAt(0)).getInt();
if (args[1].hasMany()) {
y1 = ((IntegerValue) args[1].itemAt(1)).getInt();
x2 = ((IntegerValue) args[1].itemAt(2)).getInt();
y2 = ((IntegerValue) args[1].itemAt(3)).getInt();
width = x2 - x1;
height = y2 - y1;
}
}
if (width < 1) {
logger.error("cropping error: x2 value must be greater than x1");
return Sequence.EMPTY_SEQUENCE;
}
if (height < 1) {
logger.error("cropping error: y2 must be greater than y1");
return Sequence.EMPTY_SEQUENCE;
}
// get the mime-type
String mimeType = args[2].itemAt(0).getStringValue();
String formatName = mimeType.substring(mimeType.indexOf("/") + 1);
// TODO currently ONLY tested for JPEG!!!
BufferedImage bImage = null;
try (InputStream inputStream = ((BinaryValue) args[0].itemAt(0)).getInputStream()) {
// get the image data
Image image = ImageIO.read(inputStream);
if (image == null) {
logger.error("Unable to read image data!");
return Sequence.EMPTY_SEQUENCE;
}
// crop the image
Image cropImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(x1, y1, width, height)));
if (cropImage instanceof BufferedImage) {
// just in case cropImage is allready an BufferedImage
bImage = (BufferedImage) cropImage;
} else {
bImage = new BufferedImage(cropImage.getWidth(null), cropImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Paint the image onto the buffered image
Graphics2D g = bImage.createGraphics();
g.drawImage(cropImage, 0, 0, null);
g.dispose();
}
try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
ImageIO.write(bImage, formatName, os);
// return the new croped image data
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), os.toInputStream());
}
} catch (Exception e) {
throw new XPathException(this, e.getMessage());
}
}
use of org.exist.xquery.value.BinaryValue in project exist by eXist-db.
the class GZipFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
// is there some data to GZip?
if (args[0].isEmpty())
return Sequence.EMPTY_SEQUENCE;
BinaryValue bin = (BinaryValue) args[0].itemAt(0);
// gzip the data
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
final GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
bin.streamBinaryTo(gzos);
gzos.flush();
gzos.finish();
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
} catch (final IOException ioe) {
throw new XPathException(this, ioe.getMessage(), ioe);
}
}
Aggregations