1 /*
2 * Copyright (c) 1998-2002 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.relacs.types;
20
21 import java.io.IOException;
22 import java.io.ObjectInput;
23 import java.io.ObjectOutput;
24
25 /**
26 * The <code>Checksum</code> class wraps a value of the relacs daemon type
27 * "checksum" in an object. A checksum is used to quickly verify if two
28 * proposals are equal or not. If the checksum is different, the proposal
29 * are different. Otherwise, we have to check their content.
30 *
31 * @author Alberto Montresor
32 * @since Jgroup 1.2
33 */
34
35 public final class Checksum
36 {
37
38 ////////////////////////////////////////////////////////////////////////////////////////////
39 // Constants
40 ////////////////////////////////////////////////////////////////////////////////////////////
41
42 /** Size of this object in bytes */
43 public static final int SIZE = 1;
44
45
46 ////////////////////////////////////////////////////////////////////////////////////////////
47 // Fields
48 ////////////////////////////////////////////////////////////////////////////////////////////
49
50 /** Wrapped value */
51 private int value;
52
53
54 ////////////////////////////////////////////////////////////////////////////////////////////
55 // Constructor
56 ////////////////////////////////////////////////////////////////////////////////////////////
57
58 /**
59 * Creates a <CODE>Checksum</CODE> object containing the specified
60 * value.
61 */
62 public Checksum(int value)
63 {
64 this.value = value;
65 }
66
67
68 ////////////////////////////////////////////////////////////////////////////////////////////
69 // Accessor method
70 ////////////////////////////////////////////////////////////////////////////////////////////
71
72 /**
73 * Returned the wrapped value.
74 */
75 public int getValue()
76 {
77 return value;
78 }
79
80 ////////////////////////////////////////////////////////////////////////////////////////////
81 // Marshaling / unmarshaling methods
82 ////////////////////////////////////////////////////////////////////////////////////////////
83
84 /**
85 * Marshals the specified checksum to an outgoing message.
86 */
87 public static void marshal(ObjectOutput msg, int value)
88 throws IOException
89 {
90 msg.write(value);
91 }
92
93 /**
94 * Unmarshals a checksum from an incoming message.
95 */
96 public static int unmarshal(ObjectInput msg)
97 throws IOException
98 {
99 return msg.read();
100 }
101
102 /**
103 * Returns the checksum of a sequence of bytes starting at position <code> start</code>
104 * and ending at position <code> stop</code> of array <code> data</code>.
105 * index and an ending index.
106 *
107 * @param data the byte array
108 * @param start the starting index.
109 * @param stop the ending index.
110 */
111 static int computeChecksum(byte[] data, int start, int stop)
112 {
113 byte check = 0; // Return value
114
115 for (int i=start; i<stop; i++)
116 check ^= data[i];
117 return check;
118 }
119
120 } // END Checksum