/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.examples.filesystem;

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

import org.eclipse.core.internal.utils.FileUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.core.TeamException;

public class StreamUtil {

	protected final static byte[] COPY_BUFFER = new byte[4096];

	public static void pipe(InputStream in, OutputStream out,
			long sizeEstimate, IProgressMonitor progress, String title)
			throws IOException {

		// Only show progress for files larger than 25Kb.
		Long kilobytesEstimate = new Long(sizeEstimate / 1024);
		boolean showProgress = (progress != null) && (sizeEstimate > 25000);
		long bytesCopied = 0;

		synchronized (COPY_BUFFER) {
			// Read the initial chunk.
			int read = in.read(COPY_BUFFER, 0, COPY_BUFFER.length);

			while (read != -1) {
				out.write(COPY_BUFFER, 0, read);

				// Report progress
				if (showProgress) {
					bytesCopied = bytesCopied + read;
					progress.subTask(Policy.bind("filetransfer.monitor", //$NON-NLS-1$
							new Object[] { title, new Long(bytesCopied / 1024),
									kilobytesEstimate }));
				}

				// Read the next chunk.
				read = in.read(COPY_BUFFER, 0, COPY_BUFFER.length);
			} // end while
		} // end synchronized
	}

	// Deletes all files and subdirectories under dir.
	// Returns true if all deletions were successful.
	// If a deletion fails, the method stops attempting to delete and returns
	// false.
	public static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}

		// The directory is now empty so delete it
		return dir.delete();
	}

	public static void copyFolder(File inputFolder, File outputFolder) {
		outputFolder.mkdir();
		String[] children = inputFolder.list();
		if (children == null) {
			// Either dir does not exist or is not a directory
		} else {
			for (int p = 0; p < children.length; p++) {
				File inputFileFolder = new File(inputFolder + "/" + children[p]);
				File outputFileFolder = new File(outputFolder + "/" + children[p]);
				if (inputFileFolder.isDirectory()) {
					copyFolder(inputFileFolder, outputFileFolder);
				}
				else {
					try {
						copyFile(inputFileFolder, outputFileFolder);
					} catch (Exception e) {
						throw new RuntimeException(e);
					}
				}
			}
		}
	}

	public static void copyFile(File inputFile, File outputFile) {
		try {
			File parent = outputFile.getParentFile();
			parent.mkdirs();
			FileUtil.transferStreams(new FileInputStream(inputFile), new FileOutputStream(outputFile), inputFile.getAbsolutePath(), null);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
