annotate src/main/java/org/monetdb/mcl/io/BufferedMCLReader.java @ 497:aed7f32e029a onclient

Refactor LineType to be an enum Makes it easier to catch them all when changing things.
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Fri, 13 Aug 2021 10:26:56 +0200 (2021-08-13)
parents bf9f6b6ecf40
children 4cfe4991ee63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1 /*
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2 * This Source Code Form is subject to the terms of the Mozilla Public
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
5 *
406
bf9f6b6ecf40 Update Copyright year.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 391
diff changeset
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
7 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
8
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
9 package org.monetdb.mcl.io;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
10
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
11 import java.io.BufferedReader;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
12 import java.io.IOException;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
13 import java.io.InputStream;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
14 import java.io.Reader;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
15 import java.io.UnsupportedEncodingException;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
16
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
17 import static org.monetdb.mcl.io.LineType.*;
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
18
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
19 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
20 * Read text from a character-input stream, buffering characters so as
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
21 * to provide a means for efficient reading of characters, arrays and
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
22 * lines. This class is based on the BufferedReader class, and provides
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
23 * extra functionality useful for MCL.
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
24 *
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
25 * The BufferedMCLReader is typically used as layer inbetween an
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
26 * InputStream and a specific interpreter of the data.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
27 * <pre>
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
28 * / Response
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
29 * BufferedMCLReader ---o &lt;- Tuple
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
30 * \ DataBlock
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
31 * </pre>
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
32 * Because the BufferedMCLReader provides an efficient way to access the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
33 * data from the stream in a linewise fashion, whereby each line is
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
34 * identified as a certain type, consumers can easily decide how to
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
35 * parse each retrieved line. The line parsers from
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
36 * org.monetdb.mcl.parser are well suited to work with the lines
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
37 * outputted by the BufferedMCLReader.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
38 * This class is client-oriented, as it doesn't take into account the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
39 * messages as the server receives them.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
40 *
194
1296dbcc4958 Resolved javadoc many errors and warnings, such as:
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 180
diff changeset
41 * @author Fabian Groffen
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
42 * @see org.monetdb.mcl.net.MapiSocket
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
43 * @see org.monetdb.mcl.io.BufferedMCLWriter
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
44 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 280
diff changeset
45 public final class BufferedMCLReader extends BufferedReader {
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
46
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
47 /** The type of the last line read */
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
48 private LineType lineType = UNKNOWN;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
49
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
50 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
51 * Create a buffering character-input stream that uses a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
52 * default-sized input buffer.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
53 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
54 * @param in A Reader
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
55 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 280
diff changeset
56 public BufferedMCLReader(final Reader in) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
57 super(in);
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
58 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
59
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
60 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
61 * Create a buffering character-input stream that uses a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
62 * default-sized input buffer, from an InputStream.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
63 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
64 * @param in An InputStream
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
65 * @param enc Encoding
194
1296dbcc4958 Resolved javadoc many errors and warnings, such as:
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 180
diff changeset
66 * @throws UnsupportedEncodingException If encoding is not supported
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
67 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 280
diff changeset
68 public BufferedMCLReader(final InputStream in, final String enc)
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
69 throws UnsupportedEncodingException
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
70 {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 280
diff changeset
71 super(new java.io.InputStreamReader(in, enc));
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
72 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
73
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
74 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
75 * Read a line of text. A line is considered to be terminated by
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
76 * any one of a line feed ('\n'), a carriage return ('\r'), or a
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
77 * carriage return followed immediately by a linefeed. Before this
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
78 * method returns, it sets the linetype to any of the in MCL
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
79 * recognised line types.
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
80 *
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
81 * Warning: until the server properly prefixes all of its error
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
82 * messages with SQLSTATE codes, this method prefixes all errors it
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
83 * sees without sqlstate with the generic data exception code (22000).
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
84 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
85 * @return A String containing the contents of the line, not
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
86 * including any line-termination characters, or null if the
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
87 * end of the stream has been reached
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
88 * @throws IOException If an I/O error occurs
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
89 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
90 @Override
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
91 public String readLine() throws IOException {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
92 String r = super.readLine();
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
93 setLineType(r);
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
94 if (lineType == ERROR && r != null && !r.matches("^![0-9A-Z]{5}!.+")) {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
95 r = "!22000!" + r.substring(1);
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
96 }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
97 return r;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
98 }
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
99
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
100 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
101 * Sets the linetype to the type of the string given. If the string
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
102 * is null, lineType is set to UNKNOWN.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
103 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
104 * @param line the string to examine
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
105 */
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 280
diff changeset
106 public void setLineType(final String line) {
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
107 if (line == null || line.isEmpty()) {
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
108 lineType = UNKNOWN;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
109 return;
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
110 }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
111 switch (line.charAt(0)) {
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
112 case '.':
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
113 lineType = PROMPT;
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
114 break;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
115 case ',':
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
116 lineType = MORE;
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
117 break;
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
118 case '[': /* multi field result */
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
119 case '=': /* single value result */
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
120 lineType = RESULT;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
121 break;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
122 case '%':
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
123 lineType = HEADER;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
124 break;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
125 case '&':
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
126 lineType = SOHEADER;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
127 break;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
128 case '#':
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
129 lineType = INFO;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
130 break;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
131 case '!':
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
132 lineType = ERROR;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
133 break;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
134 case '^':
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
135 lineType = REDIRECT;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
136 break;
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
137 default:
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
138 lineType = UNKNOWN;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
139 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
140 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
141
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
142 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
143 * getLineType returns the type of the last line read.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
144 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
145 * @return an integer representing the kind of line this is, one of the
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
146 * following constants: UNKNOWN, HEADER, ERROR, PROMPT, MORE,
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
147 * RESULT, SOHEADER, REDIRECT, INFO
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
148 */
497
aed7f32e029a Refactor LineType to be an enum
Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
parents: 406
diff changeset
149 public LineType getLineType() {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
150 return lineType;
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
151 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
152
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
153 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
154 * Reads up till the MonetDB prompt, indicating the server is ready
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
155 * for a new command. All read data is discarded. If the last line
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
156 * read by readLine() was a prompt, this method will immediately return.
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
157 *
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
158 * If there are errors present in the lines that are read, then they
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
159 * are put in one string and returned <b>after</b> the prompt has
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
160 * been found. If no errors are present, null will be returned.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
161 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
162 * @return a string containing error messages, or null if there aren't any
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
163 * @throws IOException if an IO exception occurs while talking to the server
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
164 *
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
165 * TODO(Wouter): should probably not have to be synchronized.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
166 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
167 final public synchronized String waitForPrompt() throws IOException {
297
bb273e9c7e09 Add "final" keyword to classes, method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 280
diff changeset
168 final StringBuilder ret = new StringBuilder(128);
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
169 String tmp;
280
72efb3e34436 Improve setLineType()
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
170
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
171 while (lineType != PROMPT) {
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
172 tmp = readLine();
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
173 if (tmp == null)
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
174 throw new IOException("Connection to server lost!");
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
175 if (lineType == ERROR)
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
176 ret.append('\n').append(tmp.substring(1));
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
177 }
180
fdf4c888d5b7 Small code and layout improvements
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 90
diff changeset
178 return ret.length() == 0 ? null : ret.toString().trim();
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
179 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
180 }