Added helper methods to SerializationUtils.

Also exporting sources and Javadoc on build.
This commit is contained in:
2020-01-06 17:41:23 +01:00
parent 867f989802
commit 407a5bdbd8
5 changed files with 64 additions and 27 deletions

View File

@ -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: <strong>envoy-client</strong><br>
@ -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 <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
@ -63,7 +73,7 @@ public class SerializationUtils {
/**
* 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 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