Java FileInputStream Read loadData(File f)

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

Description

load Data

License

Apache License

Declaration

public static byte[] loadData(File f) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;

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

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

public class Main {
    public static byte[] loadData(InputStream is) throws IOException {
        BufferedInputStream bis = null;
        byte[] data = null;
        byte[] tmp = new byte[1024];
        int num = 0;
        bis = new BufferedInputStream(is);
        while ((num = bis.read(tmp)) > 0) {
            if (data == null) {
                data = new byte[num];
                System.arraycopy(tmp, 0, data, 0, num);
            } else {
                byte[] old = data;
                data = new byte[old.length + num];
                System.arraycopy(old, 0, data, 0, old.length);
                System.arraycopy(tmp, 0, data, old.length, num);
            }/*from ww  w . java  2s  .  c  o  m*/
        }
        return data;
    }

    public static byte[] loadData(File f) throws Exception {
        byte[] data = null;
        InputStream br = new FileInputStream(f);
        byte[] tmp = new byte[8192];
        int num = 0;
        int len = 0;
        try {
            while ((num = br.read(tmp)) > 0) {
                if (data == null) {
                    data = new byte[num];
                    System.arraycopy(tmp, 0, data, 0, num);
                } else {
                    byte[] old = data;
                    data = new byte[old.length + num];
                    System.arraycopy(old, 0, data, 0, old.length);
                    System.arraycopy(tmp, 0, data, old.length, num);
                }
                len += num;
            }
        } finally {
            br.close();
        }
        return data;
    }
}

Related

  1. load(File file)
  2. load(File file)
  3. load(File file)
  4. load(String file)
  5. loadContents(File file)
  6. loadFolderFromJar(String path)
  7. loadRes(String res)
  8. loadStackableItems(String fileName)
  9. loadTxtFile(String fileName)