Merge pull request #6 from informatik-ag-ngl/f/database_adaption
Improved Message.java and User.java to remain compatible with database
This commit is contained in:
		
							
								
								
									
										47
									
								
								src/main/java/envoy/data/Contacts.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/main/java/envoy/data/Contacts.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					package envoy.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.Serializable;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-common</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>Contacts.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>02.01.2020</strong><br>
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 | 
					 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class Contacts implements Serializable {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private final long			userId;
 | 
				
			||||||
 | 
						private final List<User>	contacts;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private static final long serialVersionUID = 136970804968152871L;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Creates an instance of {@link Contacts}.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param userId   the ID of the user this contacts belong to
 | 
				
			||||||
 | 
						 * @param contacts the contact list
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public Contacts(long userId, List<User> contacts) {
 | 
				
			||||||
 | 
							this.userId		= userId;
 | 
				
			||||||
 | 
							this.contacts	= contacts;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public String toString() { return String.format("Contacts[%s]", contacts); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the ID of the user this contacts belong to
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public long getUserId() { return userId; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return a list of users messages can be sent to
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public List<User> getContacts() { return contacts; }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -49,11 +49,11 @@ public class Message implements Serializable {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private final long					id, senderId, recipientId;
 | 
						private final long					id, senderId, recipientId;
 | 
				
			||||||
	private final transient User		sender, recipient;
 | 
						private final Date					creationDate;
 | 
				
			||||||
	private final Date					date;
 | 
					 | 
				
			||||||
	private final String				text;
 | 
						private final String				text;
 | 
				
			||||||
	private final MessageAttachment<?>	attachment;
 | 
						private final MessageAttachment<?>	attachment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private Date			receivedDate, readDate;
 | 
				
			||||||
	private MessageStatus	status;
 | 
						private MessageStatus	status;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static final long serialVersionUID = -4393477412979594435L;
 | 
						private static final long serialVersionUID = -4393477412979594435L;
 | 
				
			||||||
@@ -65,25 +65,23 @@ public class Message implements Serializable {
 | 
				
			|||||||
	 * properties.
 | 
						 * properties.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param id          unique ID
 | 
						 * @param id          unique ID
 | 
				
			||||||
	 * @param sender     the user who sends the message
 | 
						 * @param senderId    the ID of the user who sends the message
 | 
				
			||||||
	 * @param recipient  the user who receives the message
 | 
						 * @param recipientId the ID of the user who receives the message
 | 
				
			||||||
	 * @param date        the creation date of the message
 | 
						 * @param date        the creation date of the message
 | 
				
			||||||
	 * @param text        the text content of the message
 | 
						 * @param text        the text content of the message
 | 
				
			||||||
	 * @param attachment  the attachment of the message, if present
 | 
						 * @param attachment  the attachment of the message, if present
 | 
				
			||||||
	 * @param status      the current {@link MessageStatus} of the message
 | 
						 * @param status      the current {@link MessageStatus} of the message
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	Message(long id, User sender, User recipient, Date date, String text, MessageAttachment<?> attachment, MessageStatus status) {
 | 
						Message(long id, long senderId, long recipientId, Date date, String text, MessageAttachment<?> attachment, MessageStatus status) {
 | 
				
			||||||
		this.id				= id;
 | 
							this.id				= id;
 | 
				
			||||||
		this.sender		= sender;
 | 
							this.senderId		= senderId;
 | 
				
			||||||
		this.recipient	= recipient;
 | 
							this.recipientId	= recipientId;
 | 
				
			||||||
		this.date		= date;
 | 
							this.creationDate	= date;
 | 
				
			||||||
		this.text			= text;
 | 
							this.text			= text;
 | 
				
			||||||
		this.attachment		= attachment;
 | 
							this.attachment		= attachment;
 | 
				
			||||||
		this.status			= status;
 | 
							this.status			= status;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		senderId	= sender.getId();
 | 
					 | 
				
			||||||
		recipientId	= recipient.getId();
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -106,13 +104,14 @@ public class Message implements Serializable {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public String toString() {
 | 
						public String toString() {
 | 
				
			||||||
		return String.format("TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s]",
 | 
							return String.format("TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,hasAttachment=%b]",
 | 
				
			||||||
				id,
 | 
									id,
 | 
				
			||||||
				sender,
 | 
									senderId,
 | 
				
			||||||
				recipient,
 | 
									recipientId,
 | 
				
			||||||
				new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date),
 | 
									new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(creationDate),
 | 
				
			||||||
				status,
 | 
									status,
 | 
				
			||||||
				text);
 | 
									text,
 | 
				
			||||||
 | 
									attachment != null);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -121,24 +120,12 @@ public class Message implements Serializable {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public long getId() { return id; }
 | 
						public long getId() { return id; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @return the sender of this message
 | 
					 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public User getSender() { return sender; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the sender ID of this message
 | 
						 * @return the sender ID of this message
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public long getSenderId() { return senderId; }
 | 
						public long getSenderId() { return senderId; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @return the recipient of this message
 | 
					 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public User getRecipient() { return recipient; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the recipient ID of this message
 | 
						 * @return the recipient ID of this message
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
@@ -149,7 +136,33 @@ public class Message implements Serializable {
 | 
				
			|||||||
	 * @return the date at which this message was created
 | 
						 * @return the date at which this message was created
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public Date getDate() { return date; }
 | 
						public Date getDate() { return creationDate; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the date at which the message has been received by the sender
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public Date getReceivedDate() { return receivedDate; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param receivedDate the date at which the message has been received by the
 | 
				
			||||||
 | 
						 *                     sender
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void setReceivedDate(Date receivedDate) { this.receivedDate = receivedDate; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the date at which the message has been read by the sender
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public Date getReadDate() { return readDate; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @param readDate at which the message has been read by the sender
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void setReadDate(Date readDate) { this.readDate = readDate; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the text content of this message
 | 
						 * @return the text content of this message
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@ import envoy.data.Message.MessageStatus;
 | 
				
			|||||||
public class MessageBuilder {
 | 
					public class MessageBuilder {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Mandatory properties without default values
 | 
						// Mandatory properties without default values
 | 
				
			||||||
	private final User sender, recipient;
 | 
						private final long senderId, recipientId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Properties with default values
 | 
						// Properties with default values
 | 
				
			||||||
	private long					id;
 | 
						private long					id;
 | 
				
			||||||
@@ -30,15 +30,13 @@ public class MessageBuilder {
 | 
				
			|||||||
	 * Creates an instance of {@link MessageBuilder} with all mandatory values
 | 
						 * Creates an instance of {@link MessageBuilder} with all mandatory values
 | 
				
			||||||
	 * without defaults for the {@link Message} class.
 | 
						 * without defaults for the {@link Message} class.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param sender    the user who sends the {@link Message}
 | 
						 * @param senderId    the ID of the user who sends the {@link Message}
 | 
				
			||||||
	 * @param recipient the user who received the {@link Message}
 | 
						 * @param recipientId the ID of the user who received the {@link Message}
 | 
				
			||||||
	 * @since Envoy Common v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public MessageBuilder(User sender, User recipient) {
 | 
						public MessageBuilder(long senderId, long recipientId) {
 | 
				
			||||||
		if (sender == null) throw new NullPointerException("Message sender is null");
 | 
							this.senderId		= senderId;
 | 
				
			||||||
		if (recipient == null) throw new NullPointerException("Message recipient is null");
 | 
							this.recipientId	= recipientId;
 | 
				
			||||||
		this.sender		= sender;
 | 
					 | 
				
			||||||
		this.recipient	= recipient;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -69,7 +67,7 @@ public class MessageBuilder {
 | 
				
			|||||||
		if (text == null) text = "";
 | 
							if (text == null) text = "";
 | 
				
			||||||
		if (status == null) status = MessageStatus.WAITING;
 | 
							if (status == null) status = MessageStatus.WAITING;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return new Message(id, sender, recipient, date, text, attachment, status);
 | 
							return new Message(id, senderId, recipientId, date, text, attachment, status);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,6 @@
 | 
				
			|||||||
package envoy.data;
 | 
					package envoy.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.Serializable;
 | 
					import java.io.Serializable;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Represents a unique user with a unique, numeric ID, a name and a current
 | 
					 * Represents a unique user with a unique, numeric ID, a name and a current
 | 
				
			||||||
@@ -48,7 +46,6 @@ public class User implements Serializable {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	private final long		id;
 | 
						private final long		id;
 | 
				
			||||||
	private final String	name;
 | 
						private final String	name;
 | 
				
			||||||
	private final List<User>	contacts	= new ArrayList<>();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private UserStatus status;
 | 
						private UserStatus status;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -60,12 +57,12 @@ public class User implements Serializable {
 | 
				
			|||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param id   unique ID
 | 
						 * @param id   unique ID
 | 
				
			||||||
	 * @param name user name
 | 
						 * @param name user name
 | 
				
			||||||
	 * @since Envoy Client v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public User(long id, String name) {
 | 
						public User(long id, String name) {
 | 
				
			||||||
		this.id		= id;
 | 
							this.id		= id;
 | 
				
			||||||
		this.name	= name;
 | 
							this.name	= name;
 | 
				
			||||||
		status		= UserStatus.OFFLINE;
 | 
							status		= UserStatus.ONLINE;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
@@ -73,33 +70,25 @@ public class User implements Serializable {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the ID of this {@link User}
 | 
						 * @return the ID of this {@link User}
 | 
				
			||||||
	 * @since Envoy Client v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public long getId() { return id; }
 | 
						public long getId() { return id; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the name of this {@link User}
 | 
						 * @return the name of this {@link User}
 | 
				
			||||||
	 * @since Envoy Client v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public String getName() { return name; }
 | 
						public String getName() { return name; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @return a list of all users this user can send messages to
 | 
					 | 
				
			||||||
	 * @since Envoy Client v0.2-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public List<User> getContacts() { return contacts; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the current status of this user
 | 
						 * @return the current status of this user
 | 
				
			||||||
	 * @since Envoy Client v0.2-alpha
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public UserStatus getStatus() { return status; }
 | 
						public UserStatus getStatus() { return status; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Sets the current status of this user
 | 
						 * @param status the next status of this user
 | 
				
			||||||
	 *
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 * @param status the status to set
 | 
					 | 
				
			||||||
	 * @since Envoy Client v0.2-alpha
 | 
					 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void setStatus(UserStatus status) { this.status = status; }
 | 
						public void setStatus(UserStatus status) { this.status = status; }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,5 +1,6 @@
 | 
				
			|||||||
package envoy.util;
 | 
					package envoy.util;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.ByteArrayInputStream;
 | 
				
			||||||
import java.io.ByteArrayOutputStream;
 | 
					import java.io.ByteArrayOutputStream;
 | 
				
			||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
import java.io.FileInputStream;
 | 
					import java.io.FileInputStream;
 | 
				
			||||||
@@ -21,12 +22,14 @@ import java.io.Serializable;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
public class SerializationUtils {
 | 
					public class SerializationUtils {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private SerializationUtils() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; }
 | 
						private static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Deserializes an arbitrary {@link Serializable} object from a file.
 | 
						 * Deserializes an arbitrary {@link Serializable} object from a file.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param <T>             the type of the object to deserialize
 | 
						 * @param <T>             the type of the serialized object
 | 
				
			||||||
	 * @param file            the file to deserialize from
 | 
						 * @param file            the file to deserialize from
 | 
				
			||||||
	 * @param serializedClass the class of the object to deserialize
 | 
						 * @param serializedClass the class of the object to deserialize
 | 
				
			||||||
	 * @return the deserialized object
 | 
						 * @return the deserialized object
 | 
				
			||||||
@@ -42,7 +45,26 @@ public class SerializationUtils {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @param <T>             the deserialized object
 | 
						 * Deserializes an arbitrary {@link Serializable} object from a byte array.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param <T>             the type of the serialized object
 | 
				
			||||||
 | 
						 * @param bytes           the array in which the serialized object is stored
 | 
				
			||||||
 | 
						 * @param serializedClass the class of the serialized object
 | 
				
			||||||
 | 
						 * @return the deserialized object
 | 
				
			||||||
 | 
						 * @throws IOException            if something failed while deserializing the
 | 
				
			||||||
 | 
						 *                                object
 | 
				
			||||||
 | 
						 * @throws ClassNotFoundException if the deserialized object can not be linked
 | 
				
			||||||
 | 
						 *                                to a class
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static <T extends Serializable> T read(byte[] bytes, Class<T> serializedClass) throws IOException, ClassNotFoundException {
 | 
				
			||||||
 | 
							return read(new ByteArrayInputStream(bytes), serializedClass);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Deserializes an arbitrary {@link Serializable} object from a stream.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param <T>             the type of the serialized object
 | 
				
			||||||
	 * @param in              the {@link InputStream} of a serialized Object
 | 
						 * @param in              the {@link InputStream} of a serialized Object
 | 
				
			||||||
	 * @param serializedClass the object type to convert the deserialized object
 | 
						 * @param serializedClass the object type to convert the deserialized object
 | 
				
			||||||
	 *                        into
 | 
						 *                        into
 | 
				
			||||||
@@ -79,6 +101,22 @@ public class SerializationUtils {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Serializes an arbitrary object to a byte array.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param obj the object to serialize
 | 
				
			||||||
 | 
						 * @return a byte array containing the serialized object
 | 
				
			||||||
 | 
						 * @throws IOException if the serialization failed
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static byte[] writeToByteArray(Object obj) throws IOException {
 | 
				
			||||||
 | 
							ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
				
			||||||
 | 
							try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
 | 
				
			||||||
 | 
								oout.writeObject(obj);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return baos.toByteArray();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Serializes an object and writes it into an output stream preceded by 4 bytes
 | 
						 * Serializes an object and writes it into an output stream preceded by 4 bytes
 | 
				
			||||||
	 * containing the number of serialized bytes.
 | 
						 * containing the number of serialized bytes.
 | 
				
			||||||
@@ -86,14 +124,11 @@ public class SerializationUtils {
 | 
				
			|||||||
	 * @param obj the object to serialize
 | 
						 * @param obj the object to serialize
 | 
				
			||||||
	 * @param out the output stream to serialize to
 | 
						 * @param out the output stream to serialize to
 | 
				
			||||||
	 * @throws IOException if an error occurred during serialization
 | 
						 * @throws IOException if an error occurred during serialization
 | 
				
			||||||
 | 
						 * @since Envoy Common v0.2-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public static void writeBytesWithLength(Object obj, OutputStream out) throws IOException {
 | 
						public static void writeBytesWithLength(Object obj, OutputStream out) throws IOException {
 | 
				
			||||||
		// Serialize object to byte array
 | 
							// Serialize object to byte array
 | 
				
			||||||
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
							byte[] objBytes = writeToByteArray(obj);
 | 
				
			||||||
		try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
 | 
					 | 
				
			||||||
			oout.writeObject(obj);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		byte[] objBytes = baos.toByteArray();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Get length of byte array in bytes
 | 
							// Get length of byte array in bytes
 | 
				
			||||||
		byte[] objLen = intToBytes(objBytes.length);
 | 
							byte[] objLen = intToBytes(objBytes.length);
 | 
				
			||||||
@@ -102,6 +137,4 @@ public class SerializationUtils {
 | 
				
			|||||||
		out.write(objLen);
 | 
							out.write(objLen);
 | 
				
			||||||
		out.write(objBytes);
 | 
							out.write(objBytes);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	private SerializationUtils() {}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user