comparison src/org/tmatesoft/hg/internal/WorkingCopyContent.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
children 4e6179bde4fc
comparison
equal deleted inserted replaced
617:65c01508f002 618:7c0d2ce340b8
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.IOException;
21 import java.nio.ByteBuffer;
22
23 import org.tmatesoft.hg.core.HgCommitCommand;
24 import org.tmatesoft.hg.core.HgIOException;
25 import org.tmatesoft.hg.repo.HgDataFile;
26 import org.tmatesoft.hg.repo.HgInvalidFileException;
27 import org.tmatesoft.hg.repo.HgInvalidStateException;
28 import org.tmatesoft.hg.repo.HgRepository;
29 import org.tmatesoft.hg.util.ByteChannel;
30 import org.tmatesoft.hg.util.CancelledException;
31
32 /**
33 * Access content of the working copy. The difference with {@link FileContentSupplier} is that this one doesn't need {@link File}
34 * in the working directory. However, provided this class is used from {@link HgCommitCommand} when "modified" file was detected,
35 * it's odd to expect no file in the working dir.
36 *
37 * @author Artem Tikhomirov
38 * @author TMate Software Ltd.
39 */
40 public class WorkingCopyContent implements DataSerializer.DataSource {
41 private final HgDataFile file;
42
43 public WorkingCopyContent(HgDataFile dataFile) {
44 file = dataFile;
45 if (!dataFile.exists()) {
46 throw new IllegalArgumentException();
47 }
48 }
49
50 public void serialize(final DataSerializer out) throws HgIOException {
51 final HgIOException failure[] = new HgIOException[1];
52 try {
53 // TODO #workingCopy API is very limiting, CancelledException is inconvenient,
54 // and absence of HgIOException is very uncomfortable
55 file.workingCopy(new ByteChannel() {
56
57 public int write(ByteBuffer buffer) throws IOException {
58 try {
59 if (buffer.hasArray()) {
60 out.write(buffer.array(), buffer.position(), buffer.remaining());
61 }
62 int rv = buffer.remaining();
63 buffer.position(buffer.limit()); // pretend we've consumed the data
64 return rv;
65 } catch (HgIOException ex) {
66 failure[0] = ex;
67 throw new IOException(ex);
68 }
69 }
70 });
71 } catch (HgInvalidFileException ex) {
72 if (failure[0] != null) {
73 throw failure[0];
74 }
75 throw new HgIOException("Write failure", ex, new File(file.getRepo().getWorkingDir(), file.getPath().toString()));
76 } catch (CancelledException ex) {
77 throw new HgInvalidStateException("Our channel doesn't cancel here");
78 }
79 }
80
81 public int serializeLength() {
82 return file.getLength(HgRepository.WORKING_COPY);
83 }
84 }