1 | /*
|
---|
2 | * Copyright (c) 2003 Objectix Pty Ltd All rights reserved.
|
---|
3 | *
|
---|
4 | * This library is free software; you can redistribute it and/or
|
---|
5 | * modify it under the terms of the GNU Lesser General Public
|
---|
6 | * License as published by the Free Software Foundation.
|
---|
7 | *
|
---|
8 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
|
---|
9 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
10 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
11 | * DISCLAIMED. IN NO EVENT SHALL OBJECTIX PTY LTD BE LIABLE FOR ANY
|
---|
12 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
13 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
---|
14 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
15 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
---|
16 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
---|
17 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
---|
18 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
19 | */
|
---|
20 | package org.openstreetmap.josm.data.projection.datum;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * A set of static utility methods for reading the NTv2 file format
|
---|
26 | *
|
---|
27 | * @author Peter Yuill
|
---|
28 | */
|
---|
29 | public final class NTV2Util {
|
---|
30 |
|
---|
31 | private NTV2Util() {
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Get a Little Endian int from four bytes of a byte array
|
---|
36 | * @param b the byte array
|
---|
37 | * @param i the index of the first data byte in the array
|
---|
38 | * @return the int
|
---|
39 | */
|
---|
40 | public static int getIntLE(byte[] b, int i) {
|
---|
41 | return (b[i++] & 0x000000FF) | ((b[i++] << 8) & 0x0000FF00) | ((b[i++] << 16) & 0x00FF0000) | (b[i] << 24);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Get a Big Endian int from four bytes of a byte array
|
---|
46 | * @param b the byte array
|
---|
47 | * @param i the index of the first data byte in the array
|
---|
48 | * @return the int
|
---|
49 | */
|
---|
50 | public static int getIntBE(byte[] b, int i) {
|
---|
51 | return (b[i++] << 24) | ((b[i++] << 16) & 0x00FF0000) | ((b[i++] << 8) & 0x0000FF00) | (b[i] & 0x000000FF);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Get an int from the first 4 bytes of a byte array,
|
---|
56 | * in either Big Endian or Little Endian format.
|
---|
57 | * @param b the byte array
|
---|
58 | * @param bigEndian is the byte array Big Endian?
|
---|
59 | * @return the int
|
---|
60 | */
|
---|
61 | public static int getInt(byte[] b, boolean bigEndian) {
|
---|
62 | if (bigEndian)
|
---|
63 | return getIntBE(b, 0);
|
---|
64 | else
|
---|
65 | return getIntLE(b, 0);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Get a float from the first 4 bytes of a byte array,
|
---|
70 | * in either Big Endian or Little Endian format.
|
---|
71 | * @param b the byte array
|
---|
72 | * @param bigEndian is the byte array Big Endian?
|
---|
73 | * @return the float
|
---|
74 | */
|
---|
75 | public static float getFloat(byte[] b, boolean bigEndian) {
|
---|
76 | int i = 0;
|
---|
77 | if (bigEndian) {
|
---|
78 | i = getIntBE(b, 0);
|
---|
79 | } else {
|
---|
80 | i = getIntLE(b, 0);
|
---|
81 | }
|
---|
82 | return Float.intBitsToFloat(i);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Get a double from the first 8 bytes of a byte array,
|
---|
87 | * in either Big Endian or Little Endian format.
|
---|
88 | * @param b the byte array
|
---|
89 | * @param bigEndian is the byte array Big Endian?
|
---|
90 | * @return the double
|
---|
91 | */
|
---|
92 | public static double getDouble(byte[] b, boolean bigEndian) {
|
---|
93 | int i = 0;
|
---|
94 | int j = 0;
|
---|
95 | if (bigEndian) {
|
---|
96 | i = getIntBE(b, 0);
|
---|
97 | j = getIntBE(b, 4);
|
---|
98 | } else {
|
---|
99 | 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 | }
|
---|