annotate src/org/tmatesoft/hg/internal/FileContentSupplier.java @ 614:1d1e96f5a908

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