Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id F0D8A109A3 for ; Thu, 24 Oct 2013 00:09:30 +0000 (UTC) Received: (qmail 80675 invoked by uid 500); 24 Oct 2013 00:09:30 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 80613 invoked by uid 500); 24 Oct 2013 00:09:30 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 80605 invoked by uid 99); 24 Oct 2013 00:09:30 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Oct 2013 00:09:30 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Oct 2013 00:09:27 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id BBA0023889E0; Thu, 24 Oct 2013 00:09:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1535219 - /commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java Date: Thu, 24 Oct 2013 00:09:05 -0000 To: commits@commons.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131024000905.BBA0023889E0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ggregory Date: Thu Oct 24 00:09:05 2013 New Revision: 1535219 URL: http://svn.apache.org/r1535219 Log: Sort members in AB order. Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java?rev=1535219&r1=1535218&r2=1535219&view=diff ============================================================================== --- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java (original) +++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/util/IoUtils.java Thu Oct 24 00:09:05 2013 @@ -31,42 +31,49 @@ import java.nio.channels.FileChannel; import org.apache.commons.imaging.ImagingConstants; public class IoUtils implements ImagingConstants { - /** - * This class should never be instantiated. - */ - private IoUtils() { - } - - /** - * Reads an InputStream to the end. - *

- * - * @param is - * The InputStream to read. - * @return A byte array containing the contents of the InputStream - * @see InputStream - */ - public static byte[] getInputStreamBytes(InputStream is) throws IOException { - ByteArrayOutputStream os = null; - + public static final boolean copyFileNio(final File src, final File dst) + throws IOException { + FileChannel srcChannel = null, dstChannel = null; try { - os = new ByteArrayOutputStream(4096); + // Create channel on the source + srcChannel = new FileInputStream(src).getChannel(); - is = new BufferedInputStream(is); + // Create channel on the destination + dstChannel = new FileOutputStream(dst).getChannel(); - int count; - final byte[] buffer = new byte[4096]; - while ((count = is.read(buffer, 0, 4096)) > 0) { - os.write(buffer, 0, count); + // // Copy file contents from source to destination + // dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); + + { + // long theoretical_max = (64 * 1024 * 1024) - (32 * 1024); + final int safe_max = (64 * 1024 * 1024) / 4; + final long size = srcChannel.size(); + long position = 0; + while (position < size) { + position += srcChannel.transferTo(position, safe_max, + dstChannel); + } } - os.flush(); + // Close the channels + srcChannel.close(); + srcChannel = null; + dstChannel.close(); + dstChannel = null; - return os.toByteArray(); + return true; } finally { try { - if (os != null) { - os.close(); + if (srcChannel != null) { + srcChannel.close(); + } + } catch (final IOException e) { + Debug.debug(e); + + } + try { + if (dstChannel != null) { + dstChannel.close(); } } catch (final IOException e) { Debug.debug(e); @@ -74,6 +81,47 @@ public class IoUtils implements ImagingC } } + public static void copyStreamToStream(final InputStream src, final OutputStream dst) + throws IOException { + copyStreamToStream(src, dst, true); + } + + public static void copyStreamToStream(final InputStream src, final OutputStream dst, + final boolean close_streams) throws IOException { + BufferedInputStream bis = null; + BufferedOutputStream bos = null; + + try { + bis = new BufferedInputStream(src); + bos = new BufferedOutputStream(dst); + + int count; + final byte[] buffer = new byte[4096]; + while ((count = bis.read(buffer, 0, buffer.length)) > 0) { + dst.write(buffer, 0, count); + } + bos.flush(); + } finally { + if (close_streams) { + try { + if (bis != null) { + bis.close(); + } + } catch (final IOException e) { + Debug.debug(e); + } + try { + if (bos != null) { + bos.close(); + } + } catch (final IOException e) { + Debug.debug(e); + } + } + } + + } + /** * Reads a File into memory. *

@@ -101,21 +149,39 @@ public class IoUtils implements ImagingC } } - public static void writeToFile(final byte[] src, final File file) throws IOException { - ByteArrayInputStream stream = null; + /** + * Reads an InputStream to the end. + *

+ * + * @param is + * The InputStream to read. + * @return A byte array containing the contents of the InputStream + * @see InputStream + */ + public static byte[] getInputStreamBytes(InputStream is) throws IOException { + ByteArrayOutputStream os = null; try { - stream = new ByteArrayInputStream(src); + os = new ByteArrayOutputStream(4096); - putInputStreamToFile(stream, file); + is = new BufferedInputStream(is); + + int count; + final byte[] buffer = new byte[4096]; + while ((count = is.read(buffer, 0, 4096)) > 0) { + os.write(buffer, 0, count); + } + + os.flush(); + + return os.toByteArray(); } finally { try { - if (stream != null) { - stream.close(); + if (os != null) { + os.close(); } - } catch (final Exception e) { + } catch (final IOException e) { Debug.debug(e); - } } } @@ -144,95 +210,29 @@ public class IoUtils implements ImagingC } } - public static void copyStreamToStream(final InputStream src, final OutputStream dst) - throws IOException { - copyStreamToStream(src, dst, true); - } - - public static void copyStreamToStream(final InputStream src, final OutputStream dst, - final boolean close_streams) throws IOException { - BufferedInputStream bis = null; - BufferedOutputStream bos = null; - - try { - bis = new BufferedInputStream(src); - bos = new BufferedOutputStream(dst); - - int count; - final byte[] buffer = new byte[4096]; - while ((count = bis.read(buffer, 0, buffer.length)) > 0) { - dst.write(buffer, 0, count); - } - bos.flush(); - } finally { - if (close_streams) { - try { - if (bis != null) { - bis.close(); - } - } catch (final IOException e) { - Debug.debug(e); - } - try { - if (bos != null) { - bos.close(); - } - } catch (final IOException e) { - Debug.debug(e); - } - } - } - - } + public static void writeToFile(final byte[] src, final File file) throws IOException { + ByteArrayInputStream stream = null; - public static final boolean copyFileNio(final File src, final File dst) - throws IOException { - FileChannel srcChannel = null, dstChannel = null; try { - // Create channel on the source - srcChannel = new FileInputStream(src).getChannel(); - - // Create channel on the destination - dstChannel = new FileOutputStream(dst).getChannel(); - - // // Copy file contents from source to destination - // dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); - - { - // long theoretical_max = (64 * 1024 * 1024) - (32 * 1024); - final int safe_max = (64 * 1024 * 1024) / 4; - final long size = srcChannel.size(); - long position = 0; - while (position < size) { - position += srcChannel.transferTo(position, safe_max, - dstChannel); - } - } - - // Close the channels - srcChannel.close(); - srcChannel = null; - dstChannel.close(); - dstChannel = null; + stream = new ByteArrayInputStream(src); - return true; + putInputStreamToFile(stream, file); } finally { try { - if (srcChannel != null) { - srcChannel.close(); + if (stream != null) { + stream.close(); } - } catch (final IOException e) { + } catch (final Exception e) { Debug.debug(e); } - try { - if (dstChannel != null) { - dstChannel.close(); - } - } catch (final IOException e) { - Debug.debug(e); - } } } + /** + * This class should never be instantiated. + */ + private IoUtils() { + } + }