comparison src/main/java/org/monetdb/mcl/io/BufferedMCLWriter.java @ 391:f523727db392

Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.* This naming complies to the Java Package Naming convention as MonetDB's main website is www.monetdb.org.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 12 Nov 2020 22:02:01 +0100 (2020-11-12)
parents src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLWriter.java@54137aeb1f92
children bf9f6b6ecf40
comparison
equal deleted inserted replaced
390:6199e0be3c6e 391:f523727db392
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V.
7 */
8
9 package org.monetdb.mcl.io;
10
11 import java.io.BufferedWriter;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.UnsupportedEncodingException;
15 import java.io.Writer;
16
17 /**
18 * Write text to a character-output stream, buffering characters so as
19 * to provide a means for efficient writing of single characters,
20 * arrays, and strings.
21 *
22 * In contrast to the BufferedWriter class, this class' newLine()
23 * method always writes the newline character '\n', regardless the
24 * platform's own notion of line separator. Apart from that there are
25 * no differences in the behaviour of this class, compared to its parent
26 * class, the BufferedWriter. A small convenience is built into this
27 * class for cooperation with the BufferedMCLReader, via the
28 * registerReader() method. It causes the reader to be reset upon each
29 * write performed though this class. This effectuates the MCL protocol
30 * flow where a write invalidates the state of the read buffers, since
31 * each write must be answered by the server. That also makes this
32 * class client-oriented when a reader is registered.
33 *
34 * @author Fabian Groffen
35 * @see org.monetdb.mcl.net.MapiSocket
36 * @see org.monetdb.mcl.io.BufferedMCLReader
37 */
38 public final class BufferedMCLWriter extends BufferedWriter {
39 private BufferedMCLReader reader;
40
41 /**
42 * Create a buffered character-output stream that uses a
43 * default-sized output buffer.
44 *
45 * @param in A Writer
46 */
47 public BufferedMCLWriter(final Writer in) {
48 super(in);
49 }
50
51 /**
52 * Create a buffered character-output stream that uses a
53 * default-sized output buffer, from an OutputStream.
54 *
55 * @param in An OutputStream
56 * @param enc Encoding
57 * @throws UnsupportedEncodingException If encoding is not supported
58 */
59 public BufferedMCLWriter(final OutputStream in, final String enc)
60 throws UnsupportedEncodingException
61 {
62 super(new java.io.OutputStreamWriter(in, enc));
63 }
64
65 /**
66 * Write a line separator. The line separator string is in this
67 * class always the single newline character '\n'.
68 *
69 * @throws IOException If an I/O error occurs
70 */
71 @Override
72 public void newLine() throws IOException {
73 write('\n');
74 }
75
76 /**
77 * Registers the given reader in this writer. A registered reader
78 * receives a linetype reset when a line is written from this
79 * writer.
80 *
81 * @param r an BufferedMCLReader
82 */
83 public void registerReader(final BufferedMCLReader r) {
84 reader = r;
85 }
86
87 /**
88 * Write a single line, terminated with a line separator, and flush
89 * the stream. This is a shorthand method for a call to write()
90 * and flush().
91 *
92 * @param line The line to write
93 * @throws IOException If an I/O error occurs
94 */
95 public void writeLine(final String line) throws IOException {
96 write(line);
97 flush();
98
99 // reset reader state, last line isn't valid any more now
100 if (reader != null)
101 reader.setLineType(null);
102 }
103 }