Java File to Byte Array getBytes(File f)

Here you can find the source of getBytes(File f)

Description

get Bytes

License

Open Source License

Declaration

static byte[] getBytes(File f) throws IOException 

Method Source Code

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.io.InputStream;

import java.io.IOException;

public class Main {
    static byte[] getBytes(InputStream is) throws IOException {
        int available = is.available();
        byte[] tmpBuf = new byte[available];
        int size = 0;
        int currentPointer = 0;
        byte[] result = null;
        while ((size = is.read(tmpBuf)) != -1) {
            result = updateBuffer(result, tmpBuf, currentPointer, size);
            currentPointer += size;/*from  www. ja  v a  2  s  .c o m*/
        }
        return result;
    }

    static byte[] getBytes(File f) throws IOException {
        FileInputStream fis = new FileInputStream(f.getAbsolutePath());
        byte[] result = getBytes(fis);
        fis.close();
        return result;
    }

    static byte[] updateBuffer(byte[] buf1, byte[] buf2, int currentPointer, int size) {
        if (size <= 0) {
            return buf2;
        }
        if (buf1 == null || buf1.length < currentPointer + size) {
            buf1 = new byte[currentPointer + size];
        }

        System.arraycopy(buf2, 0, buf1, currentPointer, size);
        return buf1;
    }
}

Related

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