001    /* JAPI - (Yet anothr (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.util;
022    
023    /** Class to convert data from little endian to big endian and vice versa.
024     * Since the conversion is symmetric, there are no special conversion methods, just generic ones, e.g. the same method is used to convert a little
025     * endian integer to a big endian integer and vice versa.
026     * @author <a href="mailto:Christian.Hujer@itcqis.com">Christian Hujer</a>
027     */
028    public class EndianConverter {
029    
030        /** Convert a double.
031         * This method changes byte order from 1, 2, 3, 4, 5, 6, 7, 8 to 8, 7, 6, 5, 4, 3, 2, 1.
032         * @param d double to convert
033         * @return converted double
034         */
035        public static final double se(final double d) {
036            return Double.longBitsToDouble(se(Double.doubleToRawLongBits(d)));
037        }
038    
039        /** Convert a float.
040         * This method changes byte order from 1, 2, 3, 4 to 4, 3, 2, 1.
041         * @param f float to convert
042         * @return converted float
043         */
044        public static final float se(final float f) {
045            return Float.intBitsToFloat(se(Float.floatToRawIntBits(f)));
046        }
047    
048        /** Convert a long.
049         * This method changes byte order from 1, 2, 3, 4, 5, 6, 7, 8 to 8, 7, 6, 5, 4, 3, 2, 1.
050         * @param l long to convert
051         * @return converted long
052         */
053        public static final long se(final long l) {
054            return
055                (l & 0x00000000000000FFl) <<  56 |
056                (l & 0x000000000000FF00l) <<  40 |
057                (l & 0x0000000000FF0000l) <<  24 |
058                (l & 0x00000000FF000000l) <<   8 |
059                (l & 0x000000FF00000000l) >>>  8 |
060                (l & 0x0000FF0000000000l) >>> 24 |
061                (l & 0x00FF000000000000l) >>> 40 |
062                (l & 0xFF00000000000000l) >>> 56;
063        }
064    
065        /** Convert an int.
066         * This method changes byte order from 1, 2, 3, 4 to 4, 3, 2, 1.
067         * @param i int to convert
068         * @return converted int
069         */
070        public static final int se(final int i) {
071            return
072                (i & 0x000000FF) <<  24 |
073                (i & 0x0000FF00) <<   8 |
074                (i & 0x00FF0000) >>>  8 |
075                (i & 0xFF000000) >>> 24;
076        }
077    
078        /** Convert a char.
079         * This method swaps low and high byte.
080         * @param c char to convert
081         * @return converted char
082         */
083        public static final char se(final char c) {
084            return (char)
085                ((c & 0x00FF) <<  8 |
086                 (c & 0xFF00) >>> 8);
087        }
088    
089        /** Convert a short.
090         * This method swaps low and high byte.
091         * @param s short to convert
092         * @return converted short
093         */
094        public static final short se(final short s) {
095            return (short)
096                ((s & 0x00FF) <<  8 |
097                 (s & 0xFF00) >>> 8);
098        }
099    
100        /** Convert a byte.
101         * This method does nothing and exists for convenience only.
102         * @param b byte to convert
103         * @return converted byte (same as b)
104         */
105        public static final byte se(final byte b) {
106            return b;
107        }
108    
109        /** Convert a boolean.
110         * This method does nothing and exists for convenience only.
111         * @param b boolean to convert
112         * @return converted boolean (same as b)
113         */
114        public static final boolean se(final boolean b) {
115            return b;
116        }
117    
118    } // class EndianConverter