com.virgilsecurity.sdk.utils.ConvertionUtils Maven / Gradle / Ivy
/*
* Copyright (c) 2015-2019, Virgil Security, Inc.
*
* Lead Maintainer: Virgil Security Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* (1) Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* (3) Neither the name of virgil nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.virgilsecurity.sdk.utils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.virgilsecurity.crypto.foundation.Base64;
import com.virgilsecurity.sdk.common.StringEncoding;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
/**
* Utilities class for data conversion.
*/
public class ConvertionUtils {
private static class ByteArrayToBase64TypeAdapter
implements JsonSerializer, JsonDeserializer {
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return base64ToBytes(json.getAsString());
}
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(toBase64String(src));
}
}
private static class ClassTypeAdapter extends TypeAdapter> {
@Override
public Class> read(JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
return null;
}
Class> clazz = null;
try {
clazz = Class.forName(jsonReader.nextString());
} catch (ClassNotFoundException exception) {
throw new IOException(exception);
}
return clazz;
}
@Override
public void write(JsonWriter jsonWriter, Class> clazz) throws IOException {
if (clazz == null) {
jsonWriter.nullValue();
return;
}
jsonWriter.value(clazz.getName());
}
}
private static class ClassTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")
@Override
public TypeAdapter create(Gson gson, TypeToken typeToken) {
if (!Class.class.isAssignableFrom(typeToken.getRawType())) {
return null;
}
return (TypeAdapter) new ClassTypeAdapter();
}
}
private static Gson gson = null;
private static final Charset UTF8_CHARSET = StandardCharsets.UTF_8;
private static final char[] hexCode = "0123456789ABCDEF".toCharArray();
/**
* Decode Base64 string to byte array.
*
* @param value The string to be converted
* @return the byte array
*/
public static byte[] base64ToBytes(String value) {
return Base64.decode(value.getBytes());
}
/**
* Decode Base64 byte array to string.
*
* @param bytes the base64-encoded byte array
* @return the decoded string
*/
public static String base64ToString(byte[] bytes) {
return toString(base64ToBytes(toString(bytes)));
}
/**
* Decode Base64 string to string.
*
* @param value the base64-encoded string to be converted.
* @return the decoded string.
*/
public static String base64ToString(String value) {
return toString(base64ToBytes(value));
}
/**
* Take an accurate snapshot of the object, and convert it into the binary data.
*
* @param snapshotModel The snapshot model.
* @return The taken snapshot.
*/
public static byte[] captureSnapshot(Object snapshotModel) {
String snapshotModelJson = serializeToJson(snapshotModel);
return ConvertionUtils.toBytes(snapshotModelJson);
}
/**
* Concatenate two byte arrays.
*
* @param first the first array.
* @param second the second array.
* @return a byte array.
*/
public static byte[] concatenate(byte[] first, byte[] second) {
byte[] result = new byte[first.length + second.length];
System.arraycopy(first, 0, result, 0, first.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
/**
* Deserialize object of objectType type from JSON String.
*
* @param serializedObject the string presentation of object serialized to JSON
* @param objectType the type of object
* @return the {@code objectType} object deserialized from {@code serializedObject}
*/
public static T deserializeFromJson(String serializedObject, Class objectType) {
return getGson().fromJson(serializedObject, objectType);
}
/**
* Deserialize from JSON String to Map.
*
* @param serializedObject object to be deserialized
* @return Map object deserialized from JSON String
*/
public static Map deserializeMapFromJson(String serializedObject) {
Type mapType = new TypeToken © 2015 - 2025 Weber Informatics LLC | Privacy Policy