KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > mplex > SimpleStreamSelector


1 package org.sapia.ubik.net.mplex;
2
3 import java.io.UnsupportedEncodingException JavaDoc;
4
5
6 /**
7  * This basic selector implementation allows you to define a selection logic based on a
8  * simple string value. It can be configures with two types of comparison:
9  * <ol>
10  * <li>The type <code>TYPE_STARTS_WITH</code> will select a stream of data only if it
11  * starts with the string value of this selector.</li>
12  * <li>The type <code>TYPE_CONTAINS</code> will select a stream of data only if it
13  * contains the entire string value of this selector.</li>
14  * </ol>
15  *
16  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">
19  * Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
20  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
21  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a>
22  * at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class SimpleStreamSelector implements StreamSelector {
26   public static final byte TYPE_STARTS_WITH = 1;
27   public static final byte TYPE_CONTAINS = 2;
28   private String JavaDoc _theValue;
29   private int _theType;
30
31   /**
32    * Creates a new SimpleStreamSelector instance.
33    */

34   public SimpleStreamSelector(String JavaDoc aValue, int aType) {
35     _theValue = aValue;
36     _theType = aType;
37   }
38
39   /**
40    * Selects or not a stream by analyzing the header of the stream passed in.
41    *
42    * @param header The first 64 bytes of the stream.
43    * @return True if the header is accepted by this selector, false otherwise.
44    */

45   public boolean selectStream(byte[] header) {
46     try {
47       String JavaDoc aStringValue = new String JavaDoc(header, 0, header.length, "UTF-8");
48
49       if (_theType == TYPE_CONTAINS) {
50         return aStringValue.indexOf(_theValue) >= 0;
51       } else if (_theType == TYPE_STARTS_WITH) {
52         return aStringValue.startsWith(_theValue);
53       } else {
54         return false;
55       }
56     } catch (UnsupportedEncodingException JavaDoc e) {
57       e.printStackTrace();
58
59       return false;
60     }
61   }
62 }
63
Popular Tags