-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathLargeText.java
392 lines (338 loc) · 11.2 KB
/
LargeText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.model;
import hudson.util.ByteBuffer;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.framework.io.CharSpool;
import org.kohsuke.stapler.framework.io.LineEndNormalizingWriter;
import org.kohsuke.stapler.framework.io.WriterOutputStream;
import org.apache.commons.io.output.CountingOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.io.Reader;
import java.io.InputStreamReader;
/**
* Represents a large text data.
*
* <p>
* This class defines methods for handling progressive text update.
*
* @author Kohsuke Kawaguchi
* @deprecated moved to stapler, as of Hudson 1.220
*/
@Deprecated
public class LargeText {
/**
* Represents the data source of this text.
*/
private interface Source {
Session open() throws IOException;
long length();
boolean exists();
}
private final Source source;
private volatile boolean completed;
public LargeText(final File file, boolean completed) {
this.source = new Source() {
@Override
public Session open() throws IOException {
return new FileSession(file);
}
@Override
public long length() {
return file.length();
}
@Override
public boolean exists() {
return file.exists();
}
};
this.completed = completed;
}
@SuppressWarnings("deprecation")
public LargeText(final ByteBuffer memory, boolean completed) {
this.source = new Source() {
@Override
public Session open() throws IOException {
return new BufferSession(memory);
}
@Override
public long length() {
return memory.length();
}
@Override
public boolean exists() {
return true;
}
};
this.completed = completed;
}
public void markAsComplete() {
completed = true;
}
public boolean isComplete() {
return completed;
}
/**
* Returns {@link Reader} for reading the raw bytes.
*/
public Reader readAll() throws IOException {
return new InputStreamReader(new InputStream() {
final Session session = source.open();
@Override
public int read() throws IOException {
byte[] buf = new byte[1];
int n = session.read(buf);
if(n==1) return buf[0];
else return -1; // EOF
}
@Override
public int read(byte[] buf, int off, int len) throws IOException {
return session.read(buf,off,len);
}
@Override
public void close() throws IOException {
session.close();
}
});
}
/**
* Writes the tail portion of the file to the {@link Writer}.
*
* <p>
* The text file is assumed to be in the system default encoding.
*
* @param start
* The byte offset in the input file where the write operation starts.
*
* @return
* if the file is still being written, this method writes the file
* until the last newline character and returns the offset to start
* the next write operation.
*/
public long writeLogTo(long start, Writer w) throws IOException {
CountingOutputStream os = new CountingOutputStream(new WriterOutputStream(w));
try (Session f = source.open()) {
f.skip(start);
if (completed) {
// write everything till EOF
byte[] buf = new byte[1024];
int sz;
while ((sz = f.read(buf)) >= 0)
os.write(buf, 0, sz);
} else {
ByteBuf buf = new ByteBuf(null, f);
HeadMark head = new HeadMark(buf);
TailMark tail = new TailMark(buf);
while (tail.moveToNextLine(f)) {
head.moveTo(tail, os);
}
head.finish(os);
}
}
os.flush();
return os.getCount()+start;
}
/**
* Implements the progressive text handling.
* This method is used as a "web method" with progressiveText.jelly.
*/
public void doProgressText(StaplerRequest req, StaplerResponse rsp) throws IOException {
rsp.setContentType("text/plain");
rsp.setStatus(HttpServletResponse.SC_OK);
if(!source.exists()) {
// file doesn't exist yet
rsp.addHeader("X-Text-Size","0");
rsp.addHeader("X-More-Data","true");
return;
}
long start = 0;
String s = req.getParameter("start");
if(s!=null)
start = Long.parseLong(s);
if(source.length() < start )
start = 0; // text rolled over
CharSpool spool = new CharSpool();
long r = writeLogTo(start,spool);
rsp.addHeader("X-Text-Size",String.valueOf(r));
if(!completed)
rsp.addHeader("X-More-Data","true");
// when sending big text, try compression. don't bother if it's small
Writer w;
if(r-start>4096)
w = rsp.getCompressedWriter(req);
else
w = rsp.getWriter();
spool.writeTo(new LineEndNormalizingWriter(w));
w.close();
}
/**
* Points to a byte in the buffer.
*/
private static class Mark {
protected ByteBuf buf;
protected int pos;
Mark(ByteBuf buf) {
this.buf = buf;
}
}
/**
* Points to the start of the region that's not committed
* to the output yet.
*/
private static final class HeadMark extends Mark {
HeadMark(ByteBuf buf) {
super(buf);
}
/**
* Moves this mark to 'that' mark, and writes the data
* to {@link OutputStream} if necessary.
*/
void moveTo(Mark that, OutputStream os) throws IOException {
while(this.buf!=that.buf) {
os.write(buf.buf,0,buf.size);
buf = buf.next;
pos = 0;
}
this.pos = that.pos;
}
void finish(OutputStream os) throws IOException {
os.write(buf.buf,0,pos);
}
}
/**
* Points to the end of the region.
*/
private static final class TailMark extends Mark {
TailMark(ByteBuf buf) {
super(buf);
}
boolean moveToNextLine(Session f) throws IOException {
while(true) {
while(pos==buf.size) {
if(!buf.isFull()) {
// read until EOF
return false;
} else {
// read into the next buffer
buf = new ByteBuf(buf,f);
pos = 0;
}
}
byte b = buf.buf[pos++];
if(b=='\r' || b=='\n')
return true;
}
}
}
private static final class ByteBuf {
private final byte[] buf = new byte[1024];
private int size = 0;
private ByteBuf next;
ByteBuf(ByteBuf previous, Session f) throws IOException {
if(previous!=null) {
assert previous.next==null;
previous.next = this;
}
while(!this.isFull()) {
int chunk = f.read(buf, size, buf.length - size);
if(chunk==-1)
return;
size+= chunk;
}
}
public boolean isFull() {
return buf.length==size;
}
}
/**
* Represents the read session of the {@link Source}.
* Methods generally follow the contracts of {@link InputStream}.
*/
private interface Session extends AutoCloseable {
@Override
void close() throws IOException;
void skip(long start) throws IOException;
int read(byte[] buf) throws IOException;
int read(byte[] buf, int offset, int length) throws IOException;
}
/**
* {@link Session} implementation over {@link RandomAccessFile}.
*/
private static final class FileSession implements Session {
private final RandomAccessFile file;
FileSession(File file) throws IOException {
this.file = new RandomAccessFile(file,"r");
}
@Override
public void close() throws IOException {
file.close();
}
@Override
public void skip(long start) throws IOException {
file.seek(file.getFilePointer()+start);
}
@Override
public int read(byte[] buf) throws IOException {
return file.read(buf);
}
@Override
public int read(byte[] buf, int offset, int length) throws IOException {
return file.read(buf,offset,length);
}
}
/**
* {@link Session} implementation over {@link ByteBuffer}.
*/
private static final class BufferSession implements Session {
private final InputStream in;
@SuppressWarnings("deprecation")
BufferSession(ByteBuffer buf) {
this.in = buf.newInputStream();
}
@Override
public void close() throws IOException {
in.close();
}
@Override
public void skip(long n) throws IOException {
while(n>0)
n -= in.skip(n);
}
@Override
public int read(byte[] buf) throws IOException {
return in.read(buf);
}
@Override
public int read(byte[] buf, int offset, int length) throws IOException {
return in.read(buf,offset,length);
}
}
}