<commit_before>package ibis.ipl.impl.net;

import ibis.io.ArrayOutputStream;
import ibis.io.SerializationOutputStream;

import ibis.io.Replacer;

import ibis.ipl.impl.net.*;

/**
 * The ID output implementation.
 */
public abstract class NetSerializedOutput extends NetOutput {

	/**
	 * The driver used for the 'real' output.
	 */
	protected NetDriver                 subDriver = null;

	/**
	 * The 'real' output.
	 */
	protected NetOutput                 subOutput = null;

        private   SerializationOutputStream oss       = null;

        private   Replacer                  replacer  = null;

        private   boolean                   needFlush = false;

	/**
	 * Constructor.
	 *
	 * @param sp the properties of the output's
	 * {@link ibis.ipl.impl.net.NetSendPort NetSendPort}.
	 * @param driver the ID driver instance.
	 * @param output the controlling output.
	 */
	public NetSerializedOutput(NetPortType pt, NetDriver driver, String context) throws NetIbisException {
		super(pt, driver, context);
	}

	/**
	 * {@inheritDoc}
	 */
	public synchronized void setupConnection(NetConnection cnx) throws NetIbisException {
		NetOutput subOutput = this.subOutput;
                try {

		if (subOutput == null) {
			if (subDriver == null) {
                                String subDriverName = getProperty("Driver");
				subDriver = driver.getIbis().getDriver(subDriverName);
			}

			subOutput = newSubOutput(subDriver);
			this.subOutput = subOutput;
		}

                replacer = cnx.getReplacer();

		subOutput.setupConnection(cnx);

		int _mtu = subOutput.getMaximumTransfertUnit();
		if (mtu == 0  ||  mtu > _mtu) {
			mtu = _mtu;
		}

 		int _headersLength = subOutput.getHeadersLength();

 		if (headerOffset < _headersLength) {
 			headerOffset = _headersLength;
 		}

                /*
                 * Need to re-initialize the serialization stream state
                 * in order to ensure consistency between multiple receivers.
                 */
                oss = null;
		} catch (Exception e) {
                        throw new Error(e);
                }
	}

        public abstract SerializationOutputStream newSerializationOutputStream() throws NetIbisException;


	/**
	 * {@inheritDoc}
	 */
	public void initSend() throws NetIbisException {
                super.initSend();
                subOutput.initSend();
                if (oss == null) {
                        subOutput.writeByte((byte)1);
                        oss = newSerializationOutputStream();
                        if (replacer != null) oss.setReplacer(replacer);
                } else {
                        subOutput.writeByte((byte)0);
                }
	}

        private void flushStream() throws java.io.IOException {
                if (needFlush) {
                        oss.flush();
                        needFlush = false;
                }
        }


        /**
	   Block until the entire message has been sent and clean up the message. Only after finish() or reset(), the data that was written
	   may be touched. Only one message is alive at one time for a given sendport. This is done to prevent flow control problems.
	   When a message is alive and a new messages is requested, the requester is blocked until the
	   live message is finished. **/
        public void finish() throws NetIbisException {
	    try {
                super.finish();
                flushStream();
                // oss.reset(); // does not work with S_Sun serialization
                oss = null;
                subOutput.finish();
	    } catch(java.io.IOException e) {
		throw new NetIbisException("got exception", e);<commit_msg>- corrected the stream reset<commit_after>package ibis.ipl.impl.net;

import ibis.io.ArrayOutputStream;
import ibis.io.SerializationOutputStream;

import ibis.io.Replacer;

import ibis.ipl.impl.net.*;

/**
 * The ID output implementation.
 */
public abstract class NetSerializedOutput extends NetOutput {

	/**
	 * The driver used for the 'real' output.
	 */
	protected NetDriver                 subDriver = null;

	/**
	 * The 'real' output.
	 */
	protected NetOutput                 subOutput = null;

        private   SerializationOutputStream oss       = null;

        private   Replacer                  replacer  = null;

        private   boolean                   needFlush = false;

	/**
	 * Constructor.
	 *
	 * @param sp the properties of the output's
	 * {@link ibis.ipl.impl.net.NetSendPort NetSendPort}.
	 * @param driver the ID driver instance.
	 * @param output the controlling output.
	 */
	public NetSerializedOutput(NetPortType pt, NetDriver driver, String context) throws NetIbisException {
		super(pt, driver, context);
	}

	/**
	 * {@inheritDoc}
	 */
	public synchronized void setupConnection(NetConnection cnx) throws NetIbisException {
		NetOutput subOutput = this.subOutput;
                try {

		if (subOutput == null) {
			if (subDriver == null) {
                                String subDriverName = getProperty("Driver");
				subDriver = driver.getIbis().getDriver(subDriverName);
			}

			subOutput = newSubOutput(subDriver);
			this.subOutput = subOutput;
		}

                replacer = cnx.getReplacer();

		subOutput.setupConnection(cnx);

		int _mtu = subOutput.getMaximumTransfertUnit();
		if (mtu == 0  ||  mtu > _mtu) {
			mtu = _mtu;
		}

 		int _headersLength = subOutput.getHeadersLength();

 		if (headerOffset < _headersLength) {
 			headerOffset = _headersLength;
 		}

                /*
                 * Need to re-initialize the serialization stream state
                 * in order to ensure consistency between multiple receivers.
                 */
                oss = null;
		} catch (Exception e) {
                        throw new Error(e);
                }
	}

        public abstract SerializationOutputStream newSerializationOutputStream() throws NetIbisException;


	/**
	 * {@inheritDoc}
	 */
	public void initSend() throws NetIbisException {
                super.initSend();
                subOutput.initSend();
                if (oss == null) {
                        subOutput.writeByte((byte)1);
                        oss = newSerializationOutputStream();
                        if (replacer != null) oss.setReplacer(replacer);
                } else {
                        subOutput.writeByte((byte)0);
                        try {
                                oss.reset();
                        } catch(java.io.IOException e) {
                                throw new NetIbisException("got exception", e);
                        }
                }
	}

        private void flushStream() throws java.io.IOException {
                if (needFlush) {
                        oss.flush();
                        needFlush = false;
                }
        }


        /**
	   Block until the entire message has been sent and clean up the message. Only after finish() or reset(), the data that was written
	   may be touched. Only one message is alive at one time for a given sendport. This is done to prevent flow control problems.
	   When a message is alive and a new messages is requested, the requester is blocked until the
	   live message is finished. **/
        public void finish() throws NetIbisException {
	    try {
                super.finish();
                flushStream();
                subOutput.finish();
	    } catch(java.io.IOException e) {
		throw new NetIbisException("got exception", e);
{'fraction_non_alphanumeric': 0.08845175966813863, 'fraction_numerical': 0.0008028904054596548, 'mean_word_length': 1.8429060479269683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4812463', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1876, 'n_words': 569}