Fixed bug in SerializationUtils, added toString to LoginCredentials
This commit is contained in:
parent
708ffc20da
commit
72b6d67602
@ -3,6 +3,7 @@ package envoy.data;
|
||||
import java.io.Serializable;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Formatter;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
@ -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) {
|
||||
|
@ -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 extends Serializable> T read(File file, Class<T> serializedClass) throws EnvoyException {
|
||||
public static <T extends Serializable> T read(File file, Class<T> 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 extends Serializable> T read(InputStream in, Class<T> 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 }; }
|
||||
}
|
Reference in New Issue
Block a user