001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019
020package org.apache.wiki.util;
021
022/**
023 *  A collection of static byte utility methods.
024 *  
025 * @author Ichiro Furusato
026 */
027public class ByteUtils
028{
029    private static final char[] hexArray = "0123456789abcdef".toCharArray();
030    
031    
032    /**
033     *  byte[] array to hex conversion. Note that this provides
034     *  no delimiters; the bytes are simply concatenated.
035     */
036    public static String bytes2hex(final byte[] bytes )
037    {
038        final char[] ca = new char[bytes.length * 2];
039        for ( int i = 0; i < bytes.length; i++ ) {
040            final int v = bytes[i] & 0xff;
041            ca[i * 2] = hexArray[v >>> 4];
042            ca[i * 2 + 1] = hexArray[v & 0x0f];
043        }
044        return new String(ca);
045    }
046    
047    
048    /**
049     *  byte to hex conversion.
050     */
051    public static String byte2hex(final byte b )
052    {
053        return Integer.toHexString(b & 0xff);
054    }
055
056    
057    /**
058     *  Parses a hexadecimal string into its corresponding bytes.
059     */
060    public static byte[] parseHexBinary(final String hex )
061    {
062        final int len = hex.length();
063        // e.g., "111" is not a valid hex encoding.
064        if( len%2 != 0 ) {
065            throw new IllegalArgumentException("hexBinary needs to be even-length: "+hex);
066        }
067        final byte[] out = new byte[len/2];
068        for( int i = 0; i < len; i+=2 ) {
069            final int h = hexToBin(hex.charAt(i));
070            final int l = hexToBin(hex.charAt(i+1));
071            if ( h==-1 || l==-1 ) {
072                throw new IllegalArgumentException("contains illegal character for hexBinary: "+hex);
073            }
074            out[i/2] = (byte)(h*16+l);
075        }
076        return out;
077    }
078    
079    
080    /**
081     *  Converts a single hex character into its integer equivalent.
082     */
083    public static int hexToBin(final char c )
084    {
085        if ( '0'<=c && c<='9' ) return c-'0';
086        if ( 'A'<=c && c<='F' ) return c-'A'+10;
087        if ( 'a'<=c && c<='f' ) return c-'a'+10;
088        return -1;
089    }
090
091    
092    private ByteUtils() {}
093
094}