♻️ Slightly refactor the file entries
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 13 Feb 2023 07:53:30 +0000 (08:53 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 13 Feb 2023 07:53:30 +0000 (08:53 +0100)
More to come!

src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java
src/main/java/net/pterodactylus/fcp/FileEntry.java

index c2cc547..4bb503d 100644 (file)
@@ -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<String, String> fields = fileEntry.getFields();
-               for (Entry<String, String> 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());
-               }
        }
 
        /**
index 8659686..43b12aa 100644 (file)
@@ -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<String, String> getFields();
+       protected abstract Map<String, String> 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<String, String> getFields() {
+               protected Map<String, String> getFields() {
                        Map<String, String> fields = new HashMap<String, String>();
                        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<String, String> getFields() {
+               protected Map<String, String> getFields() {
                        Map<String, String> fields = new HashMap<String, String>();
                        fields.put("Name", name);
                        fields.put("UploadFrom", String.valueOf(uploadFrom));
@@ -280,7 +294,7 @@ public abstract class FileEntry {
                 * {@inheritDoc}
                 */
                @Override
-               Map<String, String> getFields() {
+               protected Map<String, String> getFields() {
                        Map<String, String> fields = new HashMap<String, String>();
                        fields.put("Name", name);
                        fields.put("UploadFrom", String.valueOf(uploadFrom));