1 /* 2 * #%L 3 * EndianUtils.java - mongodb-async-driver - Allanbank Consulting, Inc. 4 * %% 5 * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc. 6 * %% 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * #L% 19 */ 20 package com.allanbank.mongodb.bson.io; 21 22 /** 23 * Utilities to deal with integer endian differences. 24 * 25 * @api.no This class is <b>NOT</b> part of the drivers API. This class may be 26 * mutated in incompatible ways between any two releases of the driver. 27 * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved 28 */ 29 public final class EndianUtils { 30 /** 31 * Performs a byte-order swap for a 32-bit signed integer. 32 * 33 * @param value 34 * The value to swap. 35 * @return The swapped value. 36 */ 37 public static int swap(final int value) { 38 return (((value << 24) & 0xFF000000) | ((value << 8) & 0x00FF0000) 39 | ((value >> 8) & 0x0000FF00) | ((value >> 24) & 0x000000FF)); 40 } 41 42 /** 43 * Performs a byte-order swap for a 64-bit signed integer. 44 * 45 * @param value 46 * The value to swap. 47 * @return The swapped value. 48 */ 49 public static long swap(final long value) { 50 return (((value << 56) & 0xFF00000000000000L) 51 | ((value << 40) & 0x00FF000000000000L) 52 | ((value << 24) & 0x0000FF0000000000L) 53 | ((value << 8) & 0x000000FF00000000L) 54 | ((value >> 8) & 0x00000000FF000000L) 55 | ((value >> 24) & 0x0000000000FF0000L) 56 | ((value >> 40) & 0x000000000000FF00L) | ((value >> 56) & 0x00000000000000FFL)); 57 } 58 59 }