comparison src/org/tmatesoft/hg/internal/WorkingDirFileWriter.java @ 525:0be5be8d57e9

Repository checkout support, first iteration
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 11 Jan 2013 18:12:39 +0100
parents
children 2f9ed6bcefa2
comparison
equal deleted inserted replaced
524:57b2c9eb3c69 525:0be5be8d57e9
1 /*
2 * Copyright (c) 2012-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.FileOutputStream;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.channels.FileChannel;
24
25 import org.tmatesoft.hg.repo.HgDataFile;
26 import org.tmatesoft.hg.repo.HgRepository;
27 import org.tmatesoft.hg.util.ByteChannel;
28 import org.tmatesoft.hg.util.CancelledException;
29 import org.tmatesoft.hg.util.Path;
30 import org.tmatesoft.hg.util.LogFacility.Severity;
31
32 /**
33 *
34 * @author Artem Tikhomirov
35 * @author TMate Software Ltd.
36 */
37 public class WorkingDirFileWriter implements ByteChannel {
38
39
40 private final HgRepository repo;
41 private File dest;
42 private FileChannel destChannel;
43 private int totalBytesWritten;
44
45 public WorkingDirFileWriter(HgRepository hgRepo) {
46 repo = hgRepo;
47 }
48
49 public void processFile(HgDataFile df, int fileRevIndex) throws IOException {
50 try {
51 prepare(df.getPath());
52 df.contentWithFilters(fileRevIndex, this);
53 } catch (CancelledException ex) {
54 repo.getSessionContext().getLog().dump(getClass(), Severity.Error, ex, "Our impl doesn't throw cancellation");
55 }
56 finish();
57 }
58
59 public void prepare(Path fname) throws IOException {
60 String fpath = fname.toString();
61 dest = new File(repo.getWorkingDir(), fpath);
62 if (fpath.indexOf('/') != -1) {
63 dest.getParentFile().mkdirs();
64 }
65 destChannel = new FileOutputStream(dest).getChannel();
66 totalBytesWritten = 0;
67 }
68
69 public int write(ByteBuffer buffer) throws IOException, CancelledException {
70 int written = destChannel.write(buffer);
71 totalBytesWritten += written;
72 return written;
73 }
74
75 public void finish() throws IOException {
76 destChannel.close();
77 dest = null;
78 }
79
80 public int bytesWritten() {
81 return totalBytesWritten;
82 }
83 }