File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\console\LineTransformationOutputStream.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.console;
25
26 import hudson.util.ByteArrayOutputStream2;
27
28 import java.io.IOException;
29 import java.io.OutputStream;
30
31 /**
32 * Filtering {@link OutputStream} that buffers text by line, so that the derived class
33 * can perform some manipulation based on the contents of the whole line.
34 *
35 * TODO: Mac is supposed to be CR-only. This class needs to handle that.
36 *
37 * @author Kohsuke Kawaguchi
38 * @since 1.349
39 */
40 public abstract class LineTransformationOutputStream extends OutputStream {
41 private ByteArrayOutputStream2 buf = new ByteArrayOutputStream2();
42
43 /**
44 * Called for each end of the line.
45 *
46 * @param b
47 * Contents of the whole line, including the EOL code like CR/LF.
48 * @param len
49 * Specifies the length of the valid contents in 'b'. The rest is garbage.
50 * This is so that the caller doesn't have to allocate an array of the exact size.
51 */
52 protected abstract void eol(byte[] b, int len) throws IOException;
53
54 public void write(int b) throws IOException {
55 buf.write(b);
56 if (b==LF) eol();
57 }
58
59 private void eol() throws IOException {
60 eol(buf.getBuffer(),buf.size());
61
62 // reuse the buffer under normal circumstances, but don't let the line buffer grow unbounded
63 if (buf.size()>4096)
64 buf = new ByteArrayOutputStream2();
65 else
66 buf.reset();
67 }
68
69 @Override
70 public void write(byte[] b, int off, int len) throws IOException {
71 int end = off+len;
72
73 for( int i=off; i<end; i++ )
74 write(b[i]);
75 }
76
77 @Override
78 public void close() throws IOException {
79 forceEol();
80 }
81
82 /**
83 * Forces the EOL behavior.
84 *
85 * Useful if the caller wants to make sure the buffered content is all processed, but without
86 * actually neither flushing nor closing the stream.
87 */
88 public void forceEol() throws IOException {
89 if (buf.size()>0) {
90 /*
91 because LargeText cuts output at the line end boundary, this is
92 possible only for the very end of the console output, if the output ends without NL.
93 */
94 eol();
95 }
96 }
97
98 protected String trimEOL(String line) {
99 int slen = line.length();
100 while (slen>0) {
101 char ch = line.charAt(slen-1);
102 if (ch=='\r' || ch=='\n') {
103 slen--;
104 continue;
105 }
106 break;
107 }
108 line = line.substring(0,slen);
109 return line;
110 }
111
112 private static final int LF = 0x0A;
113 }
114