diff -r b329573c76d7 -r 8e9983260624 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 01:17:02 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 17:02:48 2014 +0100 @@ -18,12 +18,20 @@ package cz.frantovo.copyImageResizer; import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat; +import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.WritableByteChannel; import java.util.logging.Level; import java.util.logging.Logger; +import javax.imageio.ImageIO; /** * @@ -39,24 +47,71 @@ resizeDirectory(options.getInput(), options); } - private void resizeFile(File file, RecursiveOptions options) throws ResizeException { - log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file)); + private void resizeFile(File inputFile, RecursiveOptions options) throws ResizeException { + File inputFileRelative = relativize(options.getInput(), inputFile); + log.log(Level.FINE, "Resizing file: {0}", inputFileRelative); - ImageFormat format = ImageFormat.getMatching(file.getName()); + ImageFormat format = ImageFormat.getMatching(inputFile.getName()); if (format == null) { - log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file)); + log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", inputFileRelative); } else { try { - FileInputStream input = new FileInputStream(file); - File relative = relativize(options.getInput(), file); - File outputFile = new File(options.getOutput(), relative.getPath()); - FileOutputStream output = new FileOutputStream(outputFile); - resizer.resize(input, output, format); + for (SizeSpecification size : options.getSizes()) { + File sizeRoot = new File(options.getOutput(), size.getDirectory()); + File outputFile = new File(sizeRoot, inputFileRelative.getPath()); + try (FileInputStream input = new FileInputStream(inputFile)) { + BufferedImage image = readImage(input); + if (shouldResize(image, size)) { + try (FileOutputStream output = new FileOutputStream(outputFile)) { + resizer.resize(image, output, size, format); + } + } else { + log.log(Level.INFO, "File: {0} has already required size → just copy", inputFileRelative); + justCopy(inputFile, outputFile); + } + } + } } catch (FileNotFoundException e) { throw new ResizeException("Error while opening stream", e); + } catch (IOException e) { + throw new ResizeException("Error while closing stream", e); } } + } + + private static boolean shouldResize(BufferedImage input, SizeSpecification requested) { + if (requested.isResizeSmaller()) { + return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth(); + } else { + return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth(); + } + } + + private static BufferedImage readImage(InputStream input) throws ResizeException { + try { + return ImageIO.read(input); + } catch (IOException e) { + throw new ResizeException("Unable to read image from stream", e); + } + } + + private static void justCopy(File inputFile, File outputFile) throws ResizeException { + try { + + if (!outputFile.exists()) { + outputFile.createNewFile(); + } + + try (FileChannel input = new FileInputStream(inputFile).getChannel()) { + try (FileChannel output = new FileOutputStream(outputFile).getChannel()) { + output.transferFrom(input, 0, input.size()); + } + } + + } catch (IOException e) { + throw new ResizeException("Unable copy stream/channel", e); + } } @@ -64,9 +119,12 @@ log.log(Level.FINE, "Resizing directory: {0}", directory); - File relative = relativize(options.getInput(), directory); - File output = new File(options.getOutput(), relative.getPath()); - output.mkdirs(); + for (SizeSpecification size : options.getSizes()) { + File relative = relativize(options.getInput(), directory); + File sizeRoot = new File(options.getOutput(), size.getDirectory()); + File dir = new File(sizeRoot, relative.getPath()); + dir.mkdirs(); + } for (File entry : directory.listFiles()) { if (entry.isDirectory()) {