KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > pss > runtime > common > lib > PIDHelper


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Demarey.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.pss.runtime.common.lib;
28
29 /**
30  * This helper class provides tools for Persistent ID.
31  *
32  * PID is structured in the following way :
33  * byte 0 - 3 : Length of the Storage Home Repository ID string (l1).
34  * byte 4 - 4+l1-1 : Storage Home Repository ID.
35  * byte 4+l1 - 8+l1-1 : Length of the Short PID (l2).
36  * byte 8+l1 - end : Short PID.
37  *
38  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
39  *
40  * @version 0.1
41  */

42 public class PIDHelper
43 {
44     // ==================================================================
45
//
46
// Internal state.
47
//
48
// ==================================================================
49

50     // ==================================================================
51
//
52
// Constructor.
53
//
54
// ==================================================================
55

56     /**
57      * The default constructor.
58      */

59     public PIDHelper()
60     {
61     }
62
63     // ==================================================================
64
//
65
// Internal methods.
66
//
67
// ==================================================================
68

69     /**
70      * Encode an integer to a byte array.
71      *
72      * @param i - The integer to encode.
73      *
74      * @return Returns the value of this Integer as a byte array.
75      */

76     private static byte[]
77     intToByte(int i)
78     {
79         byte[] buf = new byte[4];
80
81         buf[0] = (byte)((i>>>0) & 0xff);
82         buf[1] = (byte)((i>>>8) & 0xff);
83         buf[2] = (byte)((i>>>16) & 0xff);
84         buf[3] = (byte)((i>>>24) & 0xff);
85         return buf;
86     }
87
88     /**
89      * Decode an integer contained in a byte array.
90      *
91      * @param buf - The byte array to decode.
92      *
93      * @return The integer value of this byte array.
94      */

95     private static int
96     byteToInt(byte[] buf)
97     {
98         return (buf[0] & 0xff) + ((buf[1] & 0xff) << 8) + ((buf[2] & 0xff) << 16) + ((buf[3] & 0xff) << 24);
99     }
100
101     /**
102      * Get the Storage Home Repository ID length.
103      *
104      * @param pid - The Persistent id to decode.
105      *
106      * @return The Storage Home Repository ID length.
107      */

108     private static int
109     get_RID_length(byte[] pid)
110     {
111         byte[] rid_length = new byte[4];
112
113         java.lang.System.arraycopy(pid,0,rid_length,0,4);
114         return byteToInt(rid_length);
115     }
116
117     /**
118      * Get the short PID length.
119      *
120      * @param pid - The Persistent id to decode.
121      *
122      * @return The short PID length.
123      */

124     private static int
125     get_short_pid_length(byte[] pid)
126     {
127         byte[] short_pid_length = new byte[4];
128         int idx;
129
130         idx = 4+get_RID_length(pid);
131         java.lang.System.arraycopy(pid,idx,short_pid_length,0,4);
132         return byteToInt(short_pid_length);
133     }
134
135     // ==================================================================
136
//
137
// Public methods.
138
//
139
// ==================================================================
140

141     /**
142      * Encode a PID.
143      *
144      * @param sh_rid - The Storage Home Repository ID.
145      * @param short_pid - The short pid.
146      *
147      * @return The generated PID.
148      */

149     public static byte[]
150     encode(String JavaDoc sh_rid, String JavaDoc short_pid)
151     {
152         java.io.ByteArrayOutputStream JavaDoc pid;
153         byte[] enc_sh_rid = null,
154                enc_short_pid = null;
155
156         pid = new java.io.ByteArrayOutputStream JavaDoc();
157         enc_sh_rid = sh_rid.getBytes();
158         enc_short_pid = short_pid.getBytes();
159
160         try{
161             pid.write(intToByte(enc_sh_rid.length));
162             pid.write(enc_sh_rid);
163             pid.write(intToByte(enc_short_pid.length));
164             pid.write(enc_short_pid);
165         }catch(java.io.IOException JavaDoc ex){
166             return null;
167         }
168         return pid.toByteArray();
169     }
170
171     /**
172      * Get the Storage Home Repository ID.
173      *
174      * @return The Storage Home Repository ID.
175      */

176     public static String JavaDoc
177     get_RID(byte[] pid)
178     {
179         byte[] rid_length = null,
180                rid = null;
181         int size, idx;
182
183         size = get_RID_length(pid);
184
185         // Get the RID
186
rid = new byte[size];
187         idx = 4;
188         java.lang.System.arraycopy(pid,idx,rid,0,size);
189
190         return new String JavaDoc(rid);
191     }
192
193     /**
194      * Get the short PID.
195      *
196      * @return The short PID.
197      */

198     public static byte[]
199     get_short_pid(byte[] pid)
200     {
201         byte[] short_pid_length = null,
202                short_pid = null;
203         int size, idx;
204
205         size = get_short_pid_length(pid);
206
207         // Get the short pid
208
short_pid = new byte[size];
209         idx = 8 + get_RID_length(pid);
210         java.lang.System.arraycopy(pid,idx,short_pid,0,size);
211
212         return short_pid;
213     }
214 }
215
Popular Tags