Make SerializationUtils#write variadic

This commit is contained in:
Kai S. K. Engelbart 2020-06-13 18:32:24 +02:00
parent b3c74562d7
commit f7de07aca2

View File

@ -3,6 +3,8 @@ package envoy.util;
import java.io.*; import java.io.*;
/** /**
* Defines utility methods related to serialization.
* <p>
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
* File: <strong>SerializationUtils.java</strong><br> * File: <strong>SerializationUtils.java</strong><br>
* Created: <strong>23.12.2019</strong><br> * Created: <strong>23.12.2019</strong><br>
@ -92,22 +94,23 @@ public class SerializationUtils {
} }
/** /**
* Serializes an arbitrary object to a file. * Serializes arbitrary objects to a file.
* *
* @param file the file to serialize to * @param file the file to serialize to
* @param obj the object to serialize * @param objs the objects to serialize
* @throws IOException if an error occurred during serialization * @throws IOException if an error occurred during serialization
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public static void write(File file, Object obj) throws IOException { public static void write(File file, Object... objs) throws IOException {
if (file == null) throw new NullPointerException("File is null"); if (file == null) throw new NullPointerException("File is null");
if (obj == null) throw new NullPointerException("Object to serialize is null"); if (objs == null) throw new NullPointerException("Null array passed to serialize");
if (!file.exists()) { if (!file.exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
file.createNewFile(); file.createNewFile();
} }
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) { try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
out.writeObject(obj); for (var obj : objs)
out.writeObject(obj);
} }
} }