Mercurial > jhg
diff src/org/tmatesoft/hg/internal/FileContentSupplier.java @ 586:73c20c648c1f
HgCommitCommand initial support
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 26 Apr 2013 18:38:41 +0200 |
parents | |
children | e447384f3771 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/tmatesoft/hg/internal/FileContentSupplier.java Fri Apr 26 18:38:41 2013 +0200 @@ -0,0 +1,74 @@ +/* + * Copyright (c) 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.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +import org.tmatesoft.hg.core.HgIOException; +import org.tmatesoft.hg.repo.CommitFacility; +import org.tmatesoft.hg.repo.HgRepository; +import org.tmatesoft.hg.util.Path; + +/** + * FIXME files are opened at the moment of instantiation, though the moment the data is requested might be distant + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class FileContentSupplier implements CommitFacility.ByteDataSupplier { + private final FileChannel channel; + private IOException error; + + public FileContentSupplier(HgRepository repo, Path file) throws HgIOException { + this(new File(repo.getWorkingDir(), file.toString())); + } + + public FileContentSupplier(File f) throws HgIOException { + if (!f.canRead()) { + throw new HgIOException(String.format("Can't read file %s", f), f); + } + try { + channel = new FileInputStream(f).getChannel(); + } catch (FileNotFoundException ex) { + throw new HgIOException("Can't open file", ex, f); + } + } + + public int read(ByteBuffer buf) { + if (error != null) { + return -1; + } + try { + return channel.read(buf); + } catch (IOException ex) { + error = ex; + } + return -1; + } + + public void done() throws IOException { + channel.close(); + if (error != null) { + throw error; + } + } +} \ No newline at end of file