Java FileInputStream Read load(File file)

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

Description

load

License

Open Source License

Declaration

public static byte[] load(File file) throws IOException 

Method Source Code

//package com.java2s;

import java.io.BufferedInputStream;

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

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

import java.util.LinkedList;
import java.util.List;

public class Main {
    public static byte[] load(File file) throws IOException {
        InputStream in = new FileInputStream(file);
        try {//from  w  w w. j  ava  2s.co m
            in = new BufferedInputStream(in);
            return toByteArray(in, -1);
        } finally {
            in.close();
        }
    }

    public static byte[] toByteArray(String hexString) {
        byte[] bytes = new byte[hexString.length() / 2];
        for (int i = 0; i < hexString.length(); i += 2) {
            String hex = hexString.substring(i, i + 2);
            bytes[i / 2] = (byte) Integer.parseInt(hex, 16);
        }
        return bytes;
    }

    /**
     * Converts (a portion) of the given stream to a byte array.
     * 
     * @param inputStream
     *            the stream from which to read bytes
     * @param size
     *            the number of bytes to be read, or -1 if the whole of the
     *            (remaining) bytes should be read
     * @return a byte array of length <tt>size</tt> containing bytes read from
     *         the given stream, starting at the current position
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream inputStream, final int size) throws IOException {
        int totalSize = 0;
        List<byte[]> bufferList = null;
        byte[] result = null;
        if (size == -1) {
            bufferList = new LinkedList<byte[]>();
        } else {
            result = new byte[size];
        }
        int avail = inputStream.available();
        while (avail > 0) {
            byte[] buffer = new byte[1024];
            int len;
            if (size == -1) {
                len = 1024;
            } else {
                len = Math.min(1024, size - totalSize);
            }
            int r = inputStream.read(buffer, 0, len);
            if (r == -1) {
                break;
            }
            if (size == -1) {
                if (r < 1024) {
                    byte[] smallerBuffer = new byte[r];
                    System.arraycopy(buffer, 0, smallerBuffer, 0, r);
                    bufferList.add(smallerBuffer);
                } else {
                    bufferList.add(buffer);
                }
            } else {
                System.arraycopy(buffer, 0, result, totalSize, r);
            }
            totalSize += r;
            if (size != -1 && totalSize == size) {
                break;
            }
        }
        if (size == -1) {
            result = new byte[totalSize];
            int offset = 0;
            for (byte[] buffer : bufferList) {
                System.arraycopy(buffer, 0, result, offset, buffer.length);
                offset += buffer.length;
            }
        }
        return result;
    }

    public static String substring(String string, int pos, int length) {
        if (string.length() - pos <= length) {
            return string.substring(pos);
        }
        return string.substring(pos, pos + length);
    }
}

Related

  1. load(File file)
  2. load(File file)
  3. load(String file)
  4. loadContents(File file)
  5. loadData(File f)
  6. loadFolderFromJar(String path)