-
Notifications
You must be signed in to change notification settings - Fork 2
/
ArcWriter.java
425 lines (393 loc) · 15.2 KB
/
ArcWriter.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* Java Web Archive Toolkit - Software to read and validate ARC, WARC
* and GZip files. (http://jwat.org/)
* Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jwat.arc;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.jwat.common.Diagnosis;
import org.jwat.common.DiagnosisType;
import org.jwat.common.Diagnostics;
/**
* Base class for ARC writer implementations.
*
* Note: the content-length is synonymous to the archive-length.
*
* @author nicl
*/
public abstract class ArcWriter implements Closeable {
/** State after writer has been constructed and before records have been written. */
protected static final int S_INIT = 0;
/** State after header has been written. */
protected static final int S_HEADER_WRITTEN = 1;
/** State after payload has been written. */
protected static final int S_PAYLOAD_WRITTEN = 2;
/** State after record has been closed. */
protected static final int S_RECORD_CLOSED = 3;
/*
* Settings.
*/
/** ARC field parser used. */
protected ArcFieldParsers fieldParsers;
/** Buffer used by streamPayload() to copy from one stream to another. */
protected byte[] stream_copy_buffer;
/** Configuration for throwing exception on content-length mismatch.
* (Default is true) */
protected boolean bExceptionOnContentLengthMismatch;
/*
* State.
*/
/** Writer level errors and warnings or when writing byte headers. */
public final Diagnostics diagnostics = new Diagnostics();
/** Current state of writer. */
protected int state = S_INIT;
/** Outputstream used to write ARC records. */
protected OutputStream out;
protected ArcHeader header;
/** Content-Length from the ARC header. */
protected Long headerContentLength;
/** Total bytes written for current record payload. */
protected long payloadWrittenTotal;
/**
* Method used to initialize a readers internal state.
* Must be called by all constructors.
*/
protected void init() {
fieldParsers = new ArcFieldParsers();
stream_copy_buffer = new byte[8192];
bExceptionOnContentLengthMismatch = true;
}
/**
* Is this writer producing compressed output.
* @return boolean indicating whether compressed output is produced
*/
public abstract boolean isCompressed();
/**
* Does this writer throw an exception if the content-length does not match
* the payload amount written.
* @return boolean indicating if an exception is thrown or not
*/
public boolean exceptionOnContentLengthMismatch() {
return bExceptionOnContentLengthMismatch;
}
/**
* Tell the writer what to do in case of mismatch between content-length
* and amount payload written.
* @param enabled boolean indicating exception throwing on/off
*/
public void setExceptionOnContentLengthMismatch(boolean enabled) {
bExceptionOnContentLengthMismatch = enabled;
}
/**
* Close ARC writer and free its resources.
* @throws IOException if an I/O exception occurs while closing the writer
*/
public abstract void close() throws IOException;
/**
* Close the ARC record in an implementation specific way.
* @throws IOException if an I/O exception occurs while closing the record
*/
public abstract void closeRecord() throws IOException;
/**
* Closes the ARC record by writing one newline and comparing the amount of
* payload data streamed with the content-length supplied with the header.
* @throws IOException if an I/O exception occurs while closing the record
*/
protected void closeRecord_impl() throws IOException {
Diagnosis diagnosis = null;
out.write(ArcConstants.endMark);
out.flush();
if (headerContentLength == null) {
diagnosis = new Diagnosis(
DiagnosisType.ERROR_EXPECTED,
"'" + ArcConstants.FN_ARCHIVE_LENGTH + "' header",
"Mandatory!");
} else {
if (headerContentLength != payloadWrittenTotal) {
diagnosis = new Diagnosis(
DiagnosisType.INVALID_EXPECTED,
"'" + ArcConstants.FN_ARCHIVE_LENGTH + "' header",
Long.toString(payloadWrittenTotal),
headerContentLength.toString());
}
}
if (diagnosis != null) {
if (header != null) {
header.diagnostics.addError(diagnosis);
} else {
diagnostics.addError(diagnosis);
}
if (bExceptionOnContentLengthMismatch) {
throw new IllegalStateException("Payload size does not match content-length!");
}
}
header = null;
headerContentLength = null;
}
/**
* Write a raw ARC header to the ARC output stream. Closes any previously
* written record that has not been closed prior to this call.
* Errors and warnings are reported on the writers diagnostics object.
* @param header_bytes raw ARC record to output
* @param contentLength the expected content-length to be written and validated
* The contentLength is synonymous to the Archive-Length.
* @throws IOException if an exception occurs while writing header data
*/
public void writeRawHeader(byte[] header_bytes, Long contentLength) throws IOException {
if (header_bytes == null) {
throw new IllegalArgumentException(
"The 'header_bytes' parameter is null!");
}
if (contentLength != null && contentLength < 0) {
throw new IllegalArgumentException(
"The 'contentLength' parameter is negative!");
}
if (state == S_HEADER_WRITTEN) {
throw new IllegalStateException("Headers written back to back!");
} else if (state == S_PAYLOAD_WRITTEN) {
closeRecord_impl();
}
out.write(header_bytes);
state = S_HEADER_WRITTEN;
header = null;
headerContentLength = contentLength;
payloadWrittenTotal = 0;
}
/**
* Write a ARC header to the ARC output stream.
* Errors and warnings are reported on the records diagnostics object.
* @param record ARC record to output
* @return byte array version of header as it was written
* @throws IOException if an I/O exception occurs while writing header data
*/
public abstract byte[] writeHeader(ArcRecordBase record) throws IOException;
/**
* Write an ARC header to the ARC output stream.
* The WARC header is not required to be valid.
* Errors and warnings are reported on the records diagnostics object.
* @param record ARC record to output
* @return byte array version of header as it was written
* @throws IOException if an I/O exception occurs while writing header data
*/
protected byte[] writeHeader_impl(ArcRecordBase record) throws IOException {
header = record.header;
headerContentLength = header.archiveLength;
if (headerContentLength == null && header.archiveLengthStr != null) {
headerContentLength = fieldParsers.parseLong(header.archiveLengthStr, ArcConstants.FN_ARCHIVE_LENGTH, false);
if (headerContentLength != null && headerContentLength < 0) {
diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_EXPECTED, "'" + ArcConstants.FN_ARCHIVE_LENGTH + "' value", header.archiveLengthStr, "A non negative number"));
}
}
ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
/*
* URL
*/
String urlStr;
if (header.urlUri != null) {
urlStr = header.urlUri.toString();
} else if (header.urlStr != null && header.urlStr.length() > 0) {
urlStr = header.urlStr;
} else {
urlStr = "-";
}
outBuf.write(urlStr.getBytes());
/*
* IP-Address
*/
String ipAddressStr;
if (header.inetAddress != null) {
ipAddressStr = header.inetAddress.getHostAddress();
} else if (header.ipAddressStr != null && header.ipAddressStr.length() > 0) {
ipAddressStr = header.ipAddressStr;
} else {
ipAddressStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(ipAddressStr.getBytes());
/*
* Archive-Date
*/
String archiveDateStr;
if (header.archiveDate != null) {
archiveDateStr = ArcDateParser.getDateFormat().format(header.archiveDate);
} else if (header.archiveDateStr != null && header.archiveDateStr.length() > 0) {
archiveDateStr = header.archiveDateStr;
} else {
archiveDateStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(archiveDateStr.getBytes());
/*
* Content-Type
*/
String contentTypeStr;
if (header.contentType != null) {
contentTypeStr = header.contentType.toStringShort();
} else if (header.contentTypeStr != null && header.contentTypeStr.length() > 0) {
contentTypeStr = header.contentTypeStr;
} else {
contentTypeStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(contentTypeStr.getBytes());
/*
* Version 2 fields.
*/
if (header.recordFieldVersion == 2) {
/*
* Result-Code
*/
String resultCodeStr;
if (header.resultCode != null) {
resultCodeStr = header.resultCode.toString();
} else if (header.resultCodeStr != null && header.resultCodeStr.length() > 0) {
resultCodeStr = header.resultCodeStr;
} else {
resultCodeStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(resultCodeStr.getBytes());
/*
* Checksum
*/
String checksumStr;
if (header.checksumStr != null && header.checksumStr.length() > 0) {
checksumStr = header.checksumStr;
} else {
checksumStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(checksumStr.getBytes());
/*
* Location
*/
String locationStr;
if (header.locationStr != null && header.locationStr.length() > 0) {
locationStr = header.locationStr;
} else {
locationStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(locationStr.getBytes());
/*
* Offset
*/
String offsetStr;
if (header.offset != null) {
offsetStr = header.offset.toString();
} else if (header.offsetStr != null && header.offsetStr.length() > 0) {
offsetStr = header.offsetStr;
} else {
offsetStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(offsetStr.getBytes());
/*
* Filename
*/
String filenameStr;
if (header.filenameStr != null && header.filenameStr.length() > 0) {
filenameStr = header.filenameStr;
} else {
filenameStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(filenameStr.getBytes());
}
/*
* Archive-Length
*/
String archiveLengthStr;
if (header.archiveLength != null) {
archiveLengthStr = header.archiveLength.toString();
} else if (header.archiveLengthStr != null && header.archiveLengthStr.length() > 0) {
archiveLengthStr = header.archiveLengthStr;
} else {
archiveLengthStr = "-";
}
outBuf.write(" ".getBytes());
outBuf.write(archiveLengthStr.getBytes());
outBuf.write("\n".getBytes());
/*
* End Of Header
*/
byte[] headerBytes = outBuf.toByteArray();
out.write(headerBytes);
state = S_HEADER_WRITTEN;
payloadWrittenTotal = 0;
return headerBytes;
}
/**
* Stream the content of an input stream to the payload content.
* @param in input stream containing payload data
* @return number of bytes written during method invocation
* @throws IOException if an I/O exception occurs while writing payload data
*/
public long streamPayload(InputStream in) throws IOException {
if (in == null) {
throw new IllegalArgumentException(
"The 'in' parameter is null!");
}
if (state != S_HEADER_WRITTEN && state != S_PAYLOAD_WRITTEN) {
throw new IllegalStateException("Write a header before writing payload!");
}
long written = 0;
int read = 0;
while (read != -1) {
out.write(stream_copy_buffer, 0, read);
written += read;
read = in.read(stream_copy_buffer);
}
state = S_PAYLOAD_WRITTEN;
payloadWrittenTotal += written;
return written;
}
/**
* Append the content of a byte array to the payload content.
* @param b byte array with data to be written
* @return number of bytes written during method invocation
* @throws IOException if an I/O exception occurs while writing payload data
*/
public long writePayload(byte[] b) throws IOException {
if (state != S_HEADER_WRITTEN && state != S_PAYLOAD_WRITTEN) {
throw new IllegalStateException("Write a header before writing payload!");
}
out.write(b);
state = S_PAYLOAD_WRITTEN;
payloadWrittenTotal += b.length;
return b.length;
}
/**
* Append the partial content of a byte array to the payload content.
* @param b byte array with partial data to be written
* @param offset offset to data to be written
* @param len length of data to be written
* @return number of bytes written during method invocation
* @throws IOException if an I/O exception occurs while writing payload data
*/
public long writePayload(byte[] b, int offset, int len) throws IOException {
if (state != S_HEADER_WRITTEN && state != S_PAYLOAD_WRITTEN) {
throw new IllegalStateException("Write a header before writing payload!");
}
out.write(b, offset, len);
state = S_PAYLOAD_WRITTEN;
payloadWrittenTotal += len;
return len;
}
}