/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.pdfbox.io;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

/**
 * This class contains various I/O-related methods.
 */
public final class IOUtils
{

    //TODO PDFBox should really use Apache Commons IO.

    private IOUtils()
    {
        //Utility class. Don't instantiate.
    }

    /**
     * Reads the input stream and returns its contents as a byte array.
     * @param in the input stream to read from.
     * @return the byte array
     * @throws IOException if an I/O error occurs
     */
    public static byte[] toByteArray(InputStream in) throws IOException
    {
        ByteArrayOutputStream baout = new ByteArrayOutputStream();
        copy(in, baout);
        return baout.toByteArray();
    }

    /**
     * Copies all the contents from the given input stream to the given output stream.
     * @param input the input stream
     * @param output the output stream
     * @return the number of bytes that have been copied
     * @throws IOException if an I/O error occurs
     */
    public static long copy(InputStream input, OutputStream output) throws IOException
    {
        byte[] buffer = new byte[4096];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer)))
        {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

    /**
     * Populates the given buffer with data read from the input stream. If the data doesn't
     * fit the buffer, only the data that fits in the buffer is read. If the data is less than
     * fits in the buffer, the buffer is not completely filled.
     * @param in the input stream to read from
     * @param buffer the buffer to fill
     * @return the number of bytes written to the buffer
     * @throws IOException if an I/O error occurs
     */
    public static long populateBuffer(InputStream in, byte[] buffer) throws IOException
    {
        int remaining = buffer.length;
        while (remaining > 0)
        {
            int bufferWritePos = buffer.length - remaining;
            int bytesRead = in.read(buffer, bufferWritePos, remaining);
            if (bytesRead < 0)
            {
                break; //EOD
            }
            remaining -= bytesRead;
        }
        return buffer.length - remaining;
    }


    /**
     * Unconditionally close an <code>InputStream</code>.
     * <p>
     * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
     * This is typically used in finally blocks.
     *
     * @param input  the InputStream to close, may be null or already closed
     */
    public static void closeQuietly(InputStream input) {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }
    
    /**
     * Unconditionally close an <code>Reader</code>.
     * <p>
     * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
     * This is typically used in finally blocks.
     *
     * @param input  the Reader to close, may be null or already closed
     */
    public static void closeQuietly(Reader input) {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

    /**
     * Unconditionally close a <code>Writer</code>.
     * <p>
     * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
     * This is typically used in finally blocks.
     *
     * @param output  the Writer to close, may be null or already closed
     */
    public static void closeQuietly(Writer output) {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

    /**
     * Unconditionally close an <code>OutputStream</code>.
     * <p>
     * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
     * This is typically used in finally blocks.
     *
     * @param output  the OutputStream to close, may be null or already closed
     */
    public static void closeQuietly(OutputStream output) {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }


    
    
}

@@ -870,32 +870,58 @@
 ayOutputStream;%0A
+import java.io.Closeable;%0A
 import java.io.I
@@ -992,54 +992,8 @@
 eam;
-%0Aimport java.io.Reader;%0Aimport java.io.Writer;
 %0A%0A/*
@@ -3211,25 +3211,24 @@
 ing;%0A    %7D%0A%0A
-%0A
     /**%0A    
@@ -3234,1398 +3234,222 @@
   * 
-Unconditionally close an %3Ccode%3EInputStream%3C/code%3E.%0A     * %3Cp%3E%0A     * Equivalent to %7B@link InputStream#close()%7D, except any exceptions will be ignored.%0A     * This is typically used in finally blocks.%0A     *%0A     * @param input  the InputStream to close, may be null or already closed%0A     */%0A    public static void closeQuietly(InputStream input) %7B%0A        try %7B%0A            if (input != null) %7B%0A                input.close();%0A            %7D%0A        %7D catch (IOException ioe) %7B%0A            // ignore%0A        %7D%0A    %7D%0A    %0A    /**%0A     * Unconditionally close an %3Ccode%3EReader%3C/code%3E.%0A     * %3Cp%3E%0A     * Equivalent to %7B@link Reader#close()%7D, except any exceptions will be ignored.%0A     * This is typically used in finally blocks.%0A     *%0A     * @param input  the Reader to close, may be null or already closed%0A     */%0A    public static void closeQuietly(Reader input) %7B%0A        try %7B%0A            if (input != null) %7B%0A                input.close();%0A            %7D%0A        %7D catch (IOException ioe) %7B%0A            // ignore%0A        %7D%0A    %7D%0A%0A    /**%0A     * Unconditionally close a %3Ccode%3EWriter%3C/code%3E.%0A     * %3Cp%3E%0A     * Equivalent to %7B@link Writer#close()%7D, except any exceptions will be ignored.%0A     * This is typically used in finally blocks.
+Null safe close of the given %7B@link Closeable%7D suppressing any exception.%0A     *%0A     * @param closeable to be closed%0A     */%0A    public static void closeQuietly(Closeable closeable)
 %0A    
- *
+%7B
 %0A     
-* @param output  the Writer to close, may be null or already closed%0A     */%0A    public static void closeQuietly(Writer output) %7B%0A        try %7B%0A
+   try%0A        %7B%0A   
     
@@ -3457,608 +3457,94 @@
     
-   
 if (
-output != null) %7B%0A                output.close();%0A            %7D%0A        %7D catch (IOException ioe) %7B%0A            // ignore%0A        %7D%0A    %7D%0A%0A    /**%0A     * Unconditionally close an %3Ccode%3EOutputStream%3C/code%3E.%0A     * %3Cp%3E%0A     * Equivalent to %7B@link OutputStream#close()%7D, except any exceptions will be ignored.%0A     * This is typically used in finally blocks.
+closeable != null)%0A            %7B
 %0A     
-*%0A
      
-* @param output  the OutputStream to close, may be null or already closed%0A     */%0A    public static void closeQuietly(OutputStream output) %7B%0A        try %7B%0A            if (output != null) %7B%0A                output.close();%0A    
+      closeable.close();%0A            %7D%0A
     
@@ -3556,18 +3556,16 @@
 %0A       
- %7D
  catch (
@@ -3580,16 +3580,24 @@
 ion ioe)
+%0A       
  %7B%0A     
@@ -3633,18 +3633,6 @@
   %7D%0A
-%0A%0A    %0A    %0A
 %7D%0A

replace four closeQuietly methods with one, as suggested by Andrea Vacondio