001/* 002 * Copyright (c) 2003 Objectix Pty Ltd All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License as published by the Free Software Foundation. 007 * 008 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 009 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 010 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 011 * DISCLAIMED. IN NO EVENT SHALL OBJECTIX PTY LTD BE LIABLE FOR ANY 012 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 013 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 014 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 015 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 016 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 017 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 018 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 019 */ 020package org.openstreetmap.josm.data.projection.datum; 021 022import org.openstreetmap.josm.Main; 023 024/** 025 * A set of static utility methods for reading the NTv2 file format 026 * 027 * @author Peter Yuill 028 */ 029public final class NTV2Util { 030 031 private NTV2Util() { 032 } 033 034 /** 035 * Get a Little Endian int from four bytes of a byte array 036 * @param b the byte array 037 * @param i the index of the first data byte in the array 038 * @return the int 039 */ 040 public static final int getIntLE(byte[] b, int i) { 041 return (b[i++] & 0x000000FF) | ((b[i++] << 8) & 0x0000FF00) | ((b[i++] << 16) & 0x00FF0000) | (b[i] << 24); 042 } 043 044 /** 045 * Get a Big Endian int from four bytes of a byte array 046 * @param b the byte array 047 * @param i the index of the first data byte in the array 048 * @return the int 049 */ 050 public static final int getIntBE(byte[] b, int i) { 051 return (b[i++] << 24) | ((b[i++] << 16) & 0x00FF0000) | ((b[i++] << 8) & 0x0000FF00) | (b[i] & 0x000000FF); 052 } 053 054 /** 055 * Get an int from the first 4 bytes of a byte array, 056 * in either Big Endian or Little Endian format. 057 * @param b the byte array 058 * @param bigEndian is the byte array Big Endian? 059 * @return the int 060 */ 061 public static final int getInt(byte[] b, boolean bigEndian) { 062 if (bigEndian) 063 return getIntBE(b, 0); 064 else 065 return getIntLE(b, 0); 066 } 067 068 /** 069 * Get a float from the first 4 bytes of a byte array, 070 * in either Big Endian or Little Endian format. 071 * @param b the byte array 072 * @param bigEndian is the byte array Big Endian? 073 * @return the float 074 */ 075 public static final float getFloat(byte[] b, boolean bigEndian) { 076 int i = 0; 077 if (bigEndian) { 078 i = getIntBE(b, 0); 079 } else { 080 i = getIntLE(b, 0); 081 } 082 return Float.intBitsToFloat(i); 083 } 084 085 /** 086 * Get a double from the first 8 bytes of a byte array, 087 * in either Big Endian or Little Endian format. 088 * @param b the byte array 089 * @param bigEndian is the byte array Big Endian? 090 * @return the double 091 */ 092 public static final double getDouble(byte[] b, boolean bigEndian) { 093 int i = 0; 094 int j = 0; 095 if (bigEndian) { 096 i = getIntBE(b, 0); 097 j = getIntBE(b, 4); 098 } else { 099 i = getIntLE(b, 4); 100 j = getIntLE(b, 0); 101 } 102 long l = ((long)i << 32) | 103 (j & 0x00000000FFFFFFFFL); 104 return Double.longBitsToDouble(l); 105 } 106 107 /** 108 * Does the current VM support the New IO api 109 * @return true or false 110 */ 111 public static boolean isNioAvailable() { 112 boolean nioAvailable = false; 113 try { 114 Class.forName("java.nio.channels.FileChannel"); 115 nioAvailable = true; 116 } catch (NoClassDefFoundError | ClassNotFoundException cnfe) { 117 Main.info(cnfe.getMessage()); 118 } 119 return nioAvailable; 120 } 121}