From: David ‘Bombe’ Roden Date: Mon, 13 Feb 2023 07:53:30 +0000 (+0100) Subject: ♻️ Slightly refactor the file entries X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3685e41018c2c20bdc285d492f674f1012613814;p=jFCPlib.git ♻️ Slightly refactor the file entries More to come! --- diff --git a/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java b/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java index c2cc547..4bb503d 100644 --- a/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java +++ b/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java @@ -24,8 +24,6 @@ import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import net.pterodactylus.fcp.FileEntry.DirectFileEntry; @@ -214,14 +212,9 @@ public class ClientPutComplexDir extends FcpMessage { * The file entry to add */ public void addFileEntry(FileEntry fileEntry) { - Map fields = fileEntry.getFields(); - for (Entry fieldEntry : fields.entrySet()) { - setField("Files." + fileIndex + "." + fieldEntry.getKey(), fieldEntry.getValue()); - } + fileEntry.getFields().forEach((key, value) -> setField("Files." + fileIndex + "." + key, value)); + directFileInputStreams.add(fileEntry.getInputStream()); fileIndex++; - if (fileEntry instanceof FileEntry.DirectFileEntry) { - directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream()); - } } /** diff --git a/src/main/java/net/pterodactylus/fcp/FileEntry.java b/src/main/java/net/pterodactylus/fcp/FileEntry.java index 8659686..43b12aa 100644 --- a/src/main/java/net/pterodactylus/fcp/FileEntry.java +++ b/src/main/java/net/pterodactylus/fcp/FileEntry.java @@ -17,6 +17,7 @@ package net.pterodactylus.fcp; +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Map; @@ -104,7 +105,19 @@ public abstract class FileEntry { * * @return The fields for this file entry */ - abstract Map getFields(); + protected abstract Map getFields(); + + /** + * Returns an {@link InputStream} delivering the content of this file. If + * this file entry is not of type {@link UploadFrom#direct}, the input + * stream returned by this method is empty. + * + * @return An {@link InputStream} delivering the content of a + * {@link UploadFrom#direct} file entry, or an empty input stream + */ + public InputStream getInputStream() { + return new ByteArrayInputStream(new byte[0]); + } /** * A file entry for a file that should be transmitted in the payload of the @@ -161,7 +174,7 @@ public abstract class FileEntry { * {@inheritDoc} */ @Override - Map getFields() { + protected Map getFields() { Map fields = new HashMap(); fields.put("Name", name); fields.put("UploadFrom", String.valueOf(uploadFrom)); @@ -177,7 +190,8 @@ public abstract class FileEntry { * * @return The input stream of the file */ - InputStream getInputStream() { + @Override + public InputStream getInputStream() { return inputStream; } @@ -237,7 +251,7 @@ public abstract class FileEntry { * {@inheritDoc} */ @Override - Map getFields() { + protected Map getFields() { Map fields = new HashMap(); fields.put("Name", name); fields.put("UploadFrom", String.valueOf(uploadFrom)); @@ -280,7 +294,7 @@ public abstract class FileEntry { * {@inheritDoc} */ @Override - Map getFields() { + protected Map getFields() { Map fields = new HashMap(); fields.put("Name", name); fields.put("UploadFrom", String.valueOf(uploadFrom));