/*
 * Copyright 2010-2020 Redgate Software Ltd
 *
 * 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.
 */
package org.flywaydb.core.internal.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;

Utility class for copying files and their contents. Inspired by Spring's own.
/** * Utility class for copying files and their contents. Inspired by Spring's own. */
public class FileCopyUtils {
Prevent instantiation.
/** * Prevent instantiation. */
private FileCopyUtils() { // Do nothing }
Copy the contents of the given Reader into a String. Closes the reader when done.
Params:
  • in – the reader to copy from
Throws:
Returns:the String that has been copied to
/** * Copy the contents of the given Reader into a String. * Closes the reader when done. * * @param in the reader to copy from * @return the String that has been copied to * @throws java.io.IOException in case of I/O errors */
public static String copyToString(Reader in) throws IOException { StringWriter out = new StringWriter(); copy(in, out); String str = out.toString(); //Strip UTF-8 BOM if necessary if (str.startsWith("\ufeff")) { return str.substring(1); } return str; }
Copy the contents of the given InputStream into a new byte array. Closes the stream when done.
Params:
  • in – the stream to copy from
Throws:
Returns:the new byte array that has been copied to
/** * Copy the contents of the given InputStream into a new byte array. * Closes the stream when done. * * @param in the stream to copy from * @return the new byte array that has been copied to * @throws IOException in case of I/O errors */
public static byte[] copyToByteArray(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(4096); copy(in, out); return out.toByteArray(); }
Copy the contents of the given InputStream into a new String based on this encoding. Closes the stream when done.
Params:
  • in – the stream to copy from
  • encoding – The encoding to use.
Throws:
Returns:The new String.
/** * Copy the contents of the given InputStream into a new String based on this encoding. * Closes the stream when done. * * @param in the stream to copy from * @param encoding The encoding to use. * @return The new String. * @throws IOException in case of I/O errors */
public static String copyToString(InputStream in, Charset encoding) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(4096); copy(in, out); return out.toString(encoding.name()); }
Copy the contents of the given Reader to the given Writer. Closes both when done.
Params:
  • in – the Reader to copy from
  • out – the Writer to copy to
Throws:
/** * Copy the contents of the given Reader to the given Writer. * Closes both when done. * * @param in the Reader to copy from * @param out the Writer to copy to * @throws IOException in case of I/O errors */
public static void copy(Reader in, Writer out) throws IOException { try { char[] buffer = new char[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); } finally { IOUtils.close(in); IOUtils.close(out); } }
Copy the contents of the given InputStream to the given OutputStream. Closes both streams when done.
Params:
  • in – the stream to copy from
  • out – the stream to copy to
Throws:
Returns:the number of bytes copied
/** * Copy the contents of the given InputStream to the given OutputStream. * Closes both streams when done. * * @param in the stream to copy from * @param out the stream to copy to * @return the number of bytes copied * @throws IOException in case of I/O errors */
public static int copy(InputStream in, OutputStream out) throws IOException { try { int byteCount = 0; byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); byteCount += bytesRead; } out.flush(); return byteCount; } finally { IOUtils.close(in); IOUtils.close(out); } } }