4
* PostGIS extension for PostgreSQL JDBC driver - Binary Parser
6
* (C) 2005 Markus Schaber, markus@schabi.de
8
* This library is free software; you can redistribute it and/or modify it under
9
* the terms of the GNU Lesser General Public License as published by the Free
10
* Software Foundation, either version 2.1 of the License.
12
* This library is distributed in the hope that it will be useful, but WITHOUT
13
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17
* You should have received a copy of the GNU Lesser General Public License
18
* along with this library; if not, write to the Free Software Foundation, Inc.,
19
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit the web at
22
* $Id: ByteGetter.java,v 1.3 2005/03/30 15:24:40 mschaber Exp $
25
package org.postgis.binary;
27
public abstract class ByteGetter {
31
* @return The result is returned as Int to eliminate sign problems when
32
* or'ing several values together.
34
public abstract int get(int index);
36
public static class BinaryByteGetter extends ByteGetter {
39
public BinaryByteGetter(byte[] array) {
43
public int get(int index) {
44
return array[index] & 0xFF; //mask out sign-extended bits.
48
public static class StringByteGetter extends ByteGetter {
51
public StringByteGetter(String rep) {
55
public int get(int index) {
57
int high = unhex(rep.charAt(index));
58
int low = unhex(rep.charAt(index + 1));
59
return (high << 4) + low;
62
public static byte unhex(char c) {
63
if (c >= '0' && c <= '9') {
64
return (byte) (c - '0');
65
} else if (c >= 'A' && c <= 'F') {
66
return (byte) (c - 'A' + 10);
67
} else if (c >= 'a' && c <= 'f') {
68
return (byte) (c - 'a' + 10);
70
throw new IllegalArgumentException("No valid Hex char " + c);
b'\\ No newline at end of file'