001 /* JAPI - (Yet another (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:chris@riedquat.de">Christian Hujer</a>
027 */
028 @SuppressWarnings({"UtilityClass"})
029 public class EndianConverter {
030
031 /** Private constructor to prevent instantiation. */
032 private EndianConverter() {
033 }
034
035 /** Convert a double.
036 * This method changes byte order from 1, 2, 3, 4, 5, 6, 7, 8 to 8, 7, 6, 5, 4, 3, 2, 1.
037 * @param d double to convert
038 * @return converted double
039 */
040 public static final double swapEndianess(final double d) {
041 return Double.longBitsToDouble(swapEndianess(Double.doubleToRawLongBits(d)));
042 }
043
044 /** Convert a float.
045 * This method changes byte order from 1, 2, 3, 4 to 4, 3, 2, 1.
046 * @param f float to convert
047 * @return converted float
048 */
049 public static final float swapEndianess(final float f) {
050 return Float.intBitsToFloat(swapEndianess(Float.floatToRawIntBits(f)));
051 }
052
053 /** Convert a long.
054 * This method changes byte order from 1, 2, 3, 4, 5, 6, 7, 8 to 8, 7, 6, 5, 4, 3, 2, 1.
055 * @param l long to convert
056 * @return converted long
057 */
058 @SuppressWarnings("OverlyComplexBooleanExpression")
059 public static final long swapEndianess(final long l) {
060 return
061 (l & 0x00000000000000FFL) << 56 |
062 (l & 0x000000000000FF00L) << 40 |
063 (l & 0x0000000000FF0000L) << 24 |
064 (l & 0x00000000FF000000L) << 8 |
065 (l & 0x000000FF00000000L) >>> 8 |
066 (l & 0x0000FF0000000000L) >>> 24 |
067 (l & 0x00FF000000000000L) >>> 40 |
068 (l & 0xFF00000000000000L) >>> 56;
069 }
070
071 /** Convert an int.
072 * This method changes byte order from 1, 2, 3, 4 to 4, 3, 2, 1.
073 * @param i int to convert
074 * @return converted int
075 */
076 @SuppressWarnings("OverlyComplexBooleanExpression")
077 public static final int swapEndianess(final int i) {
078 return
079 (i & 0x000000FF) << 24 |
080 (i & 0x0000FF00) << 8 |
081 (i & 0x00FF0000) >>> 8 |
082 (i & 0xFF000000) >>> 24;
083 }
084
085 /** Convert a char.
086 * This method swaps low and high byte.
087 * @param c char to convert
088 * @return converted char
089 */
090 public static final char swapEndianess(final char c) {
091 return (char)
092 ((c & 0x00FF) << 8 |
093 (c & 0xFF00) >>> 8);
094 }
095
096 /** Convert a short.
097 * This method swaps low and high byte.
098 * @param s short to convert
099 * @return converted short
100 */
101 public static final short swapEndianess(final short s) {
102 return (short)
103 ((s & 0x00FF) << 8 |
104 (s & 0xFF00) >>> 8);
105 }
106
107 /** Convert a byte.
108 * This method does nothing and exists for convenience only.
109 * @param b byte to convert
110 * @return converted byte (same as b)
111 */
112 public static final byte swapEndianess(final byte b) {
113 return b;
114 }
115
116 /** Convert a boolean.
117 * This method does nothing and exists for convenience only.
118 * @param b boolean to convert
119 * @return converted boolean (same as b)
120 */
121 @SuppressWarnings({"BooleanMethodNameMustStartWithQuestion"})
122 public static final boolean swapEndianess(final boolean b) {
123 return b;
124 }
125
126 } // class EndianConverter