// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb5.lib;

import java.io.*;
import java.net.*;

/**
 * TCPClient instantiates a tcp client.
 */
public class TCPClient {
    /**
     * Sets buffer size to 65507
     */
	int bufSize = 65507;
	
	/**
	 * Tcp Socket
	 * @see java.net.Socket
	 */
	Socket tcpSocket;
	
	/**
	 * Output buffer
	 * @see java.io.BufferedOutputStream
	 */
	BufferedOutputStream out;
	
	/**
	 * Input buffer
	 * @see java.io.BufferedInputStream
	 */
	BufferedInputStream in;

    /**
     * Sets the buffer size.
     *
     * @param newBufSize is of type int
     */
	public void setBufSize(int newBufSize) {
		bufSize = newBufSize;
	}

    /**
     * Class constructor specifying the ip address and the port.
     *
     * @param iaddr is of type InetAddress
	 * @param port is of type int
	 * @exception IOException an exception 
     * @see java.net.InetAddress
     * @see java.io.IOException
     */
	public TCPClient(InetAddress iaddr, int port) throws IOException {
		tcpSocket = new Socket(iaddr, port);
		out = new BufferedOutputStream(tcpSocket.getOutputStream());
		in = new BufferedInputStream(tcpSocket.getInputStream());
	}

    /**
     * Class constructor specifying the host name and the port.
     *
     * @param hostname is of type String
	 * @param port is of type int
	 * @exception IOException an exception 
     * @see java.io.IOException
     */
	public TCPClient(String hostname, int port) throws IOException {
		tcpSocket = new Socket(hostname, port);
		out = new BufferedOutputStream(tcpSocket.getOutputStream());
		in = new BufferedInputStream(tcpSocket.getInputStream());
	}

    /**
     * Class constructor specifying the socket.
     *
     * @param sock is of type Socket
	 * @exception IOException an exception 
     * @see java.net.Socket
     * @see java.io.IOException
     */
	public TCPClient(Socket sock) throws IOException {
		tcpSocket = sock;
		out = new BufferedOutputStream(tcpSocket.getOutputStream());
		in = new BufferedInputStream(tcpSocket.getInputStream());
	}

    /**
     * Gets the inet address
     *
     * @return InetAddress is a result 
     * @see java.net.InetAddress
     */
	public InetAddress getInetAddress() {
		return tcpSocket.getInetAddress();
	}

    /**
     * Sends an array of bytes
     *
     * @param data is of type byte[]
	 * @exception IOException an exception  
     * @see java.io.IOException
     */
	public void send(byte[] data) throws IOException {
		out.write(data);
		out.flush();
	}

    /**
     * Receives an array of bytes
     *
     * @return byte[] is a result
	 * @exception IOException an exception 
     * @see java.io.IOException
     */
	public byte[] receive() throws IOException {
		byte temp[] = new byte[bufSize];
		int size = in.read(temp);
		if (size >= 0) {
			byte[] data = new byte[size];
			System.arraycopy(temp, 0, data, 0, size);
			return data;
		}
		return null;
	}

    /**
     * Closes the connection
     *
     * @exception IOException an exception 
     * @see java.io.IOException
     */
	public void close() throws IOException {
		tcpSocket.close();
	}

}
