Package net.snowflake.client.jdbc
Class FileBackedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- net.snowflake.client.jdbc.FileBackedOutputStream
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
@Beta public final 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.
Adapted by Snowflake to return File object when file is spilled to disk.
- Since:
- 1.0
- Author:
- Chris Nokleberg
-
-
Constructor Summary
Constructors Constructor Description FileBackedOutputStream(int fileThreshold)
Creates a new instance that uses the given file threshold, and does not reset the data when theByteSource
returned byasByteSource()
is finalized.FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize)
Creates a new instance that uses the given file threshold, and optionally resets the data when theByteSource
returned byasByteSource()
is finalized.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description com.google.common.io.ByteSource
asByteSource()
void
close()
void
flush()
File
getFile()
void
reset()
Callsclose()
if not already closed, and then resets this object back to its initial state, for reuse.void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
FileBackedOutputStream
public FileBackedOutputStream(int fileThreshold)
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 file
-
FileBackedOutputStream
public FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize)
Creates a new instance that uses the given file threshold, and optionally resets the data when theByteSource
returned byasByteSource()
is finalized.- Parameters:
fileThreshold
- the number of bytes before the stream should switch to buffering to a fileresetOnFinalize
- if true, thereset()
method will be called when theByteSource
returned byasByteSource()
is finalized
-
-
Method Detail
-
getFile
public File getFile()
- Returns:
- the file holding the data (possibly null).
-
asByteSource
public com.google.common.io.ByteSource asByteSource()
- Returns:
- a readable
ByteSource
view of the data that has been written to this stream. - Since:
- 15.0
-
reset
public void reset() throws IOException
Callsclose()
if not already closed, and then resets this object back to its initial state, for reuse. If data was buffered to a file, it will be deleted.- Throws:
IOException
- if an I/O error occurred while deleting the file buffer
-
write
public void write(int b) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b, 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
-
-