diff src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLReader.java @ 180:fdf4c888d5b7

Small code and layout improvements
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 05 Oct 2017 17:01:17 +0200 (2017-10-05)
parents b9b35ca2eec2
children 1296dbcc4958
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLReader.java
+++ b/src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLReader.java
@@ -20,7 +20,7 @@ import java.io.UnsupportedEncodingExcept
  * to provide a means for efficient reading of characters, arrays and
  * lines.  This class is based on the BufferedReader class, and provides
  * extra functionality useful for MCL.
- * 
+ *
  * The BufferedMCLReader is typically used as layer inbetween an
  * InputStream and a specific interpreter of the data.
  * <pre>
@@ -42,29 +42,28 @@ import java.io.UnsupportedEncodingExcept
  * @see nl.cwi.monetdb.mcl.io.BufferedMCLWriter
  */
 public class BufferedMCLReader extends BufferedReader {
+	/** "there is currently no line", or the the type is unknown is represented by UNKNOWN */
+	public final static int UNKNOWN  = 0;
+	/** a line starting with ! indicates ERROR */
+	public final static int ERROR    = '!';
+	/** a line starting with % indicates HEADER */
+	public final static int HEADER   = '%';
+	/** a line starting with [ indicates RESULT */
+	public final static int RESULT   = '[';
+	/** a line which matches the pattern of prompt1 is a PROMPT */
+	public final static int PROMPT   = '.';
+	/** a line which matches the pattern of prompt2 is a MORE */
+	public final static int MORE     = ',';
+	/** a line starting with &amp; indicates the start of a header block */
+	public final static int SOHEADER = '&';
+	/** a line starting with ^ indicates REDIRECT */
+	public final static int REDIRECT = '^';
+	/** a line starting with # indicates INFO */
+	public final static int INFO     = '#';
+
 	/** The type of the last line read */
 	private int lineType;
 
-	/** "there is currently no line", or the the type is unknown is
-	    represented by UNKNOWN */
-	public final static int UNKNOWN   = 0;
-	/** a line starting with ! indicates ERROR */
-	public final static int ERROR     = '!';
-	/** a line starting with % indicates HEADER */
-	public final static int HEADER    = '%';
-	/** a line starting with [ indicates RESULT */
-	public final static int RESULT    = '[';
-	/** a line which matches the pattern of prompt1 is a PROMPT */
-	public final static int PROMPT    = '.';
-	/** a line which matches the pattern of prompt2 is a MORE */
-	public final static int MORE      = ',';
-	/** a line starting with &amp; indicates the start of a header block */
-	public final static int SOHEADER  = '&';
-	/** a line starting with ^ indicates REDIRECT */
-	public final static int REDIRECT  = '^';
-	/** a line starting with # indicates INFO */
-	public final static int INFO      = '#';
-
 	/**
 	 * Create a buffering character-input stream that uses a
 	 * default-sized input buffer.
@@ -94,7 +93,7 @@ public class BufferedMCLReader extends B
 	 * carriage return followed immediately by a linefeed.  Before this
 	 * method returns, it sets the linetype to any of the in MCL
 	 * recognised line types.
-	 * 
+	 *
 	 * Warning: until the server properly prefixes all of its error
 	 * messages with SQLSTATE codes, this method prefixes all errors it
 	 * sees without sqlstate with the generic data exception code
@@ -120,38 +119,38 @@ public class BufferedMCLReader extends B
 	 *
 	 * @param line the string to examine
 	 */
-	void setLineType(String line) {
+	public void setLineType(String line) {
 		lineType = UNKNOWN;
 		if (line == null || line.length() == 0)
 			return;
 		switch (line.charAt(0)) {
 			case '!':
 				lineType = ERROR;
-			break;
+				break;
 			case '&':
 				lineType = SOHEADER;
-			break;
+				break;
 			case '%':
 				lineType = HEADER;
-			break;
+				break;
 			case '[':
 				lineType = RESULT;
-			break;
+				break;
 			case '=':
 				lineType = RESULT;
-			break;
+				break;
 			case '^':
 				lineType = REDIRECT;
-			break;
+				break;
 			case '#':
 				lineType = INFO;
-			break;
+				break;
 			case '.':
 				lineType = PROMPT;
-			break;
+				break;
 			case ',':
 				lineType = MORE;
-			break;
+				break;
 		}
 	}
 
@@ -171,25 +170,26 @@ public class BufferedMCLReader extends B
 	 * for a new command.  All read data is discarded.  If the last line
 	 * read by readLine() was a prompt, this method will immediately
 	 * return.
-	 * 
+	 *
 	 * If there are errors present in the lines that are read, then they
 	 * are put in one string and returned <b>after</b> the prompt has
 	 * been found. If no errors are present, null will be returned.
 	 *
 	 * @return a string containing error messages, or null if there aren't any
 	 * @throws IOException if an IO exception occurs while talking to the server
-	 * 
-	 * TODO(Wouter): should probably not have to be synchronized. + StringBuilder...
+	 *
+	 * TODO(Wouter): should probably not have to be synchronized.
 	 */
 	final public synchronized String waitForPrompt() throws IOException {
-		String ret = "", tmp;
+		String tmp;
+		StringBuilder ret = new StringBuilder(128);
 		while (lineType != PROMPT) {
-			if ((tmp = readLine()) == null)
+			tmp = readLine();
+			if (tmp == null)
 				throw new IOException("Connection to server lost!");
 			if (lineType == ERROR)
-				ret += "\n" + tmp.substring(1);
+				ret.append('\n').append(tmp.substring(1));
 		}
-		return ret == "" ? null : ret.trim();
+		return ret.length() == 0 ? null : ret.toString().trim();
 	}
-
 }