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     */
020    package org.openstreetmap.josm.data.projection.datum;
021    
022    /**
023     * A set of static utility methods for reading the NTv2 file format
024     *
025     * @author Peter Yuill
026     */
027    public class NTV2Util {
028    
029        private NTV2Util() {
030        }
031    
032        /**
033         * Get a Little Endian int from four bytes of a byte array
034         * @param b the byte array
035         * @param i the index of the first data byte in the array
036         * @return the int
037         */
038        public static final int getIntLE(byte[] b, int i) {
039            return (b[i++] & 0x000000FF) | ((b[i++] << 8) & 0x0000FF00) | ((b[i++] << 16) & 0x00FF0000) | (b[i] << 24);
040        }
041    
042        /**
043         * Get a Big Endian int from four bytes of a byte array
044         * @param b the byte array
045         * @param i the index of the first data byte in the array
046         * @return the int
047         */
048        public static final int getIntBE(byte[] b, int i) {
049            return (b[i++] << 24) | ((b[i++] << 16) & 0x00FF0000) | ((b[i++] << 8) & 0x0000FF00) | (b[i] & 0x000000FF);
050        }
051    
052        /**
053         * Get an int from the first 4 bytes of a byte array,
054         * in either Big Endian or Little Endian format.
055         * @param b the byte array
056         * @param bigEndian is the byte array Big Endian?
057         * @return the int
058         */
059        public static final int getInt(byte[] b, boolean bigEndian) {
060            if (bigEndian)
061                return getIntBE(b, 0);
062            else
063                return getIntLE(b, 0);
064        }
065    
066        /**
067         * Get a float from the first 4 bytes of a byte array,
068         * in either Big Endian or Little Endian format.
069         * @param b the byte array
070         * @param bigEndian is the byte array Big Endian?
071         * @return the float
072         */
073        public static final float getFloat(byte[] b, boolean bigEndian) {
074            int i = 0;
075            if (bigEndian) {
076                i = getIntBE(b, 0);
077            } else {
078                i = getIntLE(b, 0);
079            }
080            return Float.intBitsToFloat(i);
081        }
082    
083        /**
084         * Get a double from the first 8 bytes of a byte array,
085         * in either Big Endian or Little Endian format.
086         * @param b the byte array
087         * @param bigEndian is the byte array Big Endian?
088         * @return the double
089         */
090        public static final double getDouble(byte[] b, boolean bigEndian) {
091            int i = 0;
092            int j = 0;
093            if (bigEndian) {
094                i = getIntBE(b, 0);
095                j = getIntBE(b, 4);
096            } else {
097                i = getIntLE(b, 4);
098                j = getIntLE(b, 0);
099            }
100            long l = ((long)i << 32) |
101            (j & 0x00000000FFFFFFFFL);
102            return Double.longBitsToDouble(l);
103        }
104    
105        /**
106         * Does the current VM support the New IO api
107         * @return true or false
108         */
109        public static boolean isNioAvailable() {
110            boolean nioAvailable = false;
111            try {
112                Class.forName("java.nio.channels.FileChannel");
113                nioAvailable = true;
114            } catch (ClassNotFoundException cnfe) {}
115            return nioAvailable;
116        }
117    }