-
Notifications
You must be signed in to change notification settings - Fork 2
/
ArcReader.java
479 lines (410 loc) · 14.8 KB
/
ArcReader.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/**
* 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.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.jwat.common.Diagnosis;
import org.jwat.common.Diagnostics;
import org.jwat.common.Digest;
import org.jwat.common.HeaderLineReader;
import org.jwat.common.UriProfile;
/**
* ARC Reader base class.
*
* @author nicl
*/
public abstract class ArcReader implements Closeable, Iterable<ArcRecordBase> {
/*
* Settings.
*/
/** Strict mode enabled or disabled.
* Non strict mode allows newlines when there should be no xml metadata.
* Non strict mode allows a varied number of newlines between records. */
protected boolean bStrict = false;
/** URL URI profile. */
protected UriProfile uriProfile;
/** Block Digest enabled/disabled. */
protected boolean bBlockDigest = false;
/** Optional block digest algorithm to use. */
protected String blockDigestAlgorithm;
/** Encoding scheme used to encode block digest into a string. */
protected String blockDigestEncoding = "base32";
/** Payload Digest enabled/disabled. */
protected boolean bPayloadDigest = false;
/** Optional payload digest algorithm to use. */
protected String payloadDigestAlgorithm;
/** Encoding scheme used to encode payload digest into a string. */
protected String payloadDigestEncoding = "base32";
/** Max size allowed for a record header. */
protected int recordHeaderMaxSize;
/** Max size allowed for a payload header. */
protected int payloadHeaderMaxSize;
/** ARC field parser used. */
protected ArcFieldParsers fieldParsers;
/** Line reader used to read header lines. */
protected HeaderLineReader lineReader;
/*
* State.
*/
/** Reader level errors and warnings or when no record is available. */
public final Diagnostics<Diagnosis> diagnostics = new Diagnostics<Diagnosis>();
/** Compliance status for records parsed up to now. */
protected boolean bIsCompliant = true;
/** Number of bytes consumed by this reader. */
protected long consumed = 0;
/** Records parsed. */
protected int records = 0;
/** Aggregated number of errors encountered while parsing. */
protected int errors = 0;
/** Aggregate number of warnings encountered while parsing. */
protected int warnings = 0;
/** Version header from version block. */
public ArcVersionHeader versionHeader;
/** Current record of either kind. */
protected ArcRecordBase currentRecord = null;
/** Exception thrown while using the iterator. */
protected Exception iteratorExceptionThrown;
/** Callback used to monitor and modify internal data as it is being parsed. */
protected ArcRecordParserCallback arpCallback;
/**
* Method used to initialize a readers internal state.
*/
protected void init() {
uriProfile = UriProfile.RFC3986;
recordHeaderMaxSize = 8192;
payloadHeaderMaxSize = 32768;
lineReader = HeaderLineReader.getReader();
lineReader.bNameValue = false;
lineReader.encoding = HeaderLineReader.ENC_US_ASCII;
fieldParsers = new ArcFieldParsers();
}
/**
* Reset reader for reuse.
*/
public void reset() {
diagnostics.reset();
bIsCompliant = true;
consumed = 0;
records = 0;
errors = 0;
warnings = 0;
versionHeader = null;
currentRecord = null;
}
/**
* Returns a boolean indicating whether the reader has only parsed
* compliant records up to now.
* @return a boolean indicating all compliant records parsed to far
*/
public boolean isCompliant() {
return bIsCompliant;
}
/**
* Is this reader assuming GZip compressed input.
* @return boolean indicating the assumption of GZip compressed input
*/
public abstract boolean isCompressed();
/**
* Set the readers strict mode on/off.
* @param bStrict strict mode on/off
*/
public void setStrict(boolean bStrict) {
this.bStrict = bStrict;
}
/**
* Get the readers strict mode setting.
* @return readers strict mode setting
*/
public boolean isStrict() {
return bStrict;
}
/**
* Set the URI profile used to validate URL URIs.
* If null, the uriProfile is set to RCF3986.
* @param uriProfile URI profile to use
*/
public void setUriProfile(UriProfile uriProfile) {
if (uriProfile == null) {
uriProfile = UriProfile.RFC3986;
}
this.uriProfile = uriProfile;
}
/**
* Get the URI profile used to validate URL URIs.
* @return the URI profile used to validate URL URIs
*/
public UriProfile getUriProfile() {
return uriProfile;
}
/**
* Get the readers block digest on/off status.
* @return boolean indicating block digest on/off
*/
public boolean getBlockDigestEnabled() {
return bBlockDigest;
}
/**
* Set the readers block digest on/off status. Digest, however,
* will only be computed if an algorithm has also been chosen.
* @param enabled boolean indicating block digest on/off
*/
public void setBlockDigestEnabled(boolean enabled) {
bBlockDigest = enabled;
}
/**
* Get the readers payload digest on/off status.
* @return boolean indicating payload digest on/off
*/
public boolean getPayloadDigestEnabled() {
return bPayloadDigest;
}
/**
* Set the readers payload digest on/off status. Digest, however,
* will only be computed if an algorithm has also been chosen.
* @param enabled boolean indicating payload digest on/off
*/
public void setPayloadDigestEnabled(boolean enabled) {
bPayloadDigest = enabled;
}
/**
* Get the optional block digest algorithm.
* @return optional block digest algorithm
*/
public String getBlockDigestAlgorithm() {
return blockDigestAlgorithm;
}
/**
* Tries to set the optional block digest algorithm and returns a boolean
* indicating whether the algorithm was accepted or not.
* @param digestAlgorithm block digest algorithm
* (null means no optional block digest algorithm is selected)
* @return boolean indicating the validity of the algorithm supplied
*/
public boolean setBlockDigestAlgorithm(String digestAlgorithm) {
if (digestAlgorithm == null || digestAlgorithm.length() == 0) {
blockDigestAlgorithm = null;
return true;
}
if (Digest.digestAlgorithmLength(digestAlgorithm) > 0) {
blockDigestAlgorithm = digestAlgorithm;
return true;
}
return false;
}
/**
* Get the optional payload digest algorithm.
* @return optional payload digest algorithm
*/
public String getPayloadDigestAlgorithm() {
return payloadDigestAlgorithm;
}
/**
* Tries to set the optional payload digest algorithm and returns a boolean
* indicating whether the algorithm was accepted or not.
* @param digestAlgorithm payload digest algorithm
* (null means no optional payload digest algorithm is selected)
* @return boolean indicating the validity of the algorithm supplied
*/
public boolean setPayloadDigestAlgorithm(String digestAlgorithm) {
if (digestAlgorithm == null || digestAlgorithm.length() == 0) {
payloadDigestAlgorithm = null;
return true;
}
if (Digest.digestAlgorithmLength(digestAlgorithm) > 0) {
payloadDigestAlgorithm = digestAlgorithm;
return true;
}
return false;
}
/**
* Get the optional block digest encoding scheme.
* @return optional block digest encoding scheme
*/
public String getBlockDigestEncoding() {
return blockDigestEncoding;
}
/**
* Set the optional block digest encoding scheme.
* @param encodingScheme encoding scheme
* (null means optional block digest is not encoded)
*/
public void setBlockDigestEncoding(String encodingScheme) {
if (encodingScheme != null && encodingScheme.length() > 0) {
blockDigestEncoding = encodingScheme.toLowerCase();
} else {
blockDigestEncoding = null;
}
}
/**
* Get the optional payload digest encoding scheme.
* @return optional payload digest encoding scheme
*/
public String getPayloadDigestEncoding() {
return payloadDigestEncoding;
}
/**
* Set the optional payload digest encoding scheme.
* @param encodingScheme encoding scheme
* (null means optional payload digest is not encoded)
*/
public void setPayloadDigestEncoding(String encodingScheme) {
if (encodingScheme != null && encodingScheme.length() > 0) {
payloadDigestEncoding = encodingScheme.toLowerCase();
} else {
payloadDigestEncoding = null;
}
}
/**
* Get the max size allowed for a record header.
* @return max size allowed for a record header
*/
public int getRecordHeaderMaxSize() {
return recordHeaderMaxSize;
}
/**
* Set the max size allowed for a record header.
* @param size max size allowed
*/
public void setRecordHeaderMaxSize(int size) {
recordHeaderMaxSize = size;
}
/**
* Get the max size allowed for a payload header.
* @return max size allowed for a payload header
*/
public int getPayloadHeaderMaxSize() {
return payloadHeaderMaxSize;
}
/**
* Set the max size allowed for a payload header.
* @param size max size allowed
*/
public void setPayloadHeaderMaxSize(int size) {
payloadHeaderMaxSize = size;
}
/**
* Close current record resource(s) and input stream(s).
*/
public abstract void close();
/**
* Callback method called when the payload has been processed.
*/
protected abstract void recordClosed();
/**
* Get the offset of the current ARC record or -1 if none have been read.
* @return offset of the current ARC record or -1
*/
public abstract long getStartOffset();
/**
* Get the current offset in the ARC <code>InputStream</code>.
* @return offset in ARC <code>InputStream</code>
* @see ArcRecordBase#getOffset()
*/
public abstract long getOffset();
/**
* Get number of bytes consumed by this reader.
* @return number of bytes consumed by this reader
*/
public abstract long getConsumed();
/**
* Parses and gets the next ARC record.
* @return the next ARC record
* @throws IOException I/O exception in reading process
*/
public abstract ArcRecordBase getNextRecord() throws IOException;
/**
* Parses and gets the next ARC record.
* @param in ARC record <code>InputStream</code>
* @param offset offset provided by caller
* @return the next ARC record
* @throws IOException I/O exception in reading process
*/
public abstract ArcRecordBase getNextRecordFrom(InputStream in, long offset)
throws IOException;
/**
* Parses and gets the next ARC record.
* @param in ARC record <code>InputStream</code>
* @param buffer_size size of buffer used to wrap <code>InputStream</code>
* @param offset offset provided by caller
* @return the next ARC record
* @throws IOException I/O exception in reading process
*/
public abstract ArcRecordBase getNextRecordFrom(InputStream in,
long offset, int buffer_size) throws IOException;
/**
* Gets an exception thrown in the iterator if any or null.
* @return exception thrown in the iterator if any or null
*/
public Exception getIteratorExceptionThrown() {
return iteratorExceptionThrown;
}
public ArcRecordParserCallback getArcRecordParserCallback() {
return arpCallback;
}
public void setArcRecordParserCallback(ArcRecordParserCallback arpCallback) {
this.arpCallback = arpCallback;
}
/**
* Returns an <code>Iterator</code> over the records as they are being
* parsed. Any exception thrown during parsing is accessible through the
* <code>getIteratorExceptionThrown</code> method.
* @return <code>Iterator</code> over the <code>WARC</code> records
*/
public Iterator<ArcRecordBase> iterator() {
return new Iterator<ArcRecordBase>() {
private ArcRecordBase next;
private ArcRecordBase current;
@Override
public boolean hasNext() {
if (next == null) {
iteratorExceptionThrown = null;
try {
next = getNextRecord();
} catch (IOException e) {
iteratorExceptionThrown = e;
}
}
return (next != null);
}
@Override
public ArcRecordBase next() {
if (next == null) {
iteratorExceptionThrown = null;
try {
next = getNextRecord();
} catch (IOException e) {
iteratorExceptionThrown = e;
}
}
if (next == null) {
throw new NoSuchElementException();
}
current = next;
next = null;
return current;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}