view src/main/java/org/monetdb/mcl/io/BufferedMCLWriter.java @ 937:d416e9b6b3d0

Update Copyright year.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 02 Jan 2025 13:27:58 +0100 (3 months ago)
parents e890195256ac
children
line wrap: on
line source
/*
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 2024, 2025 MonetDB Foundation;
 * Copyright August 2008 - 2023 MonetDB B.V.;
 * Copyright 1997 - July 2008 CWI.
 */

package org.monetdb.mcl.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;

/**
 * Write text to a character-output stream, buffering characters so as
 * to provide a means for efficient writing of single characters,
 * arrays, and strings.
 *
 * In contrast to the BufferedWriter class, this class' newLine()
 * method always writes the newline character '\n', regardless the
 * platform's own notion of line separator.  Apart from that there are
 * no differences in the behaviour of this class, compared to its parent
 * class, the BufferedWriter.  A small convenience is built into this
 * class for cooperation with the BufferedMCLReader, via the
 * registerReader() method.  It causes the reader to be reset upon each
 * write performed though this class.  This effectuates the MCL protocol
 * flow where a write invalidates the state of the read buffers, since
 * each write must be answered by the server.  That also makes this
 * class client-oriented when a reader is registered.
 *
 * @author Fabian Groffen
 * @see org.monetdb.mcl.net.MapiSocket
 * @see org.monetdb.mcl.io.BufferedMCLReader
 */
public final class BufferedMCLWriter extends BufferedWriter {
	private BufferedMCLReader reader;

	/**
	 * Create a buffered character-output stream that uses a
	 * default-sized output buffer.
	 *
	 * @param in A Writer
	 */
	public BufferedMCLWriter(final Writer in) {
		super(in);
	}

	/**
	 * Create a buffered character-output stream that uses a
	 * default-sized output buffer, from an OutputStream.
	 *
	 * @param in An OutputStream
	 * @param cs A Charset
	 */
	public BufferedMCLWriter(final OutputStream in, final Charset cs) {
		super(new java.io.OutputStreamWriter(in, cs));
	}

	/**
	 * Write a line separator.  The line separator string is in this
	 * class always the single newline character '\n'.
	 *
	 * @throws IOException If an I/O error occurs
	 */
	@Override
	public void newLine() throws IOException {
		write('\n');
	}

	/**
	 * Registers the given reader in this writer.  A registered reader
	 * receives a linetype reset when a line is written from this
	 * writer.
	 *
	 * @param r an BufferedMCLReader
	 */
	public void registerReader(final BufferedMCLReader r) {
		reader = r;
	}

	/**
	 * Write a single line, terminated with a line separator, and flush
	 * the stream.  This is a shorthand method for a call to write()
	 * and flush().
	 *
	 * @param line The line to write
	 * @throws IOException If an I/O error occurs
	 */
	public void writeLine(final String line) throws IOException {
		write(line);
		flush();

		// reset reader state, last line isn't valid any more now
		if (reader != null)
			reader.resetLineType();
	}
}