~ubuntu-branches/ubuntu/maverick/libpgjava/maverick

« back to all changes in this revision

Viewing changes to org/postgresql/core/Encoding.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2008-04-26 22:01:11 UTC
  • mfrom: (3.1.4 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080426220111-yasgxtas5smx2qm3
Tags: 8.2-504-2
* Updated description to mention PostgreSQL 8.3 as supported.
  Closes: #398348
* Removed libpgjava transitional package. Closes: #477557
* Moved debhelper and cdbs from Build-Depends-Indep to Build-Depends.
* Added Homepage, Vcs-Svn and Vcs-Browser fields.
* Added watch file.
* Added myself to Uploaders.
* Removed Stafan and Wolfgang from Uploaders.
* Updated Standards-Version to 3.7.3
* Updated debhelper level to 5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
* Copyright (c) 2003-2005, PostgreSQL Global Development Group
4
4
*
5
5
* IDENTIFICATION
6
 
*   $PostgreSQL: pgjdbc/org/postgresql/core/Encoding.java,v 1.21 2005/07/04 02:18:32 oliver Exp $
 
6
*   $PostgreSQL: pgjdbc/org/postgresql/core/Encoding.java,v 1.22 2006/10/30 18:30:16 jurka Exp $
7
7
*
8
8
*-------------------------------------------------------------------------
9
9
*/
16
16
import java.io.OutputStreamWriter;
17
17
import java.io.Writer;
18
18
import java.io.IOException;
19
 
import java.util.Hashtable;
 
19
import java.util.HashMap;
20
20
 
21
21
/**
22
22
 * Representation of a particular character encoding.
28
28
    /*
29
29
     * Preferred JVM encodings for backend encodings.
30
30
     */
31
 
    private static final Hashtable encodings = new Hashtable();
 
31
    private static final HashMap encodings = new HashMap();
32
32
 
33
33
    static {
34
34
        //Note: this list should match the set of supported server
75
75
    }
76
76
 
77
77
    private final String encoding;
 
78
    private final boolean fastASCIINumbers;
78
79
 
79
80
    protected Encoding(String encoding)
80
81
    {
81
82
        this.encoding = encoding;
 
83
        fastASCIINumbers = testAsciiNumbers();
 
84
    }
 
85
    
 
86
    /**
 
87
     * Returns true if this encoding has characters
 
88
     * '-' and '0'..'9' in exactly same posision as ascii.
 
89
     *  
 
90
     * @return true if the bytes can be scanned directly for ascii numbers.
 
91
     */
 
92
    public boolean hasAsciiNumbers() {
 
93
        return fastASCIINumbers;
82
94
    }
83
95
 
84
96
    /**
115
127
        // encoding in the JVM we use that. Otherwise we fall back
116
128
        // to the default encoding of the JVM.
117
129
 
118
 
        if (encodings.containsKey(databaseEncoding))
 
130
        String[] candidates = (String[]) encodings.get(databaseEncoding);
 
131
        if (candidates != null)
119
132
        {
120
 
            String[] candidates = (String[]) encodings.get(databaseEncoding);
121
133
            for (int i = 0; i < candidates.length; i++)
122
134
            {
123
135
                if (isAvailable(candidates[i]))
254
266
    public String toString() {
255
267
        return (encoding == null ? "<default JVM encoding>" : encoding);
256
268
    }
 
269
    
 
270
    /**
 
271
     * Checks weather this encoding is compatible with ASCII for the number
 
272
     * characters '-' and '0'..'9'. Where compatible means that they are encoded
 
273
     * with exactly same values. 
 
274
     * 
 
275
     * @return If faster ASCII number parsing can be used with this encoding.
 
276
     */
 
277
    private boolean testAsciiNumbers() {
 
278
        // TODO: test all postgres supported encoding to see if there are
 
279
        // any which do _not_ have ascii numbers in same location
 
280
        // at least all the encoding listed in the encodings hashmap have
 
281
        // working ascii numbers
 
282
        try {
 
283
            String test = "-0123456789";
 
284
            byte[] bytes = encode(test);
 
285
            String res = new String(bytes, "US-ASCII");
 
286
            return test.equals(res);
 
287
        } catch (java.io.UnsupportedEncodingException e) {
 
288
            return false;
 
289
        } catch (IOException e) {
 
290
            return false;
 
291
        }
 
292
    }
257
293
}