KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.net.mplex;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.DataInputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6
7
8 /**
9  * Implements the logic to select a stream of serialized Java objects.
10  *
11  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
12  * <dl>
13  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">
14  * Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a>
17  * at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class ObjectStreamSelector implements StreamSelector {
21   /**
22     * Magic number that is written to the stream header.
23     */

24   final static short STREAM_MAGIC = (short) 0xaced;
25
26   /**
27    * Version number that is written to the stream header.
28    */

29   final static short STREAM_VERSION = 5;
30
31   /**
32    * Creates a new ObjectStreamSelector instance.
33    */

34   public ObjectStreamSelector() {
35   }
36
37   /**
38    * Selects or not a stream by analyzing the header of the stream passed in.
39    *
40    * @param header The first 64 bytes of the stream.
41    * @return True if the header is accepted by this selector, false otherwise.
42    */

43   public boolean selectStream(byte[] header) {
44     DataInputStream JavaDoc data = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(header));
45
46     try {
47       if ((data.readShort() != STREAM_MAGIC) ||
48             (data.readShort() != STREAM_VERSION)) {
49         return false;
50       } else {
51         return true;
52       }
53     } catch (IOException JavaDoc ioe) {
54       ioe.printStackTrace();
55
56       return false;
57     }
58   }
59 }
60
Popular Tags