00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.mpi.control.util;
00032 
00033 
00045 public class ProActiveMPIUtil {
00046     public static final int BYTE_LEN = 1;
00047     public static final int SHORT_LEN = 2;
00048     public static final int INT_LEN = 4;
00049     public static final int FLOAT_LEN = 4;
00050     public static final int LONG_LEN = 8;
00051     public static final int DOUBLE_LEN = 8;
00052 
00060     public static int intToBytes(int num, byte[] bytes, int startIndex) {
00061         bytes[startIndex] = (byte) (num & 0xff);
00062         bytes[startIndex + 1] = (byte) ((num >> 8) & 0xff);
00063         bytes[startIndex + 2] = (byte) ((num >> 16) & 0xff);
00064         bytes[startIndex + 3] = (byte) ((num >> 24) & 0xff);
00065         return startIndex + 4;
00066     }
00067 
00072     public static int bytesToInt(byte[] bytes, int startIndex) {
00073         return (((int) bytes[startIndex] & 0xff) |
00074         (((int) bytes[startIndex + 1] & 0xff) << 8) |
00075         (((int) bytes[startIndex + 2] & 0xff) << 16) |
00076         (((int) bytes[startIndex + 3] & 0xff) << 24));
00077     }
00078 
00086     public static int floatToBytes(float fnum, byte[] bytes, int startIndex) {
00087         return intToBytes(Float.floatToIntBits(fnum), bytes, startIndex);
00088     }
00089 
00094     public static float bytesToFloat(byte[] bytes, int startIndex) {
00095         return (Float.intBitsToFloat(bytesToInt(bytes, startIndex)));
00096     }
00097 
00105     public static int shortToBytes(short num, byte[] bytes, int startIndex) {
00106         bytes[startIndex] = (byte) (num & 0xff);
00107         bytes[startIndex + 1] = (byte) ((num >> 8) & 0xff);
00108         return startIndex + 2;
00109     }
00110 
00115     public static short bytesToShort(byte[] bytes, int startIndex) {
00116         return (short) (((int) bytes[startIndex] & 0xff) |
00117         (((int) bytes[startIndex + 1] & 0xff) << 8));
00118     }
00119 
00131     public static int stringToBytes(String str, byte[] bytes, int startIndex) {
00132         byte[] temp;
00133         int len = str.length();
00134 
00135         temp = str.getBytes();
00136         if (len > 255) {
00137             System.err.println(
00138                 "String has more than 255 bytes in \"stringToBytes\", it will be truncated.");
00139 
00140             bytes[startIndex++] = (byte) 255;
00141             System.arraycopy(temp, 0, bytes, startIndex, 255);
00142             return startIndex + 255;
00143         } else {
00144             bytes[startIndex++] = (byte) len;
00145             System.arraycopy(temp, 0, bytes, startIndex, len);
00146             return startIndex + len;
00147         }
00148     }
00149 
00157     public static String bytesToString(byte[] bytes, int startIndex) {
00158         int len = (int) (bytes[startIndex++]) & 0xff;
00159         return new String(bytes, startIndex, len);
00160     }
00161 
00169     public static int longToBytes(long lnum, byte[] bytes, int startIndex) {
00170         for (int i = 0; i < 8; i++)
00171             bytes[startIndex + i] = (byte) ((lnum >> (i * 8)) & 0xff);
00172         return startIndex + 8;
00173     }
00174 
00182     public static long bytesToLong(byte[] bytes, int startIndex) {
00183         
00184         
00185         
00186         return (((long) bytes[startIndex] & 0xff) |
00187         (((long) bytes[startIndex + 1] & 0xff) << 8) |
00188         (((long) bytes[startIndex + 2] & 0xff) << 16) |
00189         (((long) bytes[startIndex + 3] & 0xff) << 24) |
00190         (((long) bytes[startIndex + 4] & 0xff) << 32) |
00191         (((long) bytes[startIndex + 5] & 0xff) << 40) |
00192         (((long) bytes[startIndex + 6] & 0xff) << 48) |
00193         (((long) bytes[startIndex + 7] & 0xff) << 56));
00194     }
00195 
00203     public static int doubleToBytes(double dnum, byte[] bytes, int startIndex) {
00204         return longToBytes(Double.doubleToLongBits(dnum), bytes, startIndex);
00205     }
00206 
00214     public static double bytesToDouble(byte[] bytes, int startIndex) {
00215         return Double.longBitsToDouble(bytesToLong(bytes, startIndex));
00216     }
00217 }