
package uk.co.wingpath.io;

import java.io.*;
import java.net.*;
import uk.co.wingpath.util.*;

/**
* This class implements a dummy connection.
* All data written to the connection is discarded.
* Attempts to read from the connection will timeout.
*/

public class NullConnection
    implements Connection
{
    private String name = null;

    /**
    * This method does nothing.
    * If the number of bytes to be read is zero, it returns
    * immediately. Otherwise, it returns after the specified timeout
    * period. In neither case is the <code>data</code> array modified.
    * @param data the buffer into which the data is read.
    * @param offset the start offset in array <code>data</code> at which
    *   the data is written.
    * @param length the maximum number of bytes to read.
    * @param timeout maximum time (in milliseconds) to wait for data to be
    *   available.
    * @param first <code>true</code> if the first "chunk" of a message is
    *   being read. <code>false</code> for subsequent chunks.
    * @return the number of bytes read.
    * @throws InterruptedIOException if a timeout occurs.
    * @throws InterruptedException if the thread is interrupted.
    */
    public int read (byte [] data, int offset, int length, int timeout,
            boolean first)
        throws InterruptedIOException, InterruptedException
    {
        if (length == 0)
            return 0;
        Thread.sleep (timeout);
        throw new HInterruptedIOException ("I120", "Timed out");
    }

    /**
    * This method does nothing.
    */
    public void write (byte [] data, int offset, int length)
    {
    }

    /**
    * This method does nothing.
    * @return <code>null</code>.
    */
    public byte [] discardInput ()
    {
        return null;
    }

    /**
    * This method does nothing.
    */
    public void drain ()
    {
    }

    /**
    * This method does nothing.
    */
    public void flush ()
    {
    }

    /**
    * This method does nothing.
    */
    public void open ()
    {
    }

    /**
    * This method does nothing.
    */
    public void close ()
    {
    }

    public boolean isOpen ()
    {
        return true;
    }

    public String getName ()
    {
        if (name != null)
            return name;
        return "null";
    }

    public void setName (String name)
    {
        this.name = name;
    }
}


