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

1 package hudson.cli;
2
3 import java.io.OutputStream;
4 import java.io.IOException;
5 import java.io.SequenceInputStream;
6
7 /**
8 * {@link OutputStream} version of {@link SequenceInputStream}.
9 *
10 * Provides a single {@link OutputStream} view over multiple {@link OutputStream}s (each of the fixed length.)
11 *
12 * @author Kohsuke Kawaguchi
13 */
14 abstract class SequenceOutputStream extends OutputStream {
15 protected static class Block {
16 final OutputStream out;
17 long capacity;
18
19 public Block(OutputStream out, long capacity) {
20 this.out = out;
21 this.capacity = capacity;
22 }
23 }
24
25 /**
26 * Current block being written.
27 */
28 private Block block;
29
30 protected SequenceOutputStream(Block block) {
31 this.block = block;
32 }
33
34 @Override
35 public void write(byte[] b, int off, int len) throws IOException {
36 while(len>0) {
37 int sz = (int)Math.min(len, block.capacity);
38 block.out.write(b,off,sz);
39 block.capacity -=sz;
40 len-=sz;
41 off+=sz;
42 swapIfNeeded();
43 }
44 }
45
46 public void write(int b) throws IOException {
47 block.out.write(b);
48 block.capacity--;
49 swapIfNeeded();
50 }
51
52 private void swapIfNeeded() throws IOException {
53 if(block.capacity >0) return;
54 block.out.close();
55 block=next(block);
56 }
57
58 @Override
59 public void flush() throws IOException {
60 block.out.flush();
61 }
62
63 @Override
64 public void close() throws IOException {
65 block.out.close();
66 block=null;
67 }
68
69 /**
70 * Fetches the next {@link OutputStream} to write to,
71 * along with their capacity.
72 */
73 protected abstract Block next(Block current) throws IOException;
74 }
75