From 66c64ad00ba348d2e95999e16bc1a06f6f01cbb0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 16 Mar 2013 01:29:27 +0100 Subject: [PATCH] Add file sink. --- .../pterodactylus/sonitus/data/sink/FileSink.java | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java new file mode 100644 index 0000000..015dd6f --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/FileSink.java @@ -0,0 +1,84 @@ +/* + * Sonitus - FileSink.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +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; + +/** + * {@link Sink} that writes all received data into a file. + * + * @author David ‘Bombe’ Roden + */ +public class FileSink implements Sink { + + /** The logger. */ + private static final Logger logger = Logger.getLogger(FileSink.class.getName()); + + /** The path of the file to write to. */ + private final String path; + + /** + * Creates a new file sink that will write to the given path. + * + * @param path + * The path of the file to write to + */ + public FileSink(String path) { + this.path = path; + } + + @Override + public void connect(Source source) throws ConnectException { + Preconditions.checkNotNull(source, "source must not be null"); + + 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); + } + } + +} -- 2.7.4