--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.services.stream.BasicHeaderPrintWriter\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.services.stream;\r
+\r
+import org.apache.derby.iapi.services.stream.HeaderPrintWriter;\r
+import org.apache.derby.iapi.services.stream.PrintWriterGetHeader;\r
+\r
+import java.io.PrintWriter;\r
+import java.io.Writer;\r
+import java.io.OutputStream;\r
+\r
+/**\r
+ * Basic class to print lines with headers. \r
+ * <p>\r
+ *\r
+ * STUB: Should include code to emit a new line before a header\r
+ * which is not the first thing on the line.\r
+ *\r
+ */\r
+class BasicHeaderPrintWriter \r
+ extends PrintWriter\r
+ implements HeaderPrintWriter\r
+{\r
+\r
+ private final PrintWriterGetHeader headerGetter;\r
+ private final boolean canClose;\r
+ private final String name;\r
+\r
+ // constructors\r
+\r
+ /**\r
+ * the constructor sets up the HeaderPrintWriter. \r
+ * <p>\r
+ * @param writeTo Where to write to.\r
+ * @param headerGetter Object to get headers for output lines.\r
+ * @param canClose If true, {@link #complete} will also close writeTo\r
+ * @param streamName Name of writeTo, e.g. a file name\r
+ *\r
+ * @see PrintWriterGetHeader\r
+ */\r
+ BasicHeaderPrintWriter(OutputStream writeTo,\r
+ PrintWriterGetHeader headerGetter, boolean canClose, String streamName){\r
+ super(writeTo, true);\r
+ this.headerGetter = headerGetter;\r
+ this.canClose = canClose;\r
+ this.name = streamName;\r
+ }\r
+\r
+ /**\r
+ * the constructor sets up the HeaderPrintWriter. \r
+ * <p>\r
+ * @param writeTo Where to write to.\r
+ * @param headerGetter Object to get headers for output lines.\r
+ * @param canClose If true, {@link #complete} will also close writeTo\r
+ * @param writerName Name of writeTo, e.g. a file name\r
+ *\r
+ * @see PrintWriterGetHeader\r
+ */\r
+ BasicHeaderPrintWriter(Writer writeTo,\r
+ PrintWriterGetHeader headerGetter, boolean canClose, String writerName){\r
+ super(writeTo, true);\r
+ this.headerGetter = headerGetter;\r
+ this.canClose = canClose;\r
+ this.name = writerName;\r
+ }\r
+\r
+ /*\r
+ * HeaderPrintWriter interface (partial; remaining methods\r
+ * come from the PrintWriter supertype).\r
+ */\r
+ public synchronized void printlnWithHeader(String message)\r
+ { \r
+ print(headerGetter.getHeader());\r
+ println(message);\r
+ }\r
+\r
+ public PrintWriterGetHeader getHeader()\r
+ {\r
+ return headerGetter;\r
+ }\r
+\r
+ public PrintWriter getPrintWriter(){\r
+ return this;\r
+ }\r
+\r
+ public String getName(){\r
+ return name;\r
+ }\r
+\r
+ /**\r
+ * Flushes stream, and optionally also closes it if constructed\r
+ * with canClose equal to true.\r
+ */\r
+\r
+ void complete() {\r
+ flush();\r
+ if (canClose) {\r
+ close();\r
+ }\r
+ }\r
+}\r
+\r