Return-Path: X-Original-To: apmail-jackrabbit-commits-archive@www.apache.org Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7B94B4297 for ; Tue, 31 May 2011 07:40:59 +0000 (UTC) Received: (qmail 77331 invoked by uid 500); 31 May 2011 07:40:59 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 77245 invoked by uid 500); 31 May 2011 07:40:58 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 77238 invoked by uid 99); 31 May 2011 07:40:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 31 May 2011 07:40:58 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 31 May 2011 07:40:57 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 929E823888FD; Tue, 31 May 2011 07:40:37 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1129522 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java Date: Tue, 31 May 2011 07:40:37 -0000 To: commits@jackrabbit.apache.org From: thomasm@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110531074037.929E823888FD@eris.apache.org> Author: thomasm Date: Tue May 31 07:40:37 2011 New Revision: 1129522 URL: http://svn.apache.org/viewvc?rev=1129522&view=rev Log: String utilities for byte array to hex conversion. Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java?rev=1129522&view=auto ============================================================================== --- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java (added) +++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/StringUtils.java Tue May 31 07:40:37 2011 @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.mk.util; + +/** + * Some string utility methods. + */ +public class StringUtils { + + private static final char[] HEX = "0123456789abcdef".toCharArray(); + + /** + * Convert a byte array to a hex encoded string. + * + * @param value the byte array + * @param len the number of bytes to encode + * @return the hex encoded string + */ + public static String convertBytesToHex(byte[] value) { + int len = value.length; + char[] buff = new char[len + len]; + char[] hex = HEX; + for (int i = 0; i < len; i++) { + int c = value[i] & 0xff; + buff[i + i] = hex[c >> 4]; + buff[i + i + 1] = hex[c & 0xf]; + } + return new String(buff); + } + + /** + * Append a number as two hex digits to the string builder. + * + * @param buff the string builder + * @param x the number to append + */ + public static void appendHex(StringBuilder buff, int x) { + buff.append(HEX[x >> 4]).append(HEX[x & 0xf]); + } + + /** + * Convert a hex encoded string to a byte array. + * + * @param s the hex encoded string + * @return the byte array + */ + public static byte[] convertHexToBytes(String s) { + int len = s.length(); + if (len % 2 != 0) { + throw new IllegalArgumentException(s); + } + len /= 2; + byte[] buff = new byte[len]; + for (int i = 0; i < len; i++) { + buff[i] = (byte) ((getHexDigit(s, i + i) << 4) | getHexDigit(s, i + i + 1)); + } + return buff; + } + + /** + * Convert the digit at the given position to a hex number. + * + * @param s the hex encoded string + * @param i the index + * @return the number + */ + public static int getHexDigit(String s, int i) { + char c = s.charAt(i); + if (c >= '0' && c <= '9') { + return c - '0'; + } else if (c >= 'a' && c <= 'f') { + return c - 'a' + 0xa; + } else if (c >= 'A' && c <= 'F') { + return c - 'A' + 0xa; + } else { + throw new IllegalArgumentException(s); + } + } + +}