Java File to InputStream openInputStream(File file, boolean buffer)

Here you can find the source of openInputStream(File file, boolean buffer)

Description

Opens an InputStream on a file for reading.

License

Open Source License

Parameter

Parameter Description
file the file to open a stream on
buffer if true, wrap the stream in a buffered stream

Exception

Parameter Description
NullPointerException if file is null

Return

an InpustStream on the file, or null if an exception was thrown

Declaration

public static InputStream openInputStream(File file, boolean buffer) 

Method Source Code


//package com.java2s;
/*//ww w  . j a v  a 2 s .  com
 * $Id$
 *
 * Copyright (C) 2003-2015 JNode.org
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.BufferedInputStream;

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

import java.io.InputStream;

public class Main {
    private static final int BUFFER_SIZE = 4096;
    private static final String ex_null_param = "A required paramater is null";

    /**
     * Opens an InputStream on a file for reading.
     *
     * This method will not throw a FileNotFoundException or SecurityException like
     * the FileInputStream constructor would.
     *
     * @param file the file to open a stream on
     * @return an InputStream on the file, or null if an exception was thrown
     * @throws NullPointerException if file is null
     */
    public static InputStream openInputStream(File file) {
        return openInputStream(file, false);
    }

    /**
     * Opens an InputStream on a file for reading.
     *
     * This method will not throw a FileNotFoundException or a SecurityException like
     * the FileInputStream constructor would.
     *
     * @param file the file to open a stream on
     * @param buffer if true, wrap the stream in a buffered stream
     * @return an InpustStream on the file, or null if an exception was thrown
     * @throws NullPointerException if file is null
     */
    public static InputStream openInputStream(File file, boolean buffer) {
        return openInputStream(file, buffer, BUFFER_SIZE);
    }

    /**
     * Opens an InputStream on a file for reading.
     * 
     * This method will not throw a FileNotFoundException or a SecurityException like
     * the FileInputStream constructor would.
     *
     * @param file the file to open a stream on
     * @param buffer if true, wrap the stream in a buffered stream
     * @param bufferSize the buffer size to use if buffer is true
     * @return an InpustStream on the file, or null if an exception was thrown
     * @throws NullPointerException if file is null
     * @throws IllegalArgumentException if bufferSize < 0
     */
    public static InputStream openInputStream(File file, boolean buffer, int bufferSize) {
        checkNull(file);

        try {
            InputStream in = new FileInputStream(file);
            if (buffer) {
                in = new BufferedInputStream(in, bufferSize);
            }
            return in;
        } catch (FileNotFoundException e) {
            return null;
        } catch (SecurityException e2) {
            return null;
        }
    }

    /**
     * Check for null objects in a list of objects.
     */
    private static void checkNull(Object... objs) {
        for (Object obj : objs) {
            if (obj == null)
                throw new NullPointerException(ex_null_param);
        }
    }
}

Related

  1. newInputStream(File file)
  2. openInputStream(File file)
  3. openInputStream(File file)
  4. openInputStream(File file)
  5. openInputStream(File file)
  6. openInputStream(File file, boolean refuseEmpty)
  7. openInputStream(String filePath)
  8. openInputStream(String infilename)
  9. openInputStream(String path)