Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/FilterByteChannel.java @ 115:c0cc2535462c
Introduced channels to pipeline (and easily filter) data streams
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 03 Feb 2011 23:32:08 +0100 |
parents | |
children | 7567f4a42fe5 |
comparison
equal
deleted
inserted
replaced
114:46291ec605a0 | 115:c0cc2535462c |
---|---|
1 /* | |
2 * Copyright (c) 2011 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@svnkit.com | |
16 */ | |
17 package org.tmatesoft.hg.internal; | |
18 | |
19 import java.nio.ByteBuffer; | |
20 import java.util.Collection; | |
21 | |
22 import org.tmatesoft.hg.util.ByteChannel; | |
23 | |
24 /** | |
25 * | |
26 * @author Artem Tikhomirov | |
27 * @author TMate Software Ltd. | |
28 */ | |
29 public class FilterByteChannel implements ByteChannel { | |
30 private final Filter[] filters; | |
31 private final ByteChannel delegate; | |
32 | |
33 public FilterByteChannel(ByteChannel delegateChannel, Collection<Filter> filtersToApply) { | |
34 if (delegateChannel == null || filtersToApply == null) { | |
35 throw new IllegalArgumentException(); | |
36 } | |
37 delegate = delegateChannel; | |
38 filters = filtersToApply.toArray(new Filter[filtersToApply.size()]); | |
39 } | |
40 | |
41 public int write(ByteBuffer buffer) throws Exception { | |
42 final int srcPos = buffer.position(); | |
43 ByteBuffer processed = buffer; | |
44 for (Filter f : filters) { | |
45 // each next filter consumes not more than previous | |
46 // hence total consumed equals position shift in the original buffer | |
47 processed = f.filter(processed); | |
48 } | |
49 delegate.write(processed); | |
50 return buffer.position() - srcPos; // consumed as much from original buffer | |
51 } | |
52 | |
53 } |