~ubuntu-branches/ubuntu/feisty/postgis/feisty

« back to all changes in this revision

Viewing changes to jdbc2/src/org/postgis/binary/ByteGetter.java

  • Committer: Bazaar Package Importer
  • Author(s): Alex Bodnaru
  • Date: 2005-05-05 10:02:45 UTC
  • Revision ID: james.westby@ubuntu.com-20050505100245-3005l6jn1jwvpsrw
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ByteGetter.java
 
3
 * 
 
4
 * PostGIS extension for PostgreSQL JDBC driver - Binary Parser
 
5
 * 
 
6
 * (C) 2005 Markus Schaber, markus@schabi.de
 
7
 * 
 
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.
 
11
 * 
 
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
 
15
 * details.
 
16
 * 
 
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
 
20
 * http://www.gnu.org.
 
21
 * 
 
22
 * $Id: ByteGetter.java,v 1.3 2005/03/30 15:24:40 mschaber Exp $
 
23
 */
 
24
 
 
25
package org.postgis.binary;
 
26
 
 
27
public abstract class ByteGetter {
 
28
    /**
 
29
     * Get ab byte.
 
30
     * 
 
31
     * @return The result is returned as Int to eliminate sign problems when
 
32
     *         or'ing several values together.
 
33
     */
 
34
    public abstract int get(int index);
 
35
 
 
36
    public static class BinaryByteGetter extends ByteGetter {
 
37
        private byte[] array;
 
38
 
 
39
        public BinaryByteGetter(byte[] array) {
 
40
            this.array = array;
 
41
        }
 
42
 
 
43
        public int get(int index) {
 
44
            return array[index] & 0xFF; //mask out sign-extended bits.
 
45
        }
 
46
    }
 
47
 
 
48
    public static class StringByteGetter extends ByteGetter {
 
49
        private String rep;
 
50
 
 
51
        public StringByteGetter(String rep) {
 
52
            this.rep = rep;
 
53
        }
 
54
 
 
55
        public int get(int index) {
 
56
            index *= 2;
 
57
            int high = unhex(rep.charAt(index));
 
58
            int low = unhex(rep.charAt(index + 1));
 
59
            return (high << 4) + low;
 
60
        }
 
61
 
 
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);
 
69
            } else {
 
70
                throw new IllegalArgumentException("No valid Hex char " + c);
 
71
            }
 
72
        }
 
73
    }
 
74
}
 
 
b'\\ No newline at end of file'