Class ArchiveReader
java.lang.Object
org.archive.io.ArchiveReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Iterable<ArchiveRecord>
- Direct Known Subclasses:
ARCReader,WARCReader
Reader for an Archive file of Archive
ArchiveRecords.- Version:
- $Date$ $Version$
- Author:
- stack
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classInner ArchiveRecord Iterator class. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected InputStreamArchive file input stream.static final intMaximum amount of recoverable exceptions in a row. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcdxOutput(boolean toFile) protected voidCleanout the current record if there is one.voidclose()protected abstract ArchiveRecordcreateArchiveRecord(InputStream is, long offset) Return an Archive Record homed onoffsetintois.protected ArchiveRecordabstract voiddump(boolean compress) Dump this file on STDOUTget()get(long offset) Get record at passedoffset.protected ArchiveRecordabstract ArchiveReaderabstract Stringabstract Stringprotected InputStreamgetIn()protected InputStreamgetInputStream(File f, long offset) Convenience method for constructors.protected Loggerprotected static org.apache.commons.cli.Optionsstatic StringgetStrippedFileName(String name, String dotFileExtension) protected static booleangetTrueOrFalse(String value) protected abstract voidgotoEOR(ArchiveRecord record) Skip over any trailing new lines at end of the record so we're lined up ready to read the next.protected voidinitialize(String i) Convenience method used by subclass constructors.booleanbooleanisDigest()booleanisStrict()booleanisValid()Test Archive file is valid.iterator()Returns an ArchiveRecord iterator.voidLog on stderr.protected booleanbooleanoutputRecord(String format) Output passed record using passed format specifier.protected static voidoutputRecord(ArchiveReader r, String format) Output passed record using passed format specifier.protected static longprotected voidsetCompressed(boolean compressed) voidsetDigest(boolean d) protected voidsetIn(InputStream in) protected voidvoidsetStrict(boolean s) protected voidsetVersion(String version) protected static StringstripExtension(String name, String ext) validate()Validate the Archive file.validate(int numRecords) Validate the Archive file.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
in
Archive file input stream. Keep it around so we can close it when done.Set in constructor. Should support at least 1 byte mark/reset. Make it protected so subclasses have access.
-
MAX_ALLOWED_RECOVERABLES
public static final int MAX_ALLOWED_RECOVERABLESMaximum amount of recoverable exceptions in a row. If more than this amount in a row, we'll let out the exception rather than go back in for yet another retry.- See Also:
-
-
Constructor Details
-
ArchiveReader
protected ArchiveReader()
-
-
Method Details
-
initialize
Convenience method used by subclass constructors.- Parameters:
i- Identifier for Archive file this reader goes against.
-
getInputStream
Convenience method for constructors.- Parameters:
f- File to read.offset- Offset at which to start reading.- Returns:
- InputStream to read from.
- Throws:
IOException- If failed open or fail to get a memory mapped byte buffer on file.
-
isCompressed
public boolean isCompressed() -
get
Get record at passedoffset.- Parameters:
offset- Byte index into file at which a record starts.- Returns:
- An Archive Record reference.
- Throws:
IOException
-
get
- Returns:
- Return Archive Record created against current offset.
- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
cleanupCurrentRecord
Cleanout the current record if there is one.- Throws:
IOException
-
createArchiveRecord
protected abstract ArchiveRecord createArchiveRecord(InputStream is, long offset) throws IOException Return an Archive Record homed onoffsetintois.- Parameters:
is- Stream to read Record from.offset- Offset to find Record at.- Returns:
- ArchiveRecord instance.
- Throws:
IOException
-
gotoEOR
Skip over any trailing new lines at end of the record so we're lined up ready to read the next.- Parameters:
record-- Throws:
IOException
-
getFileExtension
-
getDotFileExtension
-
getVersion
- Returns:
- Version of this Archive file.
-
validate
Validate the Archive file. This method iterates over the file throwing exception if it fails to successfully parse any record.Assumes the stream is at the start of the file.
- Returns:
- List of all read Archive Headers.
- Throws:
IOException
-
validate
Validate the Archive file. This method iterates over the file throwing exception if it fails to successfully parse.We start validation from wherever we are in the stream.
- Parameters:
numRecords- Number of records expected. Pass -1 if number is unknown.- Returns:
- List of all read metadatas. As we validate records, we add a reference to the read metadata.
- Throws:
IOException
-
isValid
public boolean isValid()Test Archive file is valid. Assumes the stream is at the start of the file. Be aware that this method makes a pass over the whole file.- Returns:
- True if file can be successfully parsed.
-
isStrict
public boolean isStrict()- Returns:
- Returns the strict.
-
setStrict
public void setStrict(boolean s) - Parameters:
s- The strict to set.
-
setDigest
public void setDigest(boolean d) - Parameters:
d- True if we're to digest.
-
isDigest
public boolean isDigest()- Returns:
- True if we're digesting as we read.
-
getLogger
-
iterator
Returns an ArchiveRecord iterator. Of note, on IOException, especially if ZipException reading compressed ARCs, rather than fail the iteration, try moving to the next record. Ifstrictis not set, this will usually succeed.- Specified by:
iteratorin interfaceIterable<ArchiveRecord>- Returns:
- An iterator over ARC records.
-
setCompressed
protected void setCompressed(boolean compressed) -
getCurrentRecord
- Returns:
- The current ARC record or null if none. After construction has the arcfile header record.
- See Also:
-
currentRecord
-
getIn
-
setIn
-
setVersion
-
getReaderIdentifier
-
setReaderIdentifier
-
logStdErr
Log on stderr. Logging should go via the logging system. This method bypasses the logging system going direct to stderr. Should not generally be used. Its used for rare messages that come of cmdline usage of ARCReader ERRORs and WARNINGs. Override if using ARCReader in a context where no stderr or where you'd like to redirect stderr to other than System.err.- Parameters:
level- Level to log message at.message- Message to log.
-
positionForRecord
-
stripExtension
-
getFileName
- Returns:
- short name of Archive file.
-
getStrippedFileName
- Returns:
- short name of Archive file.
-
getStrippedFileName
-
getTrueOrFalse
- Parameters:
value- Value to test.- Returns:
- True if value is 'true', else false.
-
output
- Parameters:
format- Format to use outputting.- Returns:
- True if handled.
- Throws:
IOExceptionParseException
-
cdxOutput
- Throws:
IOException
-
outputRecord
Output passed record using passed format specifier.- Parameters:
format- What format to use outputting.- Returns:
- True if handled.
- Throws:
IOException
-
dump
Dump this file on STDOUT- Parameters:
compress- True if dumped output is compressed.- Throws:
IOExceptionParseException
-
getDeleteFileOnCloseReader
- Returns:
- an ArchiveReader that will delete a local file on close. Used when we bring Archive files local and need to clean up afterward.
-
outputRecord
Output passed record using passed format specifier.- Parameters:
r- ARCReader instance to output.format- What format to use outputting.- Throws:
IOException
-
getOptions
protected static org.apache.commons.cli.Options getOptions()- Returns:
- Base Options object filled out with help, digest, strict, etc. options.
-