From 72b6d67602c11deb898188dd7ad94af4e47c2dbf Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 29 Dec 2019 12:53:16 +0200 Subject: [PATCH] Fixed bug in SerializationUtils, added toString to LoginCredentials --- src/main/java/envoy/data/LoginCredentials.java | 13 +++++++++++++ .../java/envoy/util/SerializationUtils.java | 17 ++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/envoy/data/LoginCredentials.java b/src/main/java/envoy/data/LoginCredentials.java index c7899d6..86b166c 100644 --- a/src/main/java/envoy/data/LoginCredentials.java +++ b/src/main/java/envoy/data/LoginCredentials.java @@ -3,6 +3,7 @@ package envoy.data; import java.io.Serializable; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Formatter; /** * Project: envoy-common
@@ -31,6 +32,18 @@ public class LoginCredentials implements Serializable { passwordHash = getSha256(toByteArray(password)); } + @Override + public String toString() { + Formatter form = new Formatter(); + form.format("LoginCredentials[name=%s,passwordHash=", name); + for (int i = 0; i < passwordHash.length; i++) + form.format("%02x", passwordHash[i]); + form.format("]"); + String str = form.toString(); + form.close(); + return str; + } + private byte[] toByteArray(char[] chars) { byte[] bytes = new byte[chars.length * 2]; for (int i = 0; i < chars.length; ++i) { diff --git a/src/main/java/envoy/util/SerializationUtils.java b/src/main/java/envoy/util/SerializationUtils.java index 3813a00..dfd166b 100644 --- a/src/main/java/envoy/util/SerializationUtils.java +++ b/src/main/java/envoy/util/SerializationUtils.java @@ -5,6 +5,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; @@ -34,12 +35,14 @@ public class SerializationUtils { * @throws EnvoyException if an error occurred during deserialization * @since Envoy Common v0.2-alpha */ - public static T read(File file, Class serializedClass) throws EnvoyException { + public static T read(File file, Class serializedClass) throws IOException, ClassNotFoundException { if (file == null) throw new NullPointerException("File is null"); - try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) { - return serializedClass.cast(in.readObject()); - } catch (ClassNotFoundException | IOException e) { - throw new EnvoyException("Could not load serialized object", e); + return read(new FileInputStream(file), serializedClass); + } + + public static T read(InputStream in, Class serializedClass) throws IOException, ClassNotFoundException { + try (ObjectInputStream oin = new ObjectInputStream(in)) { + return serializedClass.cast(oin.readObject()); } } @@ -80,12 +83,12 @@ public class SerializationUtils { byte[] objBytes = baos.toByteArray(); // Get length of byte array in bytes - byte[] objLen = getBytes(objBytes.length); + byte[] objLen = intToBytes(objBytes.length); // Write length and byte array out.write(objLen); out.write(objBytes); } - private static byte[] getBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8) }; } + private static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; } } \ No newline at end of file