Here you can find the source of InputStreamToFile(final InputStream stream, final File file)
public static long InputStreamToFile(final InputStream stream, final File file) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2010-2014 Alexander Kerner. All rights reserved. * * 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./* w w w . j a v a 2s .c o m*/ ******************************************************************************/ import java.io.Closeable; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.Writer; public class Main { /** * The JCL book specifies the default buffer size as 8K characters. */ public static final int DEFAULT_BUFFER = 8192; public static long InputStreamToFile(final InputStream stream, final File file) throws IOException { final FileWriter w = new FileWriter(file); final InputStreamReader r = new InputStreamReader(stream); final long result = readerToWriter(new InputStreamReader(stream), w); closeProperly(w); closeProperly(r); return result; } /** * Copies the content of a <code>Reader</code> to a <code>Writer</code>. * Will flush the <code>Writer</code>, but won't close <code>Reader</code> * or <code>Writer</code>. * * @param reader * <code>Reader</code> from which data is read. * @param writer * <code>Writer</code> to which data is written. * @return number of bytes read/ written. * @throws IOException */ public static long readerToWriter(final Reader reader, final Writer writer) throws IOException { return readerToWriter(reader, writer, 0); } /** * Copies the content of a <code>Reader</code> to a <code>Writer</code>. * Will flush the <code>Writer</code>, but won't close <code>Reader</code> * or <code>Writer</code>. * * @param reader * <code>Reader</code> from which data is read. * @param writer * <code>Writer</code> to which data is written. * @param buffer * the number of bytes to buffer reading. * @return number of bytes read/written. * @throws IOException */ public static long readerToWriter(final Reader reader, final Writer writer, int buffer) throws IOException { if (buffer < 1) buffer = DEFAULT_BUFFER; final char[] charBuffer = new char[buffer]; long count = 0; int n = 0; while ((n = reader.read(charBuffer)) != -1) { writer.write(charBuffer, 0, n); count += n; } writer.flush(); return count; } /** * * @deprecated Use {@link CloserProperly} instead. */ @Deprecated public static void closeProperly(final Closeable closable) { if (closable != null) try { closable.close(); } catch (final Exception e) { e.printStackTrace(); } } }