X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsink%2FFileSink.java;h=50113212a3906bb7e2304ec4846367a4e07ac077;hb=633a841142f978235ed9f745b6ba16c278963e62;hp=015dd6fa3734fe3aa49906f07c0a900f1ccaf09d;hpb=66c64ad00ba348d2e95999e16bc1a06f6f01cbb0;p=sonitus.git diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java index 015dd6f..5011321 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java @@ -17,24 +17,20 @@ package net.pterodactylus.sonitus.data.sink; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Logger; -import net.pterodactylus.sonitus.data.ConnectException; -import net.pterodactylus.sonitus.data.Connection; -import net.pterodactylus.sonitus.data.Sink; -import net.pterodactylus.sonitus.data.Source; - -import com.google.common.base.Preconditions; +import net.pterodactylus.sonitus.data.AbstractFilter; +import net.pterodactylus.sonitus.data.Metadata; /** - * {@link Sink} that writes all received data into a file. + * {@link net.pterodactylus.sonitus.data.Sink} that writes all received data + * into a file. * * @author David ‘Bombe’ Roden */ -public class FileSink implements Sink { +public class FileSink extends AbstractFilter { /** The logger. */ private static final Logger logger = Logger.getLogger(FileSink.class.getName()); @@ -42,6 +38,9 @@ public class FileSink implements Sink { /** The path of the file to write to. */ private final String path; + /** The output stream writing to the file. */ + private FileOutputStream fileOutputStream; + /** * Creates a new file sink that will write to the given path. * @@ -49,36 +48,33 @@ public class FileSink implements Sink { * The path of the file to write to */ public FileSink(String path) { + super(path); this.path = path; } + // + // FILTER METHODS + // + @Override - public void connect(Source source) throws ConnectException { - Preconditions.checkNotNull(source, "source must not be null"); + public void open(Metadata metadata) throws IOException { + fileOutputStream = new FileOutputStream(path); + metadataUpdated(metadata); + } + @Override + public void close() { try { - final FileOutputStream fileOutputStream = new FileOutputStream(path); - new Thread(new Connection(source) { - - @Override - protected int bufferSize() { - return 65536; - } - - @Override - protected void feed(byte[] buffer) throws IOException { - fileOutputStream.write(buffer); - logger.finest(String.format("FileSink: Wrote %d Bytes.", buffer.length)); - } - - @Override - protected void finish() throws IOException { - fileOutputStream.close(); - } - }).start(); - } catch (FileNotFoundException fnfe1) { - throw new ConnectException(fnfe1); + fileOutputStream.close(); + } catch (IOException e) { + /* ignore. */ } } + @Override + public void process(byte[] buffer) throws IOException { + fileOutputStream.write(buffer); + logger.finest(String.format("FileSink: Wrote %d Bytes.", buffer.length)); + } + }