1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.doxia.sink.impl;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.apache.maven.doxia.sink.Sink;
30 import org.apache.maven.doxia.sink.SinkFactory;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @Deprecated
46 public class RandomAccessSink extends SinkWrapper {
47 private SinkFactory sinkFactory;
48
49 private String encoding;
50
51 private OutputStream coreOutputStream;
52
53 private Sink coreSink;
54
55 private List<Sink> sinks = new ArrayList<>();
56
57 private List<ByteArrayOutputStream> outputStreams = new ArrayList<>();
58
59 private Sink currentSink;
60
61
62
63
64
65
66
67
68 public RandomAccessSink(SinkFactory sinkFactory, OutputStream stream) throws IOException {
69 super(sinkFactory.createSink(stream));
70 this.sinkFactory = sinkFactory;
71 this.coreOutputStream = stream;
72 this.coreSink = getWrappedSink();
73 }
74
75
76
77
78
79
80
81
82
83 public RandomAccessSink(SinkFactory sinkFactory, OutputStream stream, String encoding) throws IOException {
84 super(sinkFactory.createSink(stream, encoding));
85 this.sinkFactory = sinkFactory;
86 this.coreOutputStream = stream;
87 this.encoding = encoding;
88 this.coreSink = getWrappedSink();
89 }
90
91
92
93
94
95
96
97
98
99 public RandomAccessSink(SinkFactory sinkFactory, File outputDirectory, String outputName) throws IOException {
100 super(null);
101 this.sinkFactory = sinkFactory;
102 this.coreOutputStream = new FileOutputStream(new File(outputDirectory, outputName));
103 this.currentSink = sinkFactory.createSink(coreOutputStream);
104 this.coreSink = this.currentSink;
105 setWrappedSink(currentSink);
106 }
107
108
109
110
111
112
113
114
115
116
117 public RandomAccessSink(SinkFactory sinkFactory, File outputDirectory, String outputName, String encoding)
118 throws IOException {
119 super(null);
120 this.sinkFactory = sinkFactory;
121 this.coreOutputStream = new FileOutputStream(new File(outputDirectory, outputName));
122 this.encoding = encoding;
123 this.currentSink = sinkFactory.createSink(coreOutputStream, encoding);
124 this.coreSink = this.currentSink;
125 setWrappedSink(currentSink);
126 }
127
128
129
130
131
132
133
134 public Sink addSinkHook() {
135 Sink subSink = null;
136 try {
137 ByteArrayOutputStream subOut = new ByteArrayOutputStream();
138 ByteArrayOutputStream newOut = new ByteArrayOutputStream();
139
140 outputStreams.add(subOut);
141 outputStreams.add(newOut);
142
143 if (encoding != null) {
144 subSink = sinkFactory.createSink(subOut, encoding);
145 currentSink = sinkFactory.createSink(newOut, encoding);
146 } else {
147 subSink = sinkFactory.createSink(subOut);
148 currentSink = sinkFactory.createSink(newOut);
149 }
150 sinks.add(subSink);
151 sinks.add(currentSink);
152 setWrappedSink(currentSink);
153 } catch (IOException e) {
154
155 }
156 return subSink;
157 }
158
159
160
161
162 public void close() {
163 for (Sink sink : sinks) {
164
165 sink.close();
166 }
167 coreSink.close();
168 }
169
170
171
172
173 public void flush() {
174 for (int i = 0; i < sinks.size(); i++) {
175
176
177 Sink sink = sinks.get(i);
178 sink.flush();
179
180 ByteArrayOutputStream stream = outputStreams.get(i);
181 try {
182 coreOutputStream.write(stream.toByteArray());
183 } catch (IOException e) {
184
185 }
186 }
187 coreSink.flush();
188 }
189 }