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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2006-04-25 00:07:07 UTC
  • mfrom: (1.3.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20060425000707-6lr2s0awuz4z48hm
* Drop support for the old jdbc2 driver (can be reverted if wanted)
  (closes: #358345).
* New upstream (thanks to Wolfgang Baer).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
*
 
3
* Copyright (c) 2004-2005, PostgreSQL Global Development Group
 
4
* Copyright (c) 2004, Open Cloud Limited.
 
5
*
 
6
* IDENTIFICATION
 
7
*        $PostgreSQL: pgjdbc/org/postgresql/core/Utils.java,v 1.4 2005/01/11 08:25:43 jurka Exp $
 
8
*
 
9
*-------------------------------------------------------------------------
 
10
*/
 
11
 
 
12
package org.postgresql.core;
 
13
 
 
14
/**
 
15
 * Collection of utilities used by the protocol-level code.
 
16
 */
 
17
public class Utils {
 
18
    /**
 
19
     * Turn a bytearray into a printable form, representing
 
20
     * each byte in hex.
 
21
     *
 
22
     * @param data the bytearray to stringize
 
23
     * @return a hex-encoded printable representation of <code>data</code>
 
24
     */
 
25
    public static String toHexString(byte[] data) {
 
26
        StringBuffer sb = new StringBuffer(data.length * 2);
 
27
        for (int i = 0; i < data.length; ++i)
 
28
        {
 
29
            sb.append(Integer.toHexString((data[i] >> 4) & 15));
 
30
            sb.append(Integer.toHexString(data[i] & 15));
 
31
        }
 
32
        return sb.toString();
 
33
    }
 
34
 
 
35
    /**
 
36
     * Encode a string as UTF-8.
 
37
     *
 
38
     * @param str the string to encode
 
39
     * @return the UTF-8 representation of <code>str</code>
 
40
     */
 
41
    public static byte[] encodeUTF8(String str) {
 
42
        // It turns out that under 1.4.2, at least, calling getBytes() is
 
43
        // faster than rolling our own converter (it uses direct buffers and, I suspect,
 
44
        // a native helper -- and the cost of the encoding lookup is mitigated by a
 
45
        // ThreadLocal that caches the last lookup). So we just do things the simple way here.
 
46
        try
 
47
        {
 
48
            return str.getBytes("UTF-8");
 
49
        }
 
50
        catch (java.io.UnsupportedEncodingException e)
 
51
        {
 
52
            // Javadoc says that UTF-8 *must* be supported by all JVMs, so we don't try to be clever here.
 
53
            throw new RuntimeException("Unexpected exception: UTF-8 charset not supported: " + e);
 
54
        }
 
55
    }
 
56
}