/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.kernel.process.log;

import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
import com.liferay.portal.kernel.process.ProcessCallable;

import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @author Shuyang Zhou
 */
public class ProcessOutputStream extends UnsyncByteArrayOutputStream {

	public ProcessOutputStream(ObjectOutputStream objectOutputStream) {
		this(objectOutputStream, false);
	}

	public ProcessOutputStream(
		ObjectOutputStream objectOutputStream, boolean error) {

		_objectOutputStream = objectOutputStream;
		_error = error;
	}

	@Override
	public void close() throws IOException {
		_objectOutputStream.close();
	}

	@Override
	public void flush() throws IOException {
		if (index > 0) {
			byte[] logData = toByteArray();

			if (_logPrefix != null) {
				byte[] bytes = logData;

				logData = new byte[_logPrefix.length + bytes.length];

				System.arraycopy(_logPrefix, 0, logData, 0, _logPrefix.length);
				System.arraycopy(
					bytes, 0, logData, _logPrefix.length, bytes.length);
			}

			LoggingProcessCallable loggingProcessCallable =
				new LoggingProcessCallable(logData, _error);

			writeProcessCallable(loggingProcessCallable);

			reset();
		}
	}

	public void setLogPrefix(byte[] logPrefix) {
		_logPrefix = logPrefix;
	}

	public void writeProcessCallable(ProcessCallable<?> processCallable)
		throws IOException {

		synchronized (_objectOutputStream) {
			_objectOutputStream.writeObject(processCallable);

			_objectOutputStream.flush();
		}
	}

	private final boolean _error;
	private byte[] _logPrefix;
	private final ObjectOutputStream _objectOutputStream;

}