view src/org/tmatesoft/hg/internal/WorkingDirFileWriter.java @ 525:0be5be8d57e9

Repository checkout support, first iteration
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 11 Jan 2013 18:12:39 +0100
parents
children 2f9ed6bcefa2
line wrap: on
line source
/*
 * Copyright (c) 2012-2013 TMate Software Ltd
 *  
 * This program 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; version 2 of the License.
 *
 * This program 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.
 *
 * For information on how to redistribute this software under
 * the terms of a license other than GNU General Public License
 * contact TMate Software at support@hg4j.com
 */
package org.tmatesoft.hg.internal;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import org.tmatesoft.hg.repo.HgDataFile;
import org.tmatesoft.hg.repo.HgRepository;
import org.tmatesoft.hg.util.ByteChannel;
import org.tmatesoft.hg.util.CancelledException;
import org.tmatesoft.hg.util.Path;
import org.tmatesoft.hg.util.LogFacility.Severity;

/**
 * 
 * @author Artem Tikhomirov
 * @author TMate Software Ltd.
 */
public class WorkingDirFileWriter implements ByteChannel {

	
	private final HgRepository repo;
	private File dest;
	private FileChannel destChannel;
	private int totalBytesWritten;

	public WorkingDirFileWriter(HgRepository hgRepo) {
		repo = hgRepo;
	}
	
	public void processFile(HgDataFile df, int fileRevIndex) throws IOException {
		try {
			prepare(df.getPath());
			df.contentWithFilters(fileRevIndex, this);
		} catch (CancelledException ex) {
			repo.getSessionContext().getLog().dump(getClass(), Severity.Error, ex, "Our impl doesn't throw cancellation");
		}
		finish();
	}

	public void prepare(Path fname) throws IOException {
		String fpath = fname.toString();
		dest = new File(repo.getWorkingDir(), fpath);
		if (fpath.indexOf('/') != -1) {
			dest.getParentFile().mkdirs();
		}
		destChannel = new FileOutputStream(dest).getChannel();
		totalBytesWritten = 0;
	}

	public int write(ByteBuffer buffer) throws IOException, CancelledException {
		int written = destChannel.write(buffer);
		totalBytesWritten += written;
		return written;
	}

	public void finish() throws IOException {
		destChannel.close();
		dest = null;
	}
	
	public int bytesWritten() {
		return totalBytesWritten;
	}
}