comparison src/org/tmatesoft/hg/core/HgCatCommand.java @ 322:d68dcb3b5f49

Propagate command's CancelSupport to low-level API. CancelSupport from context got priority over one from command
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 30 Sep 2011 08:00:04 +0200
parents 3fbfce107f94
children 5f9073eabf06
comparison
equal deleted inserted replaced
321:ac38e75c9e8e 322:d68dcb3b5f49
19 import static org.tmatesoft.hg.repo.HgInternals.wrongLocalRevision; 19 import static org.tmatesoft.hg.repo.HgInternals.wrongLocalRevision;
20 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION; 20 import static org.tmatesoft.hg.repo.HgRepository.BAD_REVISION;
21 import static org.tmatesoft.hg.repo.HgRepository.TIP; 21 import static org.tmatesoft.hg.repo.HgRepository.TIP;
22 22
23 import java.io.FileNotFoundException; 23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
24 26
25 import org.tmatesoft.hg.repo.HgDataFile; 27 import org.tmatesoft.hg.repo.HgDataFile;
26 import org.tmatesoft.hg.repo.HgRepository; 28 import org.tmatesoft.hg.repo.HgRepository;
29 import org.tmatesoft.hg.util.Adaptable;
27 import org.tmatesoft.hg.util.ByteChannel; 30 import org.tmatesoft.hg.util.ByteChannel;
31 import org.tmatesoft.hg.util.CancelSupport;
28 import org.tmatesoft.hg.util.CancelledException; 32 import org.tmatesoft.hg.util.CancelledException;
29 import org.tmatesoft.hg.util.Path; 33 import org.tmatesoft.hg.util.Path;
30 34
31 /** 35 /**
32 * Command to obtain content of a file, 'hg cat' counterpart. 36 * Command to obtain content of a file, 'hg cat' counterpart.
168 } else if (revision != null) { 172 } else if (revision != null) {
169 revToExtract = dataFile.getLocalRevision(revision); 173 revToExtract = dataFile.getLocalRevision(revision);
170 } else { 174 } else {
171 revToExtract = localRevision; 175 revToExtract = localRevision;
172 } 176 }
173 dataFile.contentWithFilters(revToExtract, sink); 177 ByteChannel sinkWrap;
178 if (getCancelSupport(null, false) == null) {
179 // no command-specific cancel helper, no need for extra proxy
180 // sink itself still may supply CS
181 sinkWrap = sink;
182 } else {
183 // try CS from sink, if any. at least there is CS from command
184 CancelSupport cancelHelper = getCancelSupport(sink, true);
185 cancelHelper.checkCancelled();
186 sinkWrap = new ByteChannelProxy(sink, cancelHelper);
187 }
188 dataFile.contentWithFilters(revToExtract, sinkWrap);
189 }
190
191 private static class ByteChannelProxy implements ByteChannel, Adaptable {
192 private final ByteChannel delegate;
193 private final CancelSupport cancelHelper;
194
195 public ByteChannelProxy(ByteChannel _delegate, CancelSupport cs) {
196 assert _delegate != null;
197 delegate = _delegate;
198 cancelHelper = cs;
199 }
200 public int write(ByteBuffer buffer) throws IOException, CancelledException {
201 return delegate.write(buffer);
202 }
203
204 public <T> T getAdapter(Class<T> adapterClass) {
205 if (CancelSupport.class == adapterClass) {
206 return adapterClass.cast(cancelHelper);
207 }
208 if (delegate instanceof Adaptable) {
209 return ((Adaptable) delegate).getAdapter(adapterClass);
210 }
211 if (adapterClass.isInstance(delegate)) {
212 return adapterClass.cast(delegate);
213 }
214 return null;
215 }
174 } 216 }
175 } 217 }