Java BufferedInputStream Copy copyFile(InputStream src, File dest)

Here you can find the source of copyFile(InputStream src, File dest)

Description

Copy inputStream into the specified file.

License

Apache License

Parameter

Parameter Description
src a parameter
dest a parameter

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

public static boolean copyFile(InputStream src, File dest) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2005-2012 Alfresco Software Limited.
 * //  w  w w  . j a va 2  s  .c  o m
 * This file is part of the Alfresco Mobile SDK.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 ******************************************************************************/

import java.io.*;

public class Main {
    public static final int MAX_BUFFER_SIZE = 1024;

    /**
     * Copy inputStream into the specified file. If file already present, we
     * create a new one.
     * 
     * @param src
     * @param dest
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static boolean copyFile(InputStream src, File dest) throws IOException {
        ensureOrCreatePathAndFile(dest);
        return copyStream(src, new FileOutputStream(dest));
    }

    public static void ensureOrCreatePathAndFile(File contentFile) {
        contentFile.getParentFile().mkdirs();
        createUniqueName(contentFile);
    }

    public static boolean copyStream(InputStream src, OutputStream osstream) throws IOException {
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        boolean copied = true;

        try {

            bos = new BufferedOutputStream(osstream);
            bis = new BufferedInputStream(src);

            byte[] buffer = new byte[MAX_BUFFER_SIZE];

            int count;
            while ((count = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, count);
            }
            bos.flush();
        } catch (IOException e) {
            copied = false;
            throw e;
        } finally {
            closeStream(osstream);
            closeStream(src);
            closeStream(bis);
        }
        return copied;
    }

    private static File createUniqueName(File file) {
        if (!file.exists()) {
            return file;
        }

        int index = 1;

        File tmpFile = file;
        while (index < 500) {
            tmpFile = new File(tmpFile.getParentFile(), tmpFile.getName() + "-" + index);
            if (!tmpFile.exists()) {
                return tmpFile;
            }
            index++;
        }
        return null;
    }

    public static void closeStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    }
}

Related

  1. copyFile(final File source, final File destination, final boolean overwrite)
  2. copyFile(final File sourceFile, final File targetFile)
  3. copyFile(final File src, final File dest)
  4. copyFile(final File to, final File from)
  5. copyFile(InputStream is, File newFile)
  6. copyFile(InputStream src, File dst)
  7. copyFile(String dest_path, String src_path)
  8. copyFile(String fileName, File sourceRoot, File targetRoot, Set copied)
  9. copyFile(String from, String to)