Class FileBackedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.opendaylight.controller.cluster.io.FileBackedOutputStream
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
- Direct Known Subclasses:
SharedFileBackedOutputStream
public class FileBackedOutputStream extends OutputStream
AnOutputStream
that starts buffering to a byte array, but switches to file buffering once the data reaches a configurable size. This class is thread-safe.- Author:
- Thomas Pantelis
-
-
Constructor Summary
Constructors Constructor Description FileBackedOutputStream(int fileThreshold, @Nullable String fileDirectory)
Creates a new instance that uses the given file threshold, and does not reset the data when theByteSource
returned byasByteSource()
is finalized.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description @NonNull ByteSource
asByteSource()
Returns a readableByteSource
view of the data that has been written to this stream.void
cleanup()
Callsclose()
if not already closed and, if data was buffered to a file, deletes the file.void
close()
void
flush()
long
getCount()
void
write(byte[] bytes)
void
write(byte[] bytes, int off, int len)
void
write(int value)
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
FileBackedOutputStream
public FileBackedOutputStream(int fileThreshold, @Nullable String fileDirectory)
Creates a new instance that uses the given file threshold, and does not reset the data when theByteSource
returned byasByteSource()
is finalized.- Parameters:
fileThreshold
- the number of bytes before the stream should switch to buffering to a filefileDirectory
- the directory in which to create the file if needed. If null, the default temp file location is used.
-
-
Method Detail
-
asByteSource
public @NonNull ByteSource asByteSource() throws IOException
Returns a readableByteSource
view of the data that has been written to this stream. This stream is closed and further attempts to write to it will result in an IOException.- Returns:
- a ByteSource instance
- Throws:
IOException
- if close fails
-
write
public void write(int value) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] bytes) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] bytes, int off, int len) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
flush
public void flush() throws IOException
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
getCount
public long getCount()
-
cleanup
public void cleanup()
Callsclose()
if not already closed and, if data was buffered to a file, deletes the file.
-
-