Class WriterPoolMember
java.lang.Object
org.archive.io.WriterPoolMember
- Direct Known Subclasses:
ARCWriter,WARCWriter
Member of
WriterPool.
Implements rotating off files, file naming with some guarantee of
uniqueness, and position in file. Subclass to pick up functionality for a
particular Writer type.- Version:
- $Date$ $Revision$
- Author:
- stack
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected MiserOutputStreamCounting stream for meteringprotected Stringprotected StringCreation date for the current file.static final StringDefault for file prefix.static final StringDefault archival-aggregate filename template.protected FileReference to file we're currently writing.protected OutputStreamOutput stream for file.protected static intDirectories round-robin index.protected final byte[]Buffer to reuse writing streams.protected static NumberFormatNumberFormat instance for formatting serial number.protected static DecimalFormatSymbolsSymbol set for serial number formatter.protected WriterPoolSettingsstatic final String -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedWriterPoolMember(AtomicInteger serialNo, OutputStream out, File file, WriterPoolSettings settings) Constructor.WriterPoolMember(AtomicInteger serialNo, WriterPoolSettings settings, String extension) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidCall this method just before/after any significant write.protected Filevoidclose()protected longcopyFrom(InputStream is, long recordLength, boolean enforceLength) Copy bytes from the provided InputStream to the target file/stream being written.protected StringCreate a new file.protected StringcreateFile(File file) protected voidflush()protected voidGenerate a new basename by interpolating values in the configured template.protected StringGet the file namegetFile()Get this file.protected FilegetNextDirectory(List<File> dirs) protected OutputStreamlongPosition in raw output (typically, physical file).booleanbooleanCheck if underlying file has already reached its target size.protected voidPost file write tasks.protected voidPost write tasks.protected voidwrite(byte[] b) protected voidwrite(byte[] b, int off, int len) protected voidwrite(int b)
-
Field Details
-
UTF8
-
DEFAULT_TEMPLATE
Default archival-aggregate filename template. Under usual assumptions -- hostnames aren't shared among crawling hosts; processes have unique PIDs and admin ports; timestamps inside one process don't repeat (see UniqueTimestampService); clocks are generally accurate -- will generate a unique name. Stands for Internet Archive Heritrix.- See Also:
-
DEFAULT_PREFIX
-
f
Reference to file we're currently writing. -
out
Output stream for file. -
countOut
Counting stream for metering -
settings
-
currentTimestamp
Creation date for the current file. Set bycreateFile(). -
currentBasename
-
roundRobinIndex
protected static int roundRobinIndexDirectories round-robin index. -
serialNoFormatterSymbols
Symbol set for serial number formatter. -
serialNoFormatter
NumberFormat instance for formatting serial number. Pads serial number with zeros. -
scratchbuffer
protected final byte[] scratchbufferBuffer to reuse writing streams.
-
-
Constructor Details
-
WriterPoolMember
protected WriterPoolMember(AtomicInteger serialNo, OutputStream out, File file, WriterPoolSettings settings) throws IOException Constructor. Takes a stream. Use with caution. There is no upperbound check on size. Will just keep writing.- Parameters:
serialNo- used to create unique filename sequencesout- Where to write.file- File theoutis connected to.- Throws:
IOException
-
WriterPoolMember
Constructor.- Parameters:
serialNo- used to create unique filename sequencesextension- Extension to give file.
-
-
Method Details
-
checkSize
Call this method just before/after any significant write. Call at the end of the writing of a record or just before we start writing a new record. Will close current file and open a new file if file size has passed out maxSize.Creates and opens a file if none already open. One use of this method then is after construction, call this method to add the metadata, then call
getPosition()to find offset of first record. TODO: perhaps this should be called checkForNewOpen? because it also handles initial open, even when not rolling oversize- Throws:
IOException
-
isOversize
public boolean isOversize()Check if underlying file has already reached its target size.- Returns:
- boolean true if file has reached target size and due to be closed
-
createFile
Create a new file. Rotates off the current Writer and creates a new in its place to take subsequent writes. Usually called fromcheckSize().- Returns:
- Name of file created.
- Throws:
IOException
-
createFile
- Throws:
IOException
-
getNextDirectory
- Parameters:
dirs- List of File objects that point at directories.- Returns:
- Find next directory to write an arc too. If more than one, it tries to round-robin through each in turn.
- Throws:
IOException
-
checkWriteable
-
generateNewBasename
protected void generateNewBasename()Generate a new basename by interpolating values in the configured template. Values come from local state, other configured values, and global system properties. The recommended default template will generate a unique basename under reasonable assumptions. -
getBaseFilename
-
getFile
Get this file. Used by junit test to test for creation and whenWriterPoolwants to invalidate a file.- Returns:
- The current file.
-
preWriteRecordTasks
Post write tasks. Has side effects. Will open new file if we're at the upper bound. If we're writing compressed files, it will wrap output stream with a GZIP writer with side effect that GZIP header is written out on the stream.- Throws:
IOException
-
postWriteRecordTasks
Post file write tasks. If compressed, finishes up compression and flushes stream so any subsequent checks get good reading.- Throws:
IOException
-
getPosition
public long getPosition()Position in raw output (typically, physical file). Used making accounting of bytes written.- Returns:
- Position in final media (assuming all flushing completes)
-
isCompressed
public boolean isCompressed() -
write
- Throws:
IOException
-
flush
- Throws:
IOException
-
write
- Throws:
IOException
-
write
- Throws:
IOException
-
copyFrom
protected long copyFrom(InputStream is, long recordLength, boolean enforceLength) throws IOException Copy bytes from the provided InputStream to the target file/stream being written.- Parameters:
is- InputStream to copy bytes fromrecordLength- expected number of bytes to copyenforceLength- whether to throw an exception if too many/too few bytes are available from stream- Returns:
- number of bytes written (normally equal to
enforceLength) - Throws:
IOException
-
close
- Throws:
IOException
-
getOutputStream
-