From 407a5bdbd85ae99884501ebcc7455523e4fa5ddf Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 6 Jan 2020 17:41:23 +0100 Subject: [PATCH] Added helper methods to SerializationUtils. Also exporting sources and Javadoc on build. --- .classpath | 5 --- .settings/org.eclipse.core.resources.prefs | 1 - .settings/org.eclipse.wst.common.component | 14 ++++--- pom.xml | 31 ++++++++++++++ .../java/envoy/util/SerializationUtils.java | 40 ++++++++++++------- 5 files changed, 64 insertions(+), 27 deletions(-) diff --git a/.classpath b/.classpath index 88d37ef..907ff21 100644 --- a/.classpath +++ b/.classpath @@ -24,10 +24,5 @@ - - - - - diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 04cfa2c..8bc0e1c 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,6 +1,5 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 -encoding//src/main/resources=UTF-8 encoding//src/test/java=UTF-8 encoding//src/test/resources=UTF-8 encoding/=UTF-8 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index a6896ac..44f2db9 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,15 +1,17 @@ - + + - + + - - - + + - + + diff --git a/pom.xml b/pom.xml index 6e20167..eee2dcf 100644 --- a/pom.xml +++ b/pom.xml @@ -18,5 +18,36 @@ envoy-common + + + org.apache.maven.plugins + maven-source-plugin + 3.2.1 + + + attach-sources + + jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.1.1 + + + attach-javadocs + + jar + + + + + none + + + \ No newline at end of file diff --git a/src/main/java/envoy/util/SerializationUtils.java b/src/main/java/envoy/util/SerializationUtils.java index b26aa49..3007bfe 100644 --- a/src/main/java/envoy/util/SerializationUtils.java +++ b/src/main/java/envoy/util/SerializationUtils.java @@ -1,16 +1,6 @@ package envoy.util; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -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; -import java.io.Serializable; +import java.io.*; /** * Project: envoy-client
@@ -24,7 +14,27 @@ 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 }; } + /** + * Converts an integer into a byte array. + * + * @param n the integer to convert + * @return a byte array of length 4 + * @since Envoy Common v0.2-alpha + */ + public static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; } + + /** + * Converts four bytes in byte array to an integer + * + * @param bytes the bytes to convert from + * @param offset the offset at which four bytes are read + * @return the converted integer + * @since Envoy Common v0.2-alpha + */ + public static int bytesToInt(byte[] bytes, int offset) { + return ((bytes[offset] & 0xFF) << 24) | ((bytes[offset + 1] & 0xFF) << 16) | ((bytes[offset + 2] & 0xFF) << 8) + | ((bytes[offset + 3] & 0xFF) << 0); + } /** * Deserializes an arbitrary {@link Serializable} object from a file. @@ -46,7 +56,7 @@ public class SerializationUtils { /** * Deserializes an arbitrary {@link Serializable} object from a byte array. - * + * * @param 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 @@ -63,7 +73,7 @@ public class SerializationUtils { /** * Deserializes an arbitrary {@link Serializable} object from a stream. - * + * * @param the type of the serialized object * @param in the {@link InputStream} of a serialized Object * @param serializedClass the object type to convert the deserialized object @@ -103,7 +113,7 @@ 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