Fixed bug in SerializationUtils, added toString to LoginCredentials
This commit is contained in:
		@@ -3,6 +3,7 @@ package envoy.data;
 | 
				
			|||||||
import java.io.Serializable;
 | 
					import java.io.Serializable;
 | 
				
			||||||
import java.security.MessageDigest;
 | 
					import java.security.MessageDigest;
 | 
				
			||||||
import java.security.NoSuchAlgorithmException;
 | 
					import java.security.NoSuchAlgorithmException;
 | 
				
			||||||
 | 
					import java.util.Formatter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Project: <strong>envoy-common</strong><br>
 | 
					 * Project: <strong>envoy-common</strong><br>
 | 
				
			||||||
@@ -31,6 +32,18 @@ public class LoginCredentials implements Serializable {
 | 
				
			|||||||
		passwordHash	= getSha256(toByteArray(password));
 | 
							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) {
 | 
						private byte[] toByteArray(char[] chars) {
 | 
				
			||||||
		byte[] bytes = new byte[chars.length * 2];
 | 
							byte[] bytes = new byte[chars.length * 2];
 | 
				
			||||||
		for (int i = 0; i < chars.length; ++i) {
 | 
							for (int i = 0; i < chars.length; ++i) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,7 @@ import java.io.File;
 | 
				
			|||||||
import java.io.FileInputStream;
 | 
					import java.io.FileInputStream;
 | 
				
			||||||
import java.io.FileOutputStream;
 | 
					import java.io.FileOutputStream;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.io.InputStream;
 | 
				
			||||||
import java.io.ObjectInputStream;
 | 
					import java.io.ObjectInputStream;
 | 
				
			||||||
import java.io.ObjectOutputStream;
 | 
					import java.io.ObjectOutputStream;
 | 
				
			||||||
import java.io.OutputStream;
 | 
					import java.io.OutputStream;
 | 
				
			||||||
@@ -34,12 +35,14 @@ public class SerializationUtils {
 | 
				
			|||||||
	 * @throws EnvoyException if an error occurred during deserialization
 | 
						 * @throws EnvoyException if an error occurred during deserialization
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @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");
 | 
							if (file == null) throw new NullPointerException("File is null");
 | 
				
			||||||
		try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
 | 
							return read(new FileInputStream(file), serializedClass);
 | 
				
			||||||
			return serializedClass.cast(in.readObject());
 | 
						}
 | 
				
			||||||
		} catch (ClassNotFoundException | IOException e) {
 | 
					
 | 
				
			||||||
			throw new EnvoyException("Could not load serialized object", e);
 | 
						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();
 | 
							byte[] objBytes = baos.toByteArray();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Get length of byte array in bytes
 | 
							// Get length of byte array in bytes
 | 
				
			||||||
		byte[] objLen = getBytes(objBytes.length);
 | 
							byte[] objLen = intToBytes(objBytes.length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Write length and byte array
 | 
							// Write length and byte array
 | 
				
			||||||
		out.write(objLen);
 | 
							out.write(objLen);
 | 
				
			||||||
		out.write(objBytes);
 | 
							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