package de.renew.database;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;

import de.renew.net.NetInstance;


/**
 * A net instance resolution input stream is a special
 * {@link ObjectInputStream} that contains additional information
 * for resolving the net instance references (if any) of the
 * tokens back to the net instances.
 */
public class NetInstanceResolutionInputStream extends ObjectInputStream {

    /**
     * The net instance mapping hashtable.
     */
    private final NetInstanceMap _map;

    /**
     * Creates the net instance resolution input stream.
     *
     * @param stream The InputStream to read object data from.
     * @param map The net instance mapping hashtable.
     * @throws IOException if an IO error occurs while reading the stream
     * @throws StreamCorruptedException if the stream is corrupted
     */
    public NetInstanceResolutionInputStream(InputStream stream, NetInstanceMap map)
        throws IOException, StreamCorruptedException
    {
        super(stream);
        _map = map;
    }

    /**
     * Returns a net instance for a given netID.
     *
     * @param netID The net ID.
     * @return the corresponding net instance.
     */
    public NetInstance getNetInstanceByNetID(String netID) {
        return _map.get(netID);
    }
}