Class AbstractOutputFile
- All Implemented Interfaces:
OutputFile, Closeable, AutoCloseable
- Direct Known Subclasses:
InMemoryOutputFile, S3OutputFile
A base for implementing OutputFile that enforces its lifecycle.
The public methods are final: each checks the state the lifecycle table in OutputFile
allows it in, throws IllegalStateException otherwise, and calls the protected do method
that does the work. position() counts the bytes doWrite(ByteBuffer) accepted. A
subclass implements doWrite and overrides the others it needs; they do nothing by default:
| Method | Called by, while the file is open | Does |
|---|---|---|
doCreate() |
create() |
acquires the destination's resources |
doWrite(ByteBuffer) |
write |
appends the bytes |
doClose() |
close() |
publishes the file at the destination |
doDiscard() |
discard(), or close() when doClose() throws |
releases the resources and throws away what was written |
Each do method other than doWrite is called at most once per file. When doCreate()
throws, the file ends discarded without a call to doDiscard(), so doCreate() must leave
nothing held when it fails. When doClose() throws, the file ends discarded: doDiscard()
runs, a failure of it is attached to the doClose() failure as suppressed, and the
doClose() failure is rethrown. The file is discarded before doDiscard() runs and is not
discarded again, so doDiscard() must release what it can before it throws.
This API is Experimental: the shape may change in future releases.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()Publishes an open file.final voidcreate()Performs resource acquisition (e.g. opening a file channel).final voiddiscard()Discards an open file.protected voiddoClose()Publishes the file at the destination.protected voiddoCreate()Acquires the destination's resources, such as opening a file channel.protected voidReleases the resources and throws away what was written, leaving the destination as if nothing was written.protected abstract voiddoWrite(ByteBuffer data) Appends the remaining bytes ofdata, consuming the buffer.final longposition()Returns the number of bytes written so far, which is the offset at which the nextOutputFile.write(ByteBuffer)will begin.protected final voidThrows unless the file is published, for a subclass that hands out the finished file.final voidwrite(ByteBuffer data) Appends the remaining bytes ofdatato the file.
-
Constructor Details
-
AbstractOutputFile
- Parameters:
destination- the destination, as theIllegalStateExceptions name it, such as a path or a URI
-
-
Method Details
-
create
Description copied from interface:OutputFilePerforms resource acquisition (e.g. opening a file channel). Must be called beforeOutputFile.write(ByteBuffer)orOutputFile.position().- Specified by:
createin interfaceOutputFile- Throws:
IOException- if the resource cannot be acquired
-
write
Description copied from interface:OutputFileAppends the remaining bytes of
datato the file.The buffer is consumed (its position advances to its limit).
- Specified by:
writein interfaceOutputFile- Parameters:
data- the bytes to append- Throws:
IOException- if the write fails
-
position
public final long position()Description copied from interface:OutputFileReturns the number of bytes written so far, which is the offset at which the nextOutputFile.write(ByteBuffer)will begin.- Specified by:
positionin interfaceOutputFile- Returns:
- the running byte offset
-
close
Publishes an open file. Does nothing on a file that is published or discarded, and ends an uncreated file as discarded without publishing anything.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceOutputFile- Throws:
IOException- if the file cannot be published
-
discard
Discards an open file. Does nothing on a file that is published or discarded, and ends an uncreated file as discarded.- Specified by:
discardin interfaceOutputFile- Throws:
IOException- if resources cannot be released
-
requireClosed
protected final void requireClosed()Throws unless the file is published, for a subclass that hands out the finished file.- Throws:
IllegalStateException- if the file is not published
-
doCreate
Acquires the destination's resources, such as opening a file channel. Does nothing by default.- Throws:
IOException- if the resources cannot be acquired; the file then ends discarded
-
doWrite
Appends the remaining bytes ofdata, consuming the buffer. Returning with bytes left indatamakeswritethrowIllegalStateException, sinceposition()would no longer match the file.- Parameters:
data- the bytes to append- Throws:
IOException- if the write fails
-
doClose
Publishes the file at the destination. May leave resources held when it fails, fordoDiscard()to release. Does nothing by default.- Throws:
IOException- if the file cannot be published; the file then ends discarded
-
doDiscard
Releases the resources and throws away what was written, leaving the destination as if nothing was written. Releases what it can before it throws, since it is not called again. Does nothing by default.- Throws:
IOException- if the resources cannot be released
-