KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > LineOrientedOutputStream


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

18
19 package org.apache.tools.ant.util;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 /**
26  * Invokes {@link #processLine processLine} whenever a full line has
27  * been written to this stream.
28  *
29  * <p>Tries to be smart about line separators.</p>
30  */

31 public abstract class LineOrientedOutputStream extends OutputStream JavaDoc {
32
33     /** Initial buffer size. */
34     private static final int INTIAL_SIZE = 132;
35
36     /** Carriage return */
37     private static final int CR = 0x0d;
38
39     /** Linefeed */
40     private static final int LF = 0x0a;
41
42     private ByteArrayOutputStream JavaDoc buffer
43         = new ByteArrayOutputStream JavaDoc(INTIAL_SIZE);
44     private boolean skip = false;
45
46     /**
47      * Write the data to the buffer and flush the buffer, if a line
48      * separator is detected.
49      *
50      * @param cc data to log (byte).
51      * @throws IOException if there is an error.
52      */

53     public final void write(int cc) throws IOException JavaDoc {
54         final byte c = (byte) cc;
55         if ((c == LF) || (c == CR)) {
56             if (!skip) {
57               processBuffer();
58             }
59         } else {
60             buffer.write(cc);
61         }
62         skip = (c == CR);
63     }
64
65     /**
66      * Flush this log stream
67      * @throws IOException if there is an error.
68      */

69     public final void flush() throws IOException JavaDoc {
70         if (buffer.size() > 0) {
71             processBuffer();
72         }
73     }
74
75     /**
76      * Converts the buffer to a string and sends it to
77      * <code>processLine</code>
78      * @throws IOException if there is an error.
79      */

80     protected void processBuffer() throws IOException JavaDoc {
81         try {
82             processLine(buffer.toString());
83         } finally {
84             buffer.reset();
85         }
86     }
87
88     /**
89      * Processes a line.
90      *
91      * @param line the line to log.
92      * @throws IOException if there is an error.
93      */

94     protected abstract void processLine(String JavaDoc line) throws IOException JavaDoc;
95
96     /**
97      * Writes all remaining
98      * @throws IOException if there is an error.
99      */

100     public final void close() throws IOException JavaDoc {
101         if (buffer.size() > 0) {
102             processBuffer();
103         }
104         super.close();
105     }
106
107     /**
108      * Write a block of characters to the output stream
109      *
110      * @param b the array containing the data
111      * @param off the offset into the array where data starts
112      * @param len the length of block
113      *
114      * @throws IOException if the data cannot be written into the stream.
115      */

116     public final void write(byte[] b, int off, int len) throws IOException JavaDoc {
117         // find the line breaks and pass other chars through in blocks
118
int offset = off;
119         int blockStartOffset = offset;
120         int remaining = len;
121         while (remaining > 0) {
122             while (remaining > 0 && b[offset] != LF && b[offset] != CR) {
123                 offset++;
124                 remaining--;
125             }
126             // either end of buffer or a line separator char
127
int blockLength = offset - blockStartOffset;
128             if (blockLength > 0) {
129                 buffer.write(b, blockStartOffset, blockLength);
130             }
131             while (remaining > 0 && (b[offset] == LF || b[offset] == CR)) {
132                 write(b[offset]);
133                 offset++;
134                 remaining--;
135             }
136             blockStartOffset = offset;
137         }
138     }
139
140 }
141
Popular Tags