/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2018 Yegor Bugayenko
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package org.cactoos.bytes;

import java.io.IOException;
import org.cactoos.Bytes;
import org.cactoos.Text;

/**
 * Decodes origin {@link Text} using the hexadecimal encoding scheme.
 *
 * @author Alexander Menshikov (sharplermc@gmail.com)
 * @version $Id$
 * @since 0.29
 * @checkstyle MagicNumberCheck (500 lines)
 */
public final class HexBytes implements Bytes {
    /**
     * Origin hexadecimal text.
     */
    private final Text origin;

    /**
     * Ctor.
     *
     * @param origin Hexadecimal text.
     */
    public HexBytes(final Text origin) {
        this.origin = origin;
    }

    @Override public byte[] asBytes() throws IOException {
        final String hex = this.origin.asString();
        if ((hex.length() & 1) == 1) {
            throw new IOException("Length of hexadecimal text is odd");
        }
        final byte[] result = new byte[hex.length() / 2];
        int index = 0;
        while (index < hex.length()) {
            final int most = checkedDigit(hex.charAt(index));
            final int less = checkedDigit(hex.charAt(index + 1));
            result[index >>> 1] = (byte) ((most << 4) + less);
            index += 2;
        }
        return result;
    }

    /**
     * Wrap around {@link Character#digit(char, int)}.
     *
     * @param character Hexadecimal character.
     * @return Numeric Value of the hex character.
     * @throws IOException If fails.
     */
    private static int checkedDigit(final char character) throws IOException {
        final int result = Character.digit(character, 16);
        if (result == -1) {
            throw new IOException(
                new StringBuilder()
                    .append("Unexpected character in hex string '")
                    .append(character).append('\'').toString()
            );
        }
        return result;
    }
}

@@ -1255,16 +1255,55 @@
 os.Text;
+%0Aimport org.cactoos.text.FormattedText;
 %0A%0A/**%0A *
@@ -1800,16 +1800,20 @@
 Override
+%0A   
  public 
@@ -2171,16 +2171,21 @@
  most = 
+this.
 checkedD
@@ -2238,16 +2238,21 @@
  less = 
+this.
 checkedD
@@ -2636,15 +2636,8 @@
 ate 
-static 
 int 
@@ -2838,23 +2838,22 @@
 new 
-StringBuilder()
+FormattedText(
 %0A   
@@ -2869,24 +2869,16 @@
         
-.append(
 %22Unexpec
@@ -2890,16 +2890,21 @@
 haracter
+ '%25c'
  in hex 
@@ -2911,18 +2911,21 @@
 string '
-%22)
+%25s'%22,
 %0A       
@@ -2937,24 +2937,16 @@
         
-.append(
 characte
@@ -2950,25 +2950,62 @@
 cter
-).append('%5C'').to
+,%0A                    this.origin%0A                ).as
 Stri

fix codestyle