package dk.statsbiblioteket.util;

import dk.statsbiblioteket.util.qa.QAInfo;

/** Utillity class for working with bytes, bytearrays, and strings. */
@QAInfo(level = QAInfo.Level.NORMAL,
        state = QAInfo.State.QA_OK,
        author = "kfc",
        reviewers = {"abr"})
public class ByteString {
    /** The number of bits in a nibble (used for shifting). */
    private static final byte BITS_IN_NIBBLE = 4;
    /** A bitmask for a nibble (used for "and'ing" out the bits. */
    private static final byte BITMASK_FOR_NIBBLE = 0x0f;

    /** Utility class, don't initialise. */
    private ByteString() {
    }

    /**
     * Converts a byte array to a hexstring.
     *
     * @param ba the bytearray to be converted
     * @return ba converted to a hexstring
     */
    public static String toHex(final byte[] ba) {
        char[] hexdigit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};

        int baLen = ba.length;
        StringBuffer sb = new StringBuffer(baLen * 2);

        for (int i = 0; i < baLen; i++) {
            sb.append(hexdigit[(ba[i] >> BITS_IN_NIBBLE) & BITMASK_FOR_NIBBLE]);
            sb.append(hexdigit[ba[i] & BITMASK_FOR_NIBBLE]);
        }

        return sb.toString();
    }


}

@@ -72,18 +72,50 @@
 AInfo;%0A%0A
-/*
+import java.io.IOException;%0A%0A/**%0A 
 * Utilli
@@ -171,16 +171,17 @@
 strings.
+%0A
  */%0A@QAI
@@ -246,13 +246,22 @@
 ate.
-QA_OK
+IN_DEVELOPMENT
 ,%0A  
@@ -279,16 +279,21 @@
 r = %22kfc
+, mke
 %22,%0A     
@@ -312,16 +312,21 @@
  = %7B%22abr
+, mke
 %22%7D)%0Apubl
@@ -347,16 +347,255 @@
 tring %7B%0A
+    /**%0A     * The hexidecimal characters in numeric order from %7B@code 0-f%7D%0A     */%0A    public static final char%5B%5D HEX_DIGITS =%0A            %7B'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',%0A                'a', 'b', 'c', 'd', 'e', 'f'%7D;%0A%0A
     /** 
@@ -944,24 +944,25 @@
 ray to a hex
+-
 string.%0A    
@@ -1029,25 +1029,41 @@
 turn ba 
+the byte array to 
 convert
-ed
  to a he
@@ -1063,16 +1063,17 @@
 to a hex
+-
 string%0A 
@@ -1141,211 +1141,841 @@
     
-char%5B%5D hexdigit = %7B'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',%0A                'a', 'b', 'c', 'd', 'e', 'f'%7D;%0A%0A        int baLen = ba.length;%0A        StringBuffer sb = new StringBuffer(baLen * 2);%0A
+StringBuilder sb = new StringBuilder(ba.length * 2);%0A        try %7B%0A            toHex(sb, ba);%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(%0A                    %22Unexpected IOException while appending to StringBuilder%22);%0A        %7D%0A%0A        return sb.toString();%0A    %7D%0A%0A    /**%0A     * Converts a byte array to a hex-string and write the result to an%0A     * %7B@code Appendable%7D (such as a %7B@code StringBuilder%7D).%0A     *%0A     * @param buf the appendable to write to%0A     * @param ba the byte array to convert to a hex-string%0A     * @return always returns %7B@code buf%7D%0A     * @throws IOException upon errors writing to %7B@code buf%7D%0A     */%0A    public static Appendable toHex(Appendable buf, byte%5B%5D ba)%0A                                                            throws IOException %7B%0A        int baLen = ba.length;
 %0A   
@@ -2021,34 +2021,35 @@
             
-s
 b
+uf
 .append(
 hexdigit%5B(ba
@@ -2036,24 +2036,47 @@
 .append(
-hexdigit
+%0A                    HEX_DIGITS
 %5B(ba%5Bi%5D 
@@ -2134,18 +2134,19 @@
     
-s
 b
+uf
 .append(
 hexd
@@ -2145,16 +2145,18 @@
 end(
-hexdigit
+HEX_DIGITS
 %5Bba%5B
@@ -2204,37 +2204,27 @@
      return 
-sb.toString()
+buf
 ;%0A    %7D%0A%0A%0A%7D%0A

Implement a version of ByteString.toHex() that takes an Appendable as argument.