Java InputStream Copy to File copyStreamToFile(InputStream stream, File destFile, long fileTime)

Here you can find the source of copyStreamToFile(InputStream stream, File destFile, long fileTime)

Description

copy Stream To File

License

Open Source License

Declaration

public static void copyStreamToFile(InputStream stream, File destFile, long fileTime) throws IOException 

Method Source Code

//package com.java2s;
/*==========================================================================*\
 |  Copyright (C) 2012 Virginia Tech/*from  w  w  w .  j  a  va 2 s.c  o m*/
 |
 |  This file is part of Web-CAT Eclipse Plugins.
 |
 |  Web-CAT is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU General Public License as published by
 |  the Free Software Foundation; either version 2 of the License, or
 |  (at your option) any later version.
 |
 |  Web-CAT 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 General Public License for more details.
 |
 |  You should have received a copy of the GNU General Public License along
 |  with Web-CAT; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.File;
import java.io.InputStream;

public class Main {
    public static void copyStreamToFile(InputStream stream, File destFile, long fileTime) throws IOException {
        OutputStream outStream = new FileOutputStream(destFile);
        copyStream(stream, outStream);
        outStream.flush();
        outStream.close();

        destFile.setLastModified(fileTime);
    }

    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        final int BUFFER_SIZE = 65536;

        // read in increments of BUFFER_SIZE
        byte[] b = new byte[BUFFER_SIZE];
        int count = in.read(b);
        while (count > -1) {
            out.write(b, 0, count);
            count = in.read(b);
        }

        out.flush();
    }
}

Related

  1. copyStreamToFile(InputStream in, File target)
  2. copyStreamToFile(InputStream inputStream, File destFile)
  3. copyStreamToFile(InputStream pInputStream, File pFile)
  4. copyStreamToFile(InputStream source, File target)
  5. copyStreamToFile(InputStream stream, File destFile)
  6. copyStreamToFile(InputStream stream, File file)