/*
 * ################################################################
 *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds 
 *
 * Copyright (C) 1997-2010 INRIA/University of 
 * 				Nice-Sophia Antipolis/ActiveEon
 * Contact: proactive@ow2.org or contact@activeeon.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 
 * or a different license than the GPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s): ActiveEon Team - http://www.activeeon.com
 *
 * ################################################################
 * $$ACTIVEEON_CONTRIBUTOR$$
 */
package org.ow2.proactive.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.objectweb.proactive.annotation.PublicAPI;


/**
 * Utility functions for converting files contents to a byte array,
 * and vis versa.
 *
 * @author The ProActive Team
 *
 */
@PublicAPI
public class FileToBytesConverter {

    /** Read contents of a file and return it as a byte array
     * @param file the file to read
     * @return an array of bytes containing file's data.
     * @throws IOException
     */
    public static byte[] convertFileToByteArray(File file) throws IOException {
        InputStream in = null;
        in = new FileInputStream(file);
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        long count = 0;
        int n = 0;
        while (-1 != (n = in.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        in.close();
        return output.toByteArray();
    }

    /** write contents of a byte array in a file
     * @param array Array of bytes to write
     * @param file object in which bytes will be written
     * @throws IOException
     */
    public static void convertByteArrayToFile(byte[] array, File file) throws IOException {
        FileOutputStream outStream = new FileOutputStream(file);
        outStream.write(array);
        outStream.close();
    }
}

@@ -1624,36 +1624,8 @@
 ion;
-%0Aimport java.io.InputStream;
 %0A%0Aim
@@ -2133,16 +2133,20 @@
         
+File
 InputStr
@@ -2153,58 +2153,1851 @@
 eam 
-in = null;%0A        in = new FileInputStream(file);
+fis = new FileInputStream(file);%0A        try %7B%0A            long length = file.length();%0A            if (length %3E 0) %7B%0A                return fastConversion(fis, length);%0A            %7D else %7B%0A                return slowConversion(fis);%0A            %7D%0A        %7D finally %7B%0A            try %7B%0A                fis.close();%0A            %7D catch (IOException e) %7B%0A                // We want to throw the exception thrown by the outer try block%0A                // not by the close()%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * Read content of a file of known length and return it as a byte array%0A     *  %0A     * @param fis The stream to read%0A     * @param length The number of byte to read%0A     * @return an array of bytes containing file's data%0A     * @throws IOException If an I/O error occurs, the file is too large or less bytes than length can be read%0A     */%0A    private static byte%5B%5D fastConversion(final FileInputStream fis, long length) throws IOException %7B%0A        if (length %3E Integer.MAX_VALUE) %7B%0A            throw new IOException(%22File too large to fit in a byte array%22);%0A        %7D%0A%0A        final byte%5B%5D buf = new byte%5B(int) length%5D;%0A        int offset = 0;%0A        while (offset %3C buf.length) %7B%0A            int r = fis.read(buf, offset, buf.length - offset);%0A            if (r %3E= 0) %7B%0A                offset += r;%0A            %7D else %7B%0A                throw new IOException(%22EOF encountered but fewer bits than expected has been read%22);%0A            %7D%0A        %7D%0A%0A        return buf;%0A    %7D%0A%0A    /**%0A     * Read content of a file of unknown length (or empty file) and return it as a byte array%0A     *  %0A     * @param fis the stream to read%0A     * @return an array of bytes containing file's data%0A     * @throws IOException If an I/O error occurs%0A     */%0A    private static byte%5B%5D slowConversion(final FileInputStream fis) throws IOException %7B
 %0A   
@@ -4171,18 +4171,19 @@
 != (n = 
-in
+fis
 .read(bu
@@ -4256,16 +4256,16 @@
 t += n;%0A
+
         
@@ -4270,28 +4270,8 @@
   %7D%0A
-        in.close();%0A
     

Add a "fast" file to byte array conversion