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 | /**
|
---|
23 | * A set of static utility methods for reading the NTv2 file format
|
---|
24 | *
|
---|
25 | * @author Peter Yuill
|
---|
26 | */
|
---|
27 | public class NTV2Util {
|
---|
28 |
|
---|
29 | private NTV2Util() {
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Get a Little Endian int from four bytes of a byte array
|
---|
34 | * @param b the byte array
|
---|
35 | * @param i the index of the first data byte in the array
|
---|
36 | * @return the int
|
---|
37 | */
|
---|
38 | public static final int getIntLE(byte[] b, int i) {
|
---|
39 | return (b[i++] & 0x000000FF) | ((b[i++] << 8) & 0x0000FF00) | ((b[i++] << 16) & 0x00FF0000) | (b[i] << 24);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Get a Big Endian int from four bytes of a byte array
|
---|
44 | * @param b the byte array
|
---|
45 | * @param i the index of the first data byte in the array
|
---|
46 | * @return the int
|
---|
47 | */
|
---|
48 | public static final int getIntBE(byte[] b, int i) {
|
---|
49 | return (b[i++] << 24) | ((b[i++] << 16) & 0x00FF0000) | ((b[i++] << 8) & 0x0000FF00) | (b[i] & 0x000000FF);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Get an int from the first 4 bytes of a byte array,
|
---|
54 | * in either Big Endian or Little Endian format.
|
---|
55 | * @param b the byte array
|
---|
56 | * @param bigEndian is the byte array Big Endian?
|
---|
57 | * @return the int
|
---|
58 | */
|
---|
59 | public static final int getInt(byte[] b, boolean bigEndian) {
|
---|
60 | if (bigEndian)
|
---|
61 | return getIntBE(b, 0);
|
---|
62 | else
|
---|
63 | return getIntLE(b, 0);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Get a float from the first 4 bytes of a byte array,
|
---|
68 | * in either Big Endian or Little Endian format.
|
---|
69 | * @param b the byte array
|
---|
70 | * @param bigEndian is the byte array Big Endian?
|
---|
71 | * @return the float
|
---|
72 | */
|
---|
73 | public static final float getFloat(byte[] b, boolean bigEndian) {
|
---|
74 | int i = 0;
|
---|
75 | if (bigEndian) {
|
---|
76 | i = getIntBE(b, 0);
|
---|
77 | } else {
|
---|
78 | i = getIntLE(b, 0);
|
---|
79 | }
|
---|
80 | return Float.intBitsToFloat(i);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Get a double from the first 8 bytes of a byte array,
|
---|
85 | * in either Big Endian or Little Endian format.
|
---|
86 | * @param b the byte array
|
---|
87 | * @param bigEndian is the byte array Big Endian?
|
---|
88 | * @return the double
|
---|
89 | */
|
---|
90 | public static final double getDouble(byte[] b, boolean bigEndian) {
|
---|
91 | int i = 0;
|
---|
92 | int j = 0;
|
---|
93 | if (bigEndian) {
|
---|
94 | i = getIntBE(b, 0);
|
---|
95 | j = getIntBE(b, 4);
|
---|
96 | } else {
|
---|
97 | i = getIntLE(b, 4);
|
---|
98 | j = getIntLE(b, 0);
|
---|
99 | }
|
---|
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 | }
|
---|