X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsink%2FFileSink.java;h=be934f3bfea989833c7e04cd2f265675318b517a;hb=cbeadf6d9eea57ab98cacd60e2419dd3c18bef89;hp=61e0a27f5ab85fa38f6a4ab7a50e2e89bf0bd580;hpb=7c0955003c1eb215da5f763e986e99e68206a086;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 61e0a27..be934f3 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,24 @@ package net.pterodactylus.sonitus.data.sink; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Collections; +import java.util.List; import java.util.logging.Logger; -import net.pterodactylus.sonitus.data.ConnectException; -import net.pterodactylus.sonitus.data.Connection; +import net.pterodactylus.sonitus.data.AbstractControlledComponent; +import net.pterodactylus.sonitus.data.Controller; +import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Sink; -import net.pterodactylus.sonitus.data.Source; - -import com.google.common.base.Preconditions; /** - * {@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 AbstractControlledComponent implements Sink { /** The logger. */ private static final Logger logger = Logger.getLogger(FileSink.class.getName()); @@ -42,6 +42,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,41 +52,42 @@ public class FileSink implements Sink { * The path of the file to write to */ public FileSink(String path) { + super(path); this.path = path; } - @Override - public void connect(Source source) throws ConnectException { - Preconditions.checkNotNull(source, "source must not be null"); + // + // CONTROLLED METHODS + // - try { - final FileOutputStream fileOutputStream = new FileOutputStream(path); - new Thread(new Connection(source) { + @Override + public List> controllers() { + return Collections.emptyList(); + } - @Override - protected int bufferSize() { - return 65536; - } + // + // SINK METHODS + // - @Override - protected void feed(byte[] buffer) throws IOException { - fileOutputStream.write(buffer); - logger.finest(String.format("FileSink: Wrote %d Bytes.", buffer.length)); - } + @Override + public void open(Metadata metadata) throws IOException { + fileOutputStream = new FileOutputStream(path); + metadataUpdated(metadata); + } - @Override - protected void finish() throws IOException { - fileOutputStream.close(); - } - }).start(); - } catch (FileNotFoundException fnfe1) { - throw new ConnectException(fnfe1); + @Override + public void close() { + try { + fileOutputStream.close(); + } catch (IOException e) { + /* ignore. */ } } @Override - public void metadataUpdated() { - /* ignore. */ + public void process(byte[] buffer) throws IOException { + fileOutputStream.write(buffer); + logger.finest(String.format("FileSink: Wrote %d Bytes.", buffer.length)); } }