org/objectweb/proactive/mpi/control/util/ProActiveMPIUtil.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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         // the lower 4 bytes
00184         //      long temp = (long)bytesToInt(bytes, startIndex) & (long)0xffffffff;
00185         //return temp | ((long)bytesToInt(bytes, startIndex+4) << 32);
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 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1