Class ByteOrderValues

  1 /*
  2  * Copyright (c) 2016 Vivid Solutions.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.io;
 13  
 14 /**
 15  * Methods to read and write primitive Java datatypes from/to byte
 16  * sequences, allowing the byte order to be specified
 17  * <p>
 18  * Similar to the standard Java <code>ByteBuffer</code> class.
 19  */
 20 public class ByteOrderValues
 21 {
 22   public static final int BIG_ENDIAN = 1;
 23   public static final int LITTLE_ENDIAN = 2;
 24  
 25   public static int getInt(byte[] buf, int byteOrder)
 26   {
 27     if (byteOrder == BIG_ENDIAN) {
 28       return  ( (buf[0] & 0xff) << 24)
 29             | ( (buf[1] & 0xff) << 16)
 30             | ( (buf[2] & 0xff) << 8)
 31             | ( (buf[3] & 0xff) );
 32     }
 33     else {// LITTLE_ENDIAN
 34       return  ( (buf[3] & 0xff) << 24)
 35             | ( (buf[2] & 0xff) << 16)
 36             | ( (buf[1] & 0xff) << 8)
 37             | ( (buf[0] & 0xff) );
 38     }
 39   }
 40  
 41   public static void putInt(int intValue, byte[] buf, int byteOrder)
 42   {
 43     if (byteOrder == BIG_ENDIAN) {
 44       buf[0] = (byte)(intValue >> 24);
 45       buf[1] = (byte)(intValue >> 16);
 46       buf[2] = (byte)(intValue >> 8);
 47       buf[3] = (byte) intValue;
 48     }
 49     else {// LITTLE_ENDIAN
 50       buf[0] = (byte) intValue;
 51       buf[1] = (byte)(intValue >> 8);
 52       buf[2] = (byte)(intValue >> 16);
 53       buf[3] = (byte)(intValue >> 24);
 54     }
 55   }
 56   public static long getLong(byte[] buf, int byteOrder)
 57   {
 58     if (byteOrder == BIG_ENDIAN) {
 59       return
 60             (long) (buf[0] & 0xff) << 56
 61           | (long) (buf[1] & 0xff) << 48
 62           | (long) (buf[2] & 0xff) << 40
 63           | (long) (buf[3] & 0xff) << 32
 64           | (long) (buf[4] & 0xff) << 24
 65           | (long) (buf[5] & 0xff) << 16
 66           | (long) (buf[6] & 0xff) <<  8
 67           | (long) (buf[7] & 0xff);
 68     }
 69     else {// LITTLE_ENDIAN
 70       return
 71             (long) (buf[7] & 0xff) << 56
 72           | (long) (buf[6] & 0xff) << 48
 73           | (long) (buf[5] & 0xff) << 40
 74           | (long) (buf[4] & 0xff) << 32
 75           | (long) (buf[3] & 0xff) << 24
 76           | (long) (buf[2] & 0xff) << 16
 77           | (long) (buf[1] & 0xff) <<  8
 78           | (long) (buf[0] & 0xff);
 79     }
 80   }
 81  
 82   public static void putLong(long longValue, byte[] buf, int byteOrder)
 83   {
 84     if (byteOrder == BIG_ENDIAN) {
 85       buf[0] = (byte)(longValue >> 56);
 86       buf[1] = (byte)(longValue >> 48);
 87       buf[2] = (byte)(longValue >> 40);
 88       buf[3] = (byte)(longValue >> 32);
 89       buf[4] = (byte)(longValue >> 24);
 90       buf[5] = (byte)(longValue >> 16);
 91       buf[6] = (byte)(longValue >> 8);
 92       buf[7] = (byte) longValue;
 93     }
 94     else {  // LITTLE_ENDIAN
 95       buf[0] = (byte) longValue;
 96       buf[1] = (byte)(longValue >> 8);
 97       buf[2] = (byte)(longValue >> 16);
 98       buf[3] = (byte)(longValue >> 24);
 99       buf[4] = (byte)(longValue >> 32);
100       buf[5] = (byte)(longValue >> 40);
101       buf[6] = (byte)(longValue >> 48);
102       buf[7] = (byte)(longValue >> 56);
103     }
104   }
105  
106   public static double getDouble(byte[] buf, int byteOrder)
107   {
108     long longVal = getLong(buf, byteOrder);
109     return Double.longBitsToDouble(longVal);
110   }
111  
112   public static void putDouble(double doubleValue, byte[] buf, int byteOrder)
113   {
114     long longVal = Double.doubleToLongBits(doubleValue);
115     putLong(longVal, buf, byteOrder);
116   }
117  
118 }
119