Java ClassLoader Load getBytes(File file)

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

Description

Read a file completely and return a byte array containing the data.

License

Open Source License

Parameter

Parameter Description
file the file to read.

Return

the bytes of the file, or an empty array if an error occurred.

Declaration

public static byte[] getBytes(File file) 

Method Source Code


//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;
import java.net.URL;

import java.util.jar.JarFile;

import java.util.zip.ZipFile;

public class Main {
    /**//from   w w  w. j ava  2s.c  om
     * Read a file completely and return a byte array containing the data.
     * @param file the file to read.
     * @return the bytes of the file, or an empty array if an error occurred.
     */
    public static byte[] getBytes(File file) {
        return getBytes(file, -1);
    }

    /**
     * Read the first bytes of a file and return a byte array containing the data.
     * @param file the file to read.
     * @param length the number of bytes to read, or -1 if the entire file is to be read.
     * @return the bytes, or an empty array if an error occurred.
     */
    public static byte[] getBytes(File file, int length) {
        if (!file.exists())
            return new byte[0];
        int fileLength = (int) file.length();
        if (length == -1)
            length = fileLength;
        if (fileLength < length)
            length = fileLength;
        byte[] bytes = new byte[length];
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            fis.read(bytes, 0, bytes.length);
            close(fis);
            return bytes;
        } catch (Exception e) {
            close(fis);
            return new byte[0];
        }
    }

    /**
     * Read an InputStream completely.
     * @param stream the InputStream to read.
     * @return the bytes, or an empty byte array if an error occurred.
     */
    public static byte[] getBytes(InputStream stream) {
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(stream);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int n;
            byte[] buf = new byte[1024];
            while ((n = bis.read(buf, 0, buf.length)) != -1)
                baos.write(buf, 0, n);
            close(bis);
            return baos.toByteArray();
        } catch (Exception e) {
            close(bis);
            return new byte[0];
        }
    }

    /**
     * Read the first bytes of an InputStream and leave the stream open.
     * @param stream the InputStream to read.
     * @return the bytes, or an empty byte array if an error occurred.
     * Note: the returned byte array may be shorter than the requested
     * length if the input stream ends before the length has been reached.
     */
    public static byte[] getBytes(InputStream stream, int length) {
        BufferedInputStream bis = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int n = 0;
            int bufSize = 1024;
            byte[] buf = new byte[bufSize];
            int remaining = length;
            while ((remaining > 0) && (n != -1)) {
                int toRead = Math.min(bufSize, remaining);
                n = stream.read(buf, 0, remaining);
                if (n != -1) {
                    baos.write(buf, 0, n);
                    remaining -= n;
                }
            }
            return baos.toByteArray();
        } catch (Exception e) {
            return new byte[0];
        }
    }

    /**
     * Read a resource completely and return a byte array containing the data.
     * This method uses the context classloader to find the resource.
     * @param url the resource to read.
     * @return the bytes of the resource, or an empty array if an error occurred.
     */
    public static byte[] getBytes(URL url) {
        InputStream is = null;
        try {
            is = url.openStream();
            return getBytes(is);
        } catch (Exception e) {
            close(is);
            return new byte[0];
        }
    }

    /**
     * Close an InputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(InputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close an OutputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(OutputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Writer and ignore Exceptions.
     * @param writer the writer to close.
     */
    public static void close(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Reader and ignore Exceptions.
     * @param reader the reader to close.
     */
    public static void close(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a JarFile and ignore Exceptions.
     * @param jarFile the JarFile to close.
     */
    public static void close(JarFile jarFile) {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a ZipFile and ignore Exceptions.
     * @param zipFile the zipFile to close.
     */
    public static void close(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (Exception ignore) {
            }
        }
    }
}

Related

  1. getAbsolutePath(String file)
  2. getAbsolutePathEssenceDesc(String file)
  3. getAllArquillianLibs()
  4. getAsProperties(final String name)
  5. getBaseDir()
  6. getConfigFileInputStream(String configFilePath)
  7. getContentFromFile(String fileName, Object context)
  8. getDirectories(String path)
  9. getFile(final String filename)