tikhomirov@586: /* tikhomirov@586: * Copyright (c) 2013 TMate Software Ltd tikhomirov@586: * tikhomirov@586: * This program is free software; you can redistribute it and/or modify tikhomirov@586: * it under the terms of the GNU General Public License as published by tikhomirov@586: * the Free Software Foundation; version 2 of the License. tikhomirov@586: * tikhomirov@586: * This program is distributed in the hope that it will be useful, tikhomirov@586: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@586: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@586: * GNU General Public License for more details. tikhomirov@586: * tikhomirov@586: * For information on how to redistribute this software under tikhomirov@586: * the terms of a license other than GNU General Public License tikhomirov@586: * contact TMate Software at support@hg4j.com tikhomirov@586: */ tikhomirov@586: package org.tmatesoft.hg.internal; tikhomirov@586: tikhomirov@586: import java.io.File; tikhomirov@586: import java.io.FileInputStream; tikhomirov@586: import java.io.FileNotFoundException; tikhomirov@586: import java.io.IOException; tikhomirov@586: import java.nio.ByteBuffer; tikhomirov@586: import java.nio.channels.FileChannel; tikhomirov@586: tikhomirov@586: import org.tmatesoft.hg.core.HgIOException; tikhomirov@586: import org.tmatesoft.hg.repo.CommitFacility; tikhomirov@586: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@586: import org.tmatesoft.hg.util.Path; tikhomirov@586: tikhomirov@586: /** tikhomirov@586: * FIXME files are opened at the moment of instantiation, though the moment the data is requested might be distant tikhomirov@586: * tikhomirov@586: * @author Artem Tikhomirov tikhomirov@586: * @author TMate Software Ltd. tikhomirov@586: */ tikhomirov@586: public class FileContentSupplier implements CommitFacility.ByteDataSupplier { tikhomirov@586: private final FileChannel channel; tikhomirov@586: private IOException error; tikhomirov@586: tikhomirov@586: public FileContentSupplier(HgRepository repo, Path file) throws HgIOException { tikhomirov@586: this(new File(repo.getWorkingDir(), file.toString())); tikhomirov@586: } tikhomirov@586: tikhomirov@586: public FileContentSupplier(File f) throws HgIOException { tikhomirov@586: if (!f.canRead()) { tikhomirov@586: throw new HgIOException(String.format("Can't read file %s", f), f); tikhomirov@586: } tikhomirov@586: try { tikhomirov@586: channel = new FileInputStream(f).getChannel(); tikhomirov@586: } catch (FileNotFoundException ex) { tikhomirov@586: throw new HgIOException("Can't open file", ex, f); tikhomirov@586: } tikhomirov@586: } tikhomirov@586: tikhomirov@586: public int read(ByteBuffer buf) { tikhomirov@586: if (error != null) { tikhomirov@586: return -1; tikhomirov@586: } tikhomirov@586: try { tikhomirov@586: return channel.read(buf); tikhomirov@586: } catch (IOException ex) { tikhomirov@586: error = ex; tikhomirov@586: } tikhomirov@586: return -1; tikhomirov@586: } tikhomirov@586: tikhomirov@586: public void done() throws IOException { tikhomirov@586: channel.close(); tikhomirov@586: if (error != null) { tikhomirov@586: throw error; tikhomirov@586: } tikhomirov@586: } tikhomirov@586: }