/*
 * HexToBytes.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This filter converts a hex string into a stream of bytes
 */
 
import java.math.*;
import java.io.*;
class HexToBytes extends FilterOutputStream
{
  public HexToBytes (OutputStream stream)
  {
    super (stream);
  }
  
  public void write (String s)
    throws IOException
  {
    // Convert hex string to array of bytes
    byte [] buffer = new byte [s.length() / 2];
    for (int i = 0; i < buffer.length; ++i)
    {
      buffer [i] = (byte) (Character.digit (s.charAt (i * 2), 16) * 16 + Character.digit (s.charAt (i * 2 + 1), 16));
    }
    
    // Output the completed buffer
    out.write (buffer);
  }
}
