Package com.norconex.commons.lang.io
Class CachedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.norconex.commons.lang.io.CachedOutputStream
-
- All Implemented Interfaces:
ICachedStream,Closeable,Flushable,AutoCloseable
public class CachedOutputStream extends OutputStream implements ICachedStream
OutputStreamwrapper that caches the output so it can be retrieved once as aCachedInputStream. InvokinggetInputStream()effectivelyclose()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 ofCachedOutputStream, use theCachedStreamFactoryclass. Reusing the same factory will ensure allCachedOutputStreaminstances created share the same combined maximum memory. Invoking one of thenewOutputStream(...)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
- Author:
- Pascal Essiembre
- See Also:
CachedStreamFactory
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()protected voidfinalize()PathgetCacheDirectory()Gets the cache directory where temporary cache files are created.CachedInputStreamgetInputStream()longgetMemCacheSize()CachedStreamFactorygetStreamFactory()booleanisCacheEmpty()Returnstrueif was nothing to cache (no writing was performed) or if the stream was closed.CachedOutputStreamnewOuputStream()CachedOutputStreamnewOuputStream(OutputStream os)voidwrite(byte[] b, int off, int len)voidwrite(int b)-
Methods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
-
-
-
Method Detail
-
write
public void write(int b) throws IOException- Specified by:
writein classOutputStream- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
getInputStream
public CachedInputStream getInputStream() throws IOException
- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
getCacheDirectory
public final Path getCacheDirectory()
Gets the cache directory where temporary cache files are created.- Specified by:
getCacheDirectoryin interfaceICachedStream- Returns:
- the cache directory
-
getStreamFactory
public CachedStreamFactory getStreamFactory()
-
isCacheEmpty
public boolean isCacheEmpty()
Returnstrueif was nothing to cache (no writing was performed) or if the stream was closed.- Returns:
trueif empty
-
newOuputStream
public CachedOutputStream newOuputStream(OutputStream os)
-
newOuputStream
public CachedOutputStream newOuputStream()
-
getMemCacheSize
public long getMemCacheSize()
- Specified by:
getMemCacheSizein interfaceICachedStream
-
-