KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > stream > BasicHeaderPrintWriter


1 /*
2
3    Derby - Class org.apache.derby.impl.services.stream.BasicHeaderPrintWriter
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.stream;
23
24 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
25 import org.apache.derby.iapi.services.stream.PrintWriterGetHeader;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.io.OutputStream JavaDoc;
30
31 /**
32  * Basic class to print lines with headers.
33  * <p>
34  *
35  * STUB: Should include code to emit a new line before a header
36  * which is not the first thing on the line.
37  *
38  */

39 class BasicHeaderPrintWriter
40     extends PrintWriter
41     implements HeaderPrintWriter
42 {
43
44     private final PrintWriterGetHeader headerGetter;
45     private final boolean canClose;
46     private final String JavaDoc name;
47
48     // constructors
49

50     /**
51      * the constructor sets up the HeaderPrintWriter.
52      * <p>
53      * @param writeTo Where to write to.
54      * @param headerGetter Object to get headers for output lines.
55      * @param canClose If true, {@link #complete} will also close writeTo
56      * @param streamName Name of writeTo, e.g. a file name
57      *
58      * @see PrintWriterGetHeader
59      */

60     BasicHeaderPrintWriter(OutputStream JavaDoc writeTo,
61             PrintWriterGetHeader headerGetter, boolean canClose, String JavaDoc streamName){
62         super(writeTo, true);
63         this.headerGetter = headerGetter;
64         this.canClose = canClose;
65         this.name = streamName;
66     }
67
68     /**
69      * the constructor sets up the HeaderPrintWriter.
70      * <p>
71      * @param writeTo Where to write to.
72      * @param headerGetter Object to get headers for output lines.
73      * @param canClose If true, {@link #complete} will also close writeTo
74      * @param writerName Name of writeTo, e.g. a file name
75      *
76      * @see PrintWriterGetHeader
77      */

78     BasicHeaderPrintWriter(Writer writeTo,
79             PrintWriterGetHeader headerGetter, boolean canClose, String JavaDoc writerName){
80         super(writeTo, true);
81         this.headerGetter = headerGetter;
82         this.canClose = canClose;
83         this.name = writerName;
84     }
85
86     /*
87      * HeaderPrintWriter interface (partial; remaining methods
88      * come from the PrintWriter supertype).
89      */

90     public synchronized void printlnWithHeader(String JavaDoc message)
91     {
92         print(headerGetter.getHeader());
93         println(message);
94     }
95
96     public PrintWriterGetHeader getHeader()
97     {
98         return headerGetter;
99     }
100
101     public PrintWriter getPrintWriter(){
102         return this;
103     }
104
105     public String JavaDoc getName(){
106         return name;
107     }
108
109     /**
110      * Flushes stream, and optionally also closes it if constructed
111      * with canClose equal to true.
112      */

113
114     void complete() {
115         flush();
116         if (canClose) {
117             close();
118         }
119     }
120 }
121
122
Popular Tags