All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.es.plus.adapter.tools.SerializationUtils Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
/*
 *  from organization baomidou
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at *

* https://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.es.plus.adapter.tools; import java.io.*; public class SerializationUtils { @SuppressWarnings("unchecked") public static T clone(final T object) { if (object == null) { return null; } final byte[] objectData = serialize(object); return (T) deserialize(objectData); } public static byte[] serialize(Object object) { if (object == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(object); oos.flush(); } catch (IOException ex) { throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex); } return baos.toByteArray(); } public static Object deserialize(byte[] bytes) { if (bytes == null) { return null; } try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { return ois.readObject(); } catch (IOException ex) { throw new IllegalArgumentException("Failed to deserialize object", ex); } catch (ClassNotFoundException ex) { throw new IllegalStateException("Failed to deserialize object type", ex); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy