comparison src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLWriter.java @ 0:a5a898f6886c

Copy of MonetDB java directory changeset e6e32756ad31.
author Sjoerd Mullender <sjoerd@acm.org>
date Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21)
parents
children e605cdd6373f 17fbaf2635bb
comparison
equal deleted inserted replaced
-1:000000000000 0:a5a898f6886c
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 - 2016 MonetDB B.V.
7 */
8
9 package nl.cwi.monetdb.mcl.io;
10
11 import java.io.*;
12
13 /**
14 * Write text to a character-output stream, buffering characters so as
15 * to provide a means for efficient writing of single characters,
16 * arrays, and strings.
17 *
18 * In contrast to the BufferedWriter class, this class' newLine()
19 * method always writes the newline character '\n', regardless the
20 * platform's own notion of line separator. Apart from that there are
21 * no differences in the behaviour of this class, compared to its parent
22 * class, the BufferedWriter. A small convenience is built into this
23 * class for cooperation with the BufferedMCLReader, via the
24 * registerReader() method. It causes the reader to be reset upon each
25 * write performed though this class. This effectuates the MCL protocol
26 * flow where a write invalidates the state of the read buffers, since
27 * each write must be answered by the server. That also makes this
28 * class client-oriented when a reader is registered.
29 *
30 * @author Fabian Groffen <Fabian.Groffen>
31 * @see nl.cwi.monetdb.mcl.net.MapiSocket
32 * @see nl.cwi.monetdb.mcl.io.BufferedMCLWriter
33 */
34 public class BufferedMCLWriter extends BufferedWriter {
35 private BufferedMCLReader reader;
36
37 /**
38 * Create a buffered character-output stream that uses a
39 * default-sized output buffer.
40 *
41 * @param in A Writer
42 */
43 public BufferedMCLWriter(Writer in) {
44 super(in);
45 }
46
47 /**
48 * Create a buffered character-output stream that uses a
49 * default-sized output buffer, from an OutputStream.
50 *
51 * @param in An OutputStream
52 * @param enc Encoding
53 */
54 public BufferedMCLWriter(OutputStream in, String enc)
55 throws UnsupportedEncodingException
56 {
57 super(new OutputStreamWriter(in, enc));
58 }
59
60 /**
61 * Registers the given reader in this writer. A registered reader
62 * receives a linetype reset when a line is written from this
63 * writer.
64 *
65 * @param r an BufferedMCLReader
66 */
67 public void registerReader(BufferedMCLReader r) {
68 reader = r;
69 }
70
71 /**
72 * Write a line separator. The line separator string is in this
73 * class always the single newline character '\n'.
74 *
75 * @throws IOException If an I/O error occurs
76 */
77 @Override
78 public void newLine() throws IOException {
79 write('\n');
80 }
81
82 /**
83 * Write a single line, terminated with a line separator, and flush
84 * the stream. This is a shorthand method for a call to write()
85 * and flush().
86 *
87 * @param line The line to write
88 * @throws IOException If an I/O error occurs
89 */
90 public void writeLine(String line) throws IOException {
91 write(line);
92 flush();
93 // reset reader state, last line isn't valid any more now
94 if (reader != null) reader.setLineType(null);
95 }
96 }