Mercurial > hg4j
comparison 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 |
comparison
equal
deleted
inserted
replaced
585:b47ef0d2777b | 586:73c20c648c1f |
---|---|
1 /* | |
2 * Copyright (c) 2013 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.internal; | |
18 | |
19 import java.io.File; | |
20 import java.io.FileInputStream; | |
21 import java.io.FileNotFoundException; | |
22 import java.io.IOException; | |
23 import java.nio.ByteBuffer; | |
24 import java.nio.channels.FileChannel; | |
25 | |
26 import org.tmatesoft.hg.core.HgIOException; | |
27 import org.tmatesoft.hg.repo.CommitFacility; | |
28 import org.tmatesoft.hg.repo.HgRepository; | |
29 import org.tmatesoft.hg.util.Path; | |
30 | |
31 /** | |
32 * FIXME files are opened at the moment of instantiation, though the moment the data is requested might be distant | |
33 * | |
34 * @author Artem Tikhomirov | |
35 * @author TMate Software Ltd. | |
36 */ | |
37 public class FileContentSupplier implements CommitFacility.ByteDataSupplier { | |
38 private final FileChannel channel; | |
39 private IOException error; | |
40 | |
41 public FileContentSupplier(HgRepository repo, Path file) throws HgIOException { | |
42 this(new File(repo.getWorkingDir(), file.toString())); | |
43 } | |
44 | |
45 public FileContentSupplier(File f) throws HgIOException { | |
46 if (!f.canRead()) { | |
47 throw new HgIOException(String.format("Can't read file %s", f), f); | |
48 } | |
49 try { | |
50 channel = new FileInputStream(f).getChannel(); | |
51 } catch (FileNotFoundException ex) { | |
52 throw new HgIOException("Can't open file", ex, f); | |
53 } | |
54 } | |
55 | |
56 public int read(ByteBuffer buf) { | |
57 if (error != null) { | |
58 return -1; | |
59 } | |
60 try { | |
61 return channel.read(buf); | |
62 } catch (IOException ex) { | |
63 error = ex; | |
64 } | |
65 return -1; | |
66 } | |
67 | |
68 public void done() throws IOException { | |
69 channel.close(); | |
70 if (error != null) { | |
71 throw error; | |
72 } | |
73 } | |
74 } |