X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsink%2FFileSink.java;h=92dd59aadb05b157e46aea236cb03f00fb0a7072;hb=f554e51504f7e39e658a84d79c041e0b0d8a6393;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..92dd59a 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java @@ -17,20 +17,19 @@ 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.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 */ @@ -42,6 +41,11 @@ public class FileSink implements Sink { /** The path of the file to write to. */ private final String path; + private FileOutputStream fileOutputStream; + + /** The current metadata. */ + private Metadata metadata; + /** * Creates a new file sink that will write to the given path. * @@ -52,33 +56,53 @@ public class FileSink implements Sink { this.path = path; } + // + // CONTROLLED METHODS + // + + @Override + public String name() { + return path; + } + + @Override + public Metadata metadata() { + return metadata; + } + + @Override + public List> controllers() { + return Collections.emptyList(); + } + + // + // SINK 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 metadataUpdated(Metadata metadata) { + this.metadata = metadata; + } + + @Override + public void process(byte[] buffer) throws IOException { + fileOutputStream.write(buffer); + logger.finest(String.format("FileSink: Wrote %d Bytes.", buffer.length)); + } + }