// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.util.encoders;

import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import org.bouncycastle.util.Strings;

public class Hex
{
    private static final HexEncoder encoder;
    
    public static String toHexString(final byte[] array) {
        return toHexString(array, 0, array.length);
    }
    
    public static String toHexString(final byte[] array, final int n, final int n2) {
        return Strings.fromByteArray(encode(array, n, n2));
    }
    
    public static byte[] encode(final byte[] array) {
        return encode(array, 0, array.length);
    }
    
    public static byte[] encode(final byte[] array, final int n, final int n2) {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            Hex.encoder.encode(array, n, n2, byteArrayOutputStream);
        }
        catch (final Exception ex) {
            throw new EncoderException("exception encoding Hex string: " + ex.getMessage(), ex);
        }
        return byteArrayOutputStream.toByteArray();
    }
    
    public static int encode(final byte[] array, final OutputStream outputStream) throws IOException {
        return Hex.encoder.encode(array, 0, array.length, outputStream);
    }
    
    public static int encode(final byte[] array, final int n, final int n2, final OutputStream outputStream) throws IOException {
        return Hex.encoder.encode(array, n, n2, outputStream);
    }
    
    public static byte[] decode(final byte[] array) {
        return decode(array, 0, array.length);
    }
    
    public static byte[] decode(final byte[] array, final int n, final int n2) {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(n2 / 2);
        try {
            Hex.encoder.decode(array, n, n2, byteArrayOutputStream);
        }
        catch (final Exception ex) {
            throw new DecoderException("exception decoding Hex data: " + ex.getMessage(), ex);
        }
        return byteArrayOutputStream.toByteArray();
    }
    
    public static byte[] decode(final String s) {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            Hex.encoder.decode(s, byteArrayOutputStream);
        }
        catch (final Exception ex) {
            throw new DecoderException("exception decoding Hex string: " + ex.getMessage(), ex);
        }
        return byteArrayOutputStream.toByteArray();
    }
    
    public static int decode(final String s, final OutputStream outputStream) throws IOException {
        return Hex.encoder.decode(s, outputStream);
    }
    
    public static byte[] decodeStrict(final String s) {
        try {
            return Hex.encoder.decodeStrict(s, 0, s.length());
        }
        catch (final Exception ex) {
            throw new DecoderException("exception decoding Hex string: " + ex.getMessage(), ex);
        }
    }
    
    public static byte[] decodeStrict(final String s, final int n, final int n2) {
        try {
            return Hex.encoder.decodeStrict(s, n, n2);
        }
        catch (final Exception ex) {
            throw new DecoderException("exception decoding Hex string: " + ex.getMessage(), ex);
        }
    }
    
    static {
        encoder = new HexEncoder();
    }
}
