From: <fg...@us...> - 2009-12-30 21:43:07
|
Revision: 1565 http://openutils.svn.sourceforge.net/openutils/?rev=1565&view=rev Author: fgiust Date: 2009-12-30 21:42:59 +0000 (Wed, 30 Dec 2009) Log Message: ----------- optimizations related to MEDIA-33 Modified Paths: -------------- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java Modified: trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java =================================================================== --- trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2009-12-30 21:41:27 UTC (rev 1564) +++ trunk/openutils-mgnlmedia/src/main/java/net/sourceforge/openutils/mgnlmedia/media/utils/ImageUtils.java 2009-12-30 21:42:59 UTC (rev 1565) @@ -380,45 +380,65 @@ public static void saveResolution(BufferedImage image, Content saveTo, String name, String extension, float quality, boolean forceProgressive) throws RepositoryException, IOException { - Content resolutions = saveTo.getChildByName("resolutions"); - if (resolutions == null) - { - resolutions = saveTo.createContent("resolutions", MediaConfigurationManager.RESOLUTIONS); - } - String resolution = name; - if (resolution == null) + InputStream stream = null; + + try { - resolution = "res-" + image.getWidth() + "x" + image.getHeight(); - } + stream = getStream(image, extension, quality, forceProgressive); - String originalRes = resolution; - resolution = getResolutionPath(resolution); + if (stream == null) + { + throw new IllegalArgumentException("Stream is null"); + } - synchronized (ExclusiveWrite.getInstance()) - { + Content resolutions = saveTo.getChildByName("resolutions"); + if (resolutions == null) + { + resolutions = saveTo.createContent("resolutions", MediaConfigurationManager.RESOLUTIONS); + } - if (resolutions.hasNodeData(resolution)) + String resolution = name; + if (resolution == null) { - NodeData nd = resolutions.getNodeData(resolution); - nd.delete(); + resolution = "res-" + image.getWidth() + "x" + image.getHeight(); } - NodeData nd = resolutions.createNodeData(resolution, PropertyType.BINARY); - nd.setValue(getStream(image, extension, quality, forceProgressive)); - String mimetype = "image/" + extension; - if ("jpg".equals(extension)) + String originalRes = resolution; + resolution = getResolutionPath(resolution); + + synchronized (ExclusiveWrite.getInstance()) { - mimetype = "image/jpeg"; + + if (resolutions.hasNodeData(resolution)) + { + NodeData nd = resolutions.getNodeData(resolution); + nd.delete(); + } + + NodeData nd = resolutions.createNodeData(resolution, PropertyType.BINARY); + + log.info("setting value to {}", nd.getHandle()); + nd.setValue(stream); + String mimetype = "image/" + extension; + if ("jpg".equals(extension)) + { + mimetype = "image/jpeg"; + } + nd.setAttribute(ImageUtils.RESOLUTION_PROPERTY, originalRes); + nd.setAttribute(FileProperties.PROPERTY_EXTENSION, extension); + nd.setAttribute(FileProperties.PROPERTY_FILENAME, "data"); + nd.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, mimetype); + nd.setAttribute(FileProperties.PROPERTY_LASTMODIFIED, "" + new Date().getTime()); + nd.setAttribute(FileProperties.PROPERTY_WIDTH, "" + image.getWidth()); + nd.setAttribute(FileProperties.PROPERTY_HEIGHT, "" + image.getHeight()); + } - nd.setAttribute(ImageUtils.RESOLUTION_PROPERTY, originalRes); - nd.setAttribute(FileProperties.PROPERTY_EXTENSION, extension); - nd.setAttribute(FileProperties.PROPERTY_FILENAME, "data"); - nd.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, mimetype); - nd.setAttribute(FileProperties.PROPERTY_LASTMODIFIED, "" + new Date().getTime()); - nd.setAttribute(FileProperties.PROPERTY_WIDTH, "" + image.getWidth()); - nd.setAttribute(FileProperties.PROPERTY_HEIGHT, "" + image.getHeight()); } + finally + { + IOUtils.closeQuietly(stream); + } } /** @@ -507,20 +527,24 @@ // go on } - // Deine destination type - used the ColorModel and SampleModel of the Input Image + // Define destination type - used the ColorModel and SampleModel of the Input Image params.setDestinationType(new ImageTypeSpecifier(image.getColorModel(), image.getSampleModel())); - // Set the output stream to Second Argument - // imageOutputStream = ImageIO.createImageOutputStream( new FileOutputStream(args[1]) ); imageOutputStream = ImageIO.createImageOutputStream(out); imageWriter.setOutput(imageOutputStream); - // Write the changed Image - imageWriter.write(null, new IIOImage(image, null, null), params); - - // Close the streams - imageOutputStream.close(); - imageWriter.dispose(); + try + { + // Write the changed Image + IIOImage iioimage = new IIOImage(image, null, null); + imageWriter.write(null, iioimage, params); + } + finally + { + // Close the streams + imageOutputStream.close(); + imageWriter.dispose(); + } } else { @@ -533,7 +557,14 @@ throw ex; } - return new ByteArrayInputStream(bos.toByteArray()); + byte[] byteArray = bos.toByteArray(); + + if (byteArray.length == 0) + { + return null; + } + + return new ByteArrayInputStream(byteArray); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |