1 26 27 package org.objectweb.openccm.pss.runtime.common.lib; 28 29 42 public class PIDHelper 43 { 44 50 56 59 public PIDHelper() 60 { 61 } 62 63 69 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 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 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 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 141 149 public static byte[] 150 encode(String sh_rid, String short_pid) 151 { 152 java.io.ByteArrayOutputStream pid; 153 byte[] enc_sh_rid = null, 154 enc_short_pid = null; 155 156 pid = new java.io.ByteArrayOutputStream (); 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 ex){ 166 return null; 167 } 168 return pid.toByteArray(); 169 } 170 171 176 public static String 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 rid = new byte[size]; 187 idx = 4; 188 java.lang.System.arraycopy(pid,idx,rid,0,size); 189 190 return new String (rid); 191 } 192 193 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 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 |