comparison src/org/tmatesoft/hg/internal/FilterByteChannel.java @ 157:d5268ca7715b

Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 09 Mar 2011 05:22:17 +0100
parents 1a7a9a20e1f9
children f2c11fe7f3e9
comparison
equal deleted inserted replaced
156:643ddec3be36 157:d5268ca7715b
18 18
19 import java.io.IOException; 19 import java.io.IOException;
20 import java.nio.ByteBuffer; 20 import java.nio.ByteBuffer;
21 import java.util.Collection; 21 import java.util.Collection;
22 22
23 import org.tmatesoft.hg.util.Adaptable;
23 import org.tmatesoft.hg.util.ByteChannel; 24 import org.tmatesoft.hg.util.ByteChannel;
24 import org.tmatesoft.hg.util.CancelledException; 25 import org.tmatesoft.hg.util.CancelledException;
25 26
26 /** 27 /**
27 * 28 *
28 * @author Artem Tikhomirov 29 * @author Artem Tikhomirov
29 * @author TMate Software Ltd. 30 * @author TMate Software Ltd.
30 */ 31 */
31 public class FilterByteChannel implements ByteChannel { 32 public class FilterByteChannel implements ByteChannel, Adaptable {
32 private final Filter[] filters; 33 private final Filter[] filters;
33 private final ByteChannel delegate; 34 private final ByteChannel delegate;
34 35
35 public FilterByteChannel(ByteChannel delegateChannel, Collection<Filter> filtersToApply) { 36 public FilterByteChannel(ByteChannel delegateChannel, Collection<Filter> filtersToApply) {
36 if (delegateChannel == null || filtersToApply == null) { 37 if (delegateChannel == null || filtersToApply == null) {
50 } 51 }
51 delegate.write(processed); 52 delegate.write(processed);
52 return buffer.position() - srcPos; // consumed as much from original buffer 53 return buffer.position() - srcPos; // consumed as much from original buffer
53 } 54 }
54 55
56 // adapters or implemented interfaces of the original class shall not be obfuscated by filter
57 public <T> T getAdapter(Class<T> adapterClass) {
58 if (delegate instanceof Adaptable) {
59 return ((Adaptable) delegate).getAdapter(adapterClass);
60 }
61 if (adapterClass != null && adapterClass.isInstance(delegate)) {
62 return adapterClass.cast(delegate);
63 }
64 return null;
65 }
55 } 66 }