001: /*
002:
003: Derby - Class org.apache.derby.impl.services.stream.BasicHeaderPrintWriter
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.stream;
023:
024: import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
025: import org.apache.derby.iapi.services.stream.PrintWriterGetHeader;
026:
027: import java.io.PrintWriter;
028: import java.io.Writer;
029: import java.io.OutputStream;
030:
031: /**
032: * Basic class to print lines with headers.
033: * <p>
034: *
035: * STUB: Should include code to emit a new line before a header
036: * which is not the first thing on the line.
037: *
038: */
039: class BasicHeaderPrintWriter extends PrintWriter implements
040: HeaderPrintWriter {
041:
042: private final PrintWriterGetHeader headerGetter;
043: private final boolean canClose;
044: private final String name;
045:
046: // constructors
047:
048: /**
049: * the constructor sets up the HeaderPrintWriter.
050: * <p>
051: * @param writeTo Where to write to.
052: * @param headerGetter Object to get headers for output lines.
053: * @param canClose If true, {@link #complete} will also close writeTo
054: * @param streamName Name of writeTo, e.g. a file name
055: *
056: * @see PrintWriterGetHeader
057: */
058: BasicHeaderPrintWriter(OutputStream writeTo,
059: PrintWriterGetHeader headerGetter, boolean canClose,
060: String streamName) {
061: super (writeTo, true);
062: this .headerGetter = headerGetter;
063: this .canClose = canClose;
064: this .name = streamName;
065: }
066:
067: /**
068: * the constructor sets up the HeaderPrintWriter.
069: * <p>
070: * @param writeTo Where to write to.
071: * @param headerGetter Object to get headers for output lines.
072: * @param canClose If true, {@link #complete} will also close writeTo
073: * @param writerName Name of writeTo, e.g. a file name
074: *
075: * @see PrintWriterGetHeader
076: */
077: BasicHeaderPrintWriter(Writer writeTo,
078: PrintWriterGetHeader headerGetter, boolean canClose,
079: String writerName) {
080: super (writeTo, true);
081: this .headerGetter = headerGetter;
082: this .canClose = canClose;
083: this .name = writerName;
084: }
085:
086: /*
087: * HeaderPrintWriter interface (partial; remaining methods
088: * come from the PrintWriter supertype).
089: */
090: public synchronized void printlnWithHeader(String message) {
091: print(headerGetter.getHeader());
092: println(message);
093: }
094:
095: public PrintWriterGetHeader getHeader() {
096: return headerGetter;
097: }
098:
099: public PrintWriter getPrintWriter() {
100: return this ;
101: }
102:
103: public String getName() {
104: return name;
105: }
106:
107: /**
108: * Flushes stream, and optionally also closes it if constructed
109: * with canClose equal to true.
110: */
111:
112: void complete() {
113: flush();
114: if (canClose) {
115: close();
116: }
117: }
118: }
|