1
/*-------------------------------------------------------------------------
3
* Copyright (c) 2004-2005, PostgreSQL Global Development Group
4
* Copyright (c) 2004, Open Cloud Limited.
7
* $PostgreSQL: pgjdbc/org/postgresql/core/Utils.java,v 1.4 2005/01/11 08:25:43 jurka Exp $
9
*-------------------------------------------------------------------------
12
package org.postgresql.core;
15
* Collection of utilities used by the protocol-level code.
19
* Turn a bytearray into a printable form, representing
22
* @param data the bytearray to stringize
23
* @return a hex-encoded printable representation of <code>data</code>
25
public static String toHexString(byte[] data) {
26
StringBuffer sb = new StringBuffer(data.length * 2);
27
for (int i = 0; i < data.length; ++i)
29
sb.append(Integer.toHexString((data[i] >> 4) & 15));
30
sb.append(Integer.toHexString(data[i] & 15));
36
* Encode a string as UTF-8.
38
* @param str the string to encode
39
* @return the UTF-8 representation of <code>str</code>
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.
48
return str.getBytes("UTF-8");
50
catch (java.io.UnsupportedEncodingException e)
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);