Mercurial > jhg
diff src/org/tmatesoft/hg/internal/FileContentSupplier.java @ 618:7c0d2ce340b8
Refactor approach how content finds it way down to a commit revision
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 16 May 2013 19:46:13 +0200 |
parents | e447384f3771 |
children | 12a4f60ea972 |
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/FileContentSupplier.java Wed May 15 20:10:09 2013 +0200 +++ b/src/org/tmatesoft/hg/internal/FileContentSupplier.java Thu May 16 19:46:13 2013 +0200 @@ -18,56 +18,55 @@ 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.core.SessionContext; +import org.tmatesoft.hg.internal.DataSerializer.DataSource; 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 + * {@link DataSource} that reads from regular files * * @author Artem Tikhomirov * @author TMate Software Ltd. */ -public class FileContentSupplier implements CommitFacility.ByteDataSupplier { - private final FileChannel channel; - private IOException error; +public class FileContentSupplier implements DataSource { + private final File file; + private final SessionContext ctx; - 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 FileContentSupplier(HgRepository repo, Path file) { + this(repo, new File(repo.getWorkingDir(), file.toString())); } - public int read(ByteBuffer buf) { - if (error != null) { - return -1; - } - try { - return channel.read(buf); - } catch (IOException ex) { - error = ex; - } - return -1; + public FileContentSupplier(SessionContext.Source ctxSource, File f) { + ctx = ctxSource.getSessionContext(); + file = f; } - public void done() throws IOException { - channel.close(); - if (error != null) { - throw error; + public void serialize(DataSerializer out) throws HgIOException { + FileInputStream fis = null; + try { + fis = new FileInputStream(file); + FileChannel fc = fis.getChannel(); + ByteBuffer buffer = ByteBuffer.allocate((int) Math.min(100*1024, fc.size())); + while (fc.read(buffer) != -1) { + buffer.flip(); + // #allocate() above ensures backing array + out.write(buffer.array(), 0, buffer.limit()); + buffer.clear(); + } + } catch (IOException ex) { + throw new HgIOException("Failed to get content of the file", ex, file); + } finally { + new FileUtils(ctx.getLog()).closeQuietly(fis); } } + + public int serializeLength() { + return Internals.ltoi(file.length()); + } } \ No newline at end of file