X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Fsource%2FFileSource.java;h=01216cd0c910d12416679451e362786903f2d7a9;hb=3a12209e82233cd79677a0d847321f41b41aa9a5;hp=08139d588ef3b611f79aea259610e143d8f69bf8;hpb=9a5bbdb694cda8f4bc0794aea136b993399961dd;p=sonitus.git
diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java
index 08139d5..01216cd 100644
--- a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java
+++ b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java
@@ -27,27 +27,26 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import net.pterodactylus.sonitus.data.AbstractFilter;
import net.pterodactylus.sonitus.data.Controller;
+import net.pterodactylus.sonitus.data.DataPacket;
+import net.pterodactylus.sonitus.data.Filter;
import net.pterodactylus.sonitus.data.Metadata;
-import net.pterodactylus.sonitus.data.Source;
import net.pterodactylus.sonitus.io.IdentifyingInputStream;
import com.google.common.base.Optional;
/**
- * A {@link net.pterodactylus.sonitus.data.Source} that is read from the local
- * file system.
+ * A {@link Filter} that reads a file from the local file system and does not
+ * expect any input.
*
* @author David âBombeâ Roden
*/
-public class FileSource implements Source {
+public class FileSource extends AbstractFilter {
/** The path of the file. */
private final String path;
- /** The identified metadata of the file. */
- private final Metadata metadata;
-
/** The input stream. */
private InputStream fileInputStream;
@@ -60,45 +59,37 @@ public class FileSource implements Source {
* if the file can not be found, or an I/O error occurs
*/
public FileSource(String path) throws IOException {
+ super(path);
this.path = checkNotNull(path, "path must not be null");
fileInputStream = new FileInputStream(path);
/* identify file type. */
Optional identifyingInputStream = IdentifyingInputStream.create(new FileInputStream(path));
if (identifyingInputStream.isPresent()) {
- metadata = identifyingInputStream.get().metadata();
+ metadataUpdated(identifyingInputStream.get().metadata());
} else {
/* fallback. */
- metadata = new Metadata().name(path);
+ metadataUpdated(new Metadata().name(path));
}
}
//
- // CONTROLLED METHODS
+ // FILTER METHODS
//
@Override
- public List controllers() {
+ public List> controllers() {
return Collections.emptyList();
}
- //
- // SOURCE METHODS
- //
-
@Override
- public byte[] get(int bufferSize) throws IOException {
+ public DataPacket get(int bufferSize) throws IOException {
byte[] buffer = new byte[bufferSize];
int read = fileInputStream.read(buffer);
if (read == -1) {
throw new EOFException();
}
- return Arrays.copyOf(buffer, read);
- }
-
- @Override
- public Metadata metadata() {
- return metadata;
+ return new DataPacket(metadata(), Arrays.copyOf(buffer, read));
}
//
@@ -107,7 +98,7 @@ public class FileSource implements Source {
@Override
public String toString() {
- return String.format("%s (%s)", path, metadata);
+ return String.format("%s (%s)", path, metadata());
}
}