Package com.norconex.commons.lang.io
Class CachedOutputStream
java.lang.Object
java.io.OutputStream
com.norconex.commons.lang.io.CachedOutputStream
- All Implemented Interfaces:
CachedStream,Closeable,Flushable,AutoCloseable
OutputStream wrapper that caches the output so it can be retrieved
once as a CachedInputStream. Invoking getInputStream()
effectively close() this stream and it can no longer be written
to. Obtaining an input stream before or instead of calling the close
method will not delete the cache content, but rather pass the reference
to it to the CachedInputStream.
To create new instances of
CachedOutputStream, use the
CachedStreamFactory class. Reusing the same factory
will ensure all CachedOutputStream instances created share the same
combined maximum memory. Invoking one of the
newOutputStream(...) methods on this class have the same effect.
The internal cache stores written bytes into memory, up to to the specified maximum cache size. If content exceeds the cache limit, the cache transforms itself into a file-based cache of unlimited size. Default memory cache size is 128 KB.
- Since:
- 1.5
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voiddispose()Clear the cache attached to this output stream.voidflush()final PathGets the cache directory where temporary cache files are created.Return the cached content of the of this output stream as an input stream, before disposing it.longGet the amount of bytes used for caching this stream.booleanReturnstrueif was nothing to cache (no writing was performed) or if the stream was closed.voidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
Method Details
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
getInputStream
Return the cached content of the of this output stream as an input stream, before disposing it.- Returns:
- cached input stream
- Throws:
IOException- problem getting input stream
-
dispose
Clear the cache attached to this output stream.- Throws:
IOException- could not dispose- Since:
- 3.0.0
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
getCacheDirectory
Gets the cache directory where temporary cache files are created.- Specified by:
getCacheDirectoryin interfaceCachedStream- Returns:
- the cache directory
-
getStreamFactory
-
isCacheEmpty
public boolean isCacheEmpty()Returnstrueif was nothing to cache (no writing was performed) or if the stream was closed.- Returns:
trueif empty
-
newOuputStream
-
newOuputStream
-
getMemCacheSize
public long getMemCacheSize()Description copied from interface:CachedStreamGet the amount of bytes used for caching this stream.- Specified by:
getMemCacheSizein interfaceCachedStream- Returns:
- amount of bytes
-