/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/*
 * TCP Sampler Client implementation which reads and writes binary data.  
 * 
 * Input/Output strings are passed as hex-encoded binary strings.
 *
 */
package org.apache.jmeter.protocol.tcp.sampler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.SocketTimeoutException;

import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;

/**
 * TCPClient implementation.
 * Reads data until the defined EOM byte is reached.
 * If there is no EOM byte defined, then reads until
 * the end of the stream is reached.
 * The EOM byte is defined by the property "tcp.BinaryTCPClient.eomByte".
 * 
 * Input data is assumed to be in hex, and is converted to binary
 */
public class BinaryTCPClientImpl extends AbstractTCPClient {
    private static final Logger log = LoggingManager.getLoggerForClass();

    private int eomInt = JMeterUtils.getPropDefault("tcp.BinaryTCPClient.eomByte", 1000); // $NON_NLS-1$

    // End of message byte (defaults to none)
    private byte eomByte = (byte) eomInt; // -128 to +127

    private boolean eomIgnore = eomInt < -128 || eomInt > 127;
    
    public BinaryTCPClientImpl() {
        super();
        if (!eomIgnore) {
            log.info("Using eomByte=" + eomByte);
        }
    }

    /**
     * Convert hex string to binary byte array.
     * 
     * @param s - hex-encoded binary string
     * @return Byte array containing binary representation of input hex-encoded string 
     */
    public static final byte[] hexStringToByteArray(String s) {
        if (s.length() % 2 == 0) {
            char[] sc = s.toCharArray();
            byte[] ba = new byte[sc.length / 2];

            for (int i = 0; i < ba.length; i++) {
                int nibble0 = Character.digit(sc[i * 2], 16);
                int nibble1 = Character.digit(sc[i * 2 + 1], 16);
                ba[i] = (byte) ((nibble0 << 4) | (nibble1));
            }

            return ba;
        } else {
            throw new IllegalArgumentException(
                    "Hex-encoded binary string contains an uneven no. of digits");
        }
    }

    /**
     * Input (hex) string is converted to binary and written to the output stream.
     * @param os output stream
     * @param hexEncodedBinary hex-encoded binary
     */
    public void write(OutputStream os, String hexEncodedBinary) {
        try {
            os.write(hexStringToByteArray(hexEncodedBinary));
            os.flush();
        } catch (IOException e) {
            log.warn("Write error", e);
        }
        log.debug("Wrote: " + hexEncodedBinary);
        return;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.jmeter.protocol.tcp.sampler.TCPClient#write(java.io.OutputStream
     * , java.io.InputStream)
     */
    public void write(OutputStream os, InputStream is) {
        throw new UnsupportedOperationException(
                "Method not supported for Length-Prefixed data.");
    }

    /**
     * Reads data until the defined EOM byte is reached.
     * If there is no EOM byte defined, then reads until
     * the end of the stream is reached.
     * Response data is converted to hex-encoded binary
     * @return hex-encoded binary string
     */
    public String read(InputStream is) {
        byte[] buffer = new byte[4096];
        ByteArrayOutputStream w = new ByteArrayOutputStream();
        int x = 0;
        try {
            while ((x = is.read(buffer)) > -1) {
                w.write(buffer, 0, x);
                if (!eomIgnore && (buffer[x - 1] == eomByte)) {
                    break;
                }
            }
            /*
             * Timeout is reported as follows: JDK1.3: InterruptedIOException
             * JDK1.4: SocketTimeoutException, which extends
             * InterruptedIOException
             *
             * So to make the code work on both, just check for
             * InterruptedIOException
             *
             * If 1.3 support is dropped, can change to using
             * SocketTimeoutException
             *
             * For more accurate detection of timeouts under 1.3, one could
             * perhaps examine the Exception message text...
             *
             */
        } catch (SocketTimeoutException e) {
            // drop out to handle buffer
        } catch (InterruptedIOException e) {
            // drop out to handle buffer
        } catch (IOException e) {
            log.warn("Read error:" + e);
            return "";
        }

        // do we need to close byte array (or flush it?)
        log.debug("Read: " + w.size() + "\n" + w.toString());
        return JOrphanUtils.baToHexString(w.toByteArray());
    }

    /**
     * @return Returns the eomByte.
     */
    public byte getEolByte() {
        return eomByte;
    }

    /**
     * @param eomByte
     *            The eomByte to set.
     */
    public void setEolByte(byte eomByte) {
        this.eomByte = eomByte;
        eomIgnore = false;
    }
}

@@ -2335,17 +2335,32 @@
  @param 
-s
+hexEncodedBinary
  - hex-e
@@ -2466,17 +2466,102 @@
 d string
- 
+%0A     * @throws IllegalArgumentException if string is not an even number of hex digits
 %0A     */
@@ -2624,26 +2624,56 @@
 ing 
-s) %7B%0A        if (s
+hexEncodedBinary) %7B%0A        if (hexEncodedBinary
 .len
@@ -2714,17 +2714,32 @@
 %5B%5D sc = 
-s
+hexEncodedBinary
 .toCharA
@@ -2966,32 +2966,292 @@
  * 2 + 1%5D, 16);%0A
+                if (nibble0 == -1 %7C%7C nibble1 == -1)%7B%0A                    throw new IllegalArgumentException(%0A                    %22Hex-encoded binary string contains an invalid hex digit in '%22+sc%5Bi * 2%5D+sc%5Bi * 2 + 1%5D+%22'%22);                    %0A                %7D%0A
                 

Check for illegal hex characters