Java File to Byte Array fileToBytes(final File f)

Here you can find the source of fileToBytes(final File f)

Description

file To Bytes

License

Apache License

Declaration

public static byte[] fileToBytes(final File f) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w w w.j  a  v  a  2s .  c o m*/
 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.16/src/java/org/apache/commons/ssl/Util.java $
 * $Revision: 180 $
 * $Date: 2014-09-23 11:33:47 -0700 (Tue, 23 Sep 2014) $
 *
 * ====================================================================
 * 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static final int SIZE_KEY = 0;
    public static final int LAST_READ_KEY = 1;

    public static byte[] fileToBytes(final File f) throws IOException {
        return streamToBytes(new FileInputStream(f));
    }

    public static byte[] streamToBytes(final ByteArrayInputStream in, int maxLength) {
        byte[] buf = new byte[maxLength];
        int[] status = fill(buf, 0, in);
        int size = status[SIZE_KEY];
        if (buf.length != size) {
            byte[] smallerBuf = new byte[size];
            System.arraycopy(buf, 0, smallerBuf, 0, size);
            buf = smallerBuf;
        }
        return buf;
    }

    public static byte[] streamToBytes(final InputStream in, int maxLength) throws IOException {
        byte[] buf = new byte[maxLength];
        int[] status = fill(buf, 0, in);
        int size = status[SIZE_KEY];
        if (buf.length != size) {
            byte[] smallerBuf = new byte[size];
            System.arraycopy(buf, 0, smallerBuf, 0, size);
            buf = smallerBuf;
        }
        return buf;
    }

    public static byte[] streamToBytes(final InputStream in) throws IOException {
        byte[] buf = new byte[4096];
        try {
            int[] status = fill(buf, 0, in);
            int size = status[SIZE_KEY];
            int lastRead = status[LAST_READ_KEY];
            while (lastRead != -1) {
                buf = resizeArray(buf);
                status = fill(buf, size, in);
                size = status[SIZE_KEY];
                lastRead = status[LAST_READ_KEY];
            }
            if (buf.length != size) {
                byte[] smallerBuf = new byte[size];
                System.arraycopy(buf, 0, smallerBuf, 0, size);
                buf = smallerBuf;
            }
        } finally {
            in.close();
        }
        return buf;
    }

    public static byte[] streamToBytes(final ByteArrayInputStream in) {
        byte[] buf = new byte[4096];
        int[] status = fill(buf, 0, in);
        int size = status[SIZE_KEY];
        int lastRead = status[LAST_READ_KEY];
        while (lastRead != -1) {
            buf = resizeArray(buf);
            status = fill(buf, size, in);
            size = status[SIZE_KEY];
            lastRead = status[LAST_READ_KEY];
        }
        if (buf.length != size) {
            byte[] smallerBuf = new byte[size];
            System.arraycopy(buf, 0, smallerBuf, 0, size);
            buf = smallerBuf;
        }
        // in.close();  <-- this is a no-op on ByteArrayInputStream.
        return buf;
    }

    public static int[] fill(final byte[] buf, final int offset, final InputStream in) throws IOException {
        int read = in.read(buf, offset, buf.length - offset);
        int lastRead = read;
        if (read == -1) {
            read = 0;
        }
        while (lastRead != -1 && read + offset < buf.length) {
            lastRead = in.read(buf, offset + read, buf.length - read - offset);
            if (lastRead != -1) {
                read += lastRead;
            }
        }
        return new int[] { offset + read, lastRead };
    }

    public static int[] fill(final byte[] buf, final int offset, final ByteArrayInputStream in) {
        int read = in.read(buf, offset, buf.length - offset);
        int lastRead = read;
        if (read == -1) {
            read = 0;
        }
        while (lastRead != -1 && read + offset < buf.length) {
            lastRead = in.read(buf, offset + read, buf.length - read - offset);
            if (lastRead != -1) {
                read += lastRead;
            }
        }
        return new int[] { offset + read, lastRead };
    }

    public static byte[] resizeArray(final byte[] bytes) {
        byte[] biggerBytes = new byte[bytes.length * 2];
        System.arraycopy(bytes, 0, biggerBytes, 0, bytes.length);
        return biggerBytes;
    }
}

Related

  1. fileToByteArray(String path)
  2. fileToBytes(File file)
  3. fileToBytes(File file)
  4. fileToBytes(File path)
  5. fileToBytes(File source)
  6. getBytes(File aFile)
  7. getBytes(File archiveFile)
  8. getBytes(File contentFile)
  9. getBytes(File f)