Android File to Byte Array Read readBytes(File file)

Here you can find the source of readBytes(File file)

Description

read Bytes

Declaration

public static final byte[] readBytes(File file) throws IOException 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.Arrays;

public class Main {
    public static final byte[] readBytes(InputStream in) throws IOException {
        byte[] ret = new byte[0];
        byte[] buf = new byte[2048];
        int len;//from   w w w  .j  ava  2  s.  c om
        for (;;) {
            len = in.read(buf);
            if (len > 0) {
                ret = Arrays.copyOf(ret, ret.length + len);
                System.arraycopy(buf, 0, ret, ret.length - len, len);
            } else {
                break;
            }
        }
        return ret;
    }

    public static final byte[] readBytes(String path) throws IOException {
        return readBytes(new File(path));
    }

    public static final byte[] readBytes(File file) throws IOException {
        FileInputStream in = new FileInputStream(file.getAbsolutePath());
        try {
            return readBytes(in);
        } catch (IOException e) {
            throw e;
        } finally {
            in.close();
        }
    }
}

Related

  1. readFileToByteArray(File file)
  2. readFileToByteArray(String filePath)
  3. fileToBytes(String filePath)
  4. fileToByte(String filePath)
  5. getBytesFromFile(File file)
  6. readBytes(String path)
  7. readFromFile(String fileName, int offset, int len)
  8. read(File file)
  9. buildDataFromFile(File file)